inline functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi I'm calling walls and I'm here again to talk about embedded software today I want to revisit four subjects I've looked at before but it's quite important and there are other angles on it which I want to explore and that subject is the inlining of C and C++ functions the concept here is very simple if you are writing code particularly if it's being optimized for speed the compiler could simply take a call to the code to a function and insert the actual code of the function now that makes sense if you're compiling to speed because you've eliminated the call and return sequence so the code will execute faster however it may make the code overall Forums rather larger simply because it does multiple calls each one of them will get a copy of the function so it only makes sense if you're talking about a small function and if speed is your priority now compilers can do that automatically but there is also the key word in line now that was created in the C++ language and found its way into c nowadays it was an extension to many compilers key word sense anyway and what this does this gives a hint to the compiler that inlining might be a cool idea it's only a hint because if you try to inline a very large function you generate something followed with your volley ridiculous so that's the idea of igniting there are one or two issues one of them is that compilers may generate an out of line copy of the function as well now why would they do that well there's two reasons one is some debuggers are just not quite smart enough to deal with inline code it's not good but that's the way it is so you end up with this unnecessary overhead just to deal with debugging not a very satisfactory situation the other reason for having a out of line copy is once the functions been declared it's possible that there will be an extern declaration from another module and therefore it would need to link to an independent function so therefore and out of line copy is necessary now if you end up with out of line copies which are redundant then a small linker will sort that out for you but that's another thing to worry about a simple way around this is when you create an inline function make it static in fact if you make you static and you can power speed you probably don't even need the inline keyword because the compiler will then know that that's because they function is local to this particular module the options in place for this module are the way it should compile the code and it will inline is appropriately so that gets you around the problem let's look at an example of inlining and we'll talk through the sequence of events so here we have some code at the top we have a function called ant that simply takes two integer parameters adds them together and returns the result fairly trivial but just an example underneath this we have a main function and in there we have two calls to add with different parameters which are returning the result so that's what the code looks like and if you compile it conventionally then each of the calls that Erik would indeed produce a call return sequence and it would do which want it to do if we compile for with speed as our option and we allow inlining to occur then the resulting code will be functionally just like this so instead of the calls to add we have the expression z equals x plus y in each place in that particular case the code will actually not be any bigger if it'll probably be smaller overall and faster so it's bit of a win-win very simple almost trivial example but it gives you the idea of what inlining is all about just for fun let me build upon my comment there about it being trivial as I'm sure several viewers will have figured out already the triviality of this example will work against me because but the compiler will actually do with this code is generate something more like this it will generate Z equals 1 plus 2 instead of ever creating the variables x and y actually it'll go one step further than that the code could end up like this because the 1 plus 2 gets evaluates to a compile time to be 3 and the females floor gets evaluated a 7 all the compile time and the assignments just occur actually it gets even worse because the compiler will spot that the variable Z is never actually used for anything so we might as well not compile any code at all so my example comes down to this I told you it was a trivial example that's the danger of triviality sometimes but nevertheless I hope you get the idea of what inlining is all about so that's it for today I will be back soon with another story about embedded software see you then [Music]
Info
Channel: Siemens Embedded
Views: 4,881
Rating: 4.9340658 out of 5
Keywords:
Id: gINI12EMbds
Channel Id: undefined
Length: 5min 45sec (345 seconds)
Published: Thu Feb 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.