Это видео недоступно.
Сожалеем об этом.
How to Automatically Remove Dead Code?
HTML-код
- Опубликовано: 27 сен 2020
- Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
Extra Headers? Does My Compiler Automatically Remove Dead Code? // New programmers often worry if there's a downside to including header files that we don't use. What about dead code - code that I write, but don't every call? In this video, we look into the issue of dead code and what our compilers can do to help us remove it.
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
+ rep the channel with nerdy merch --- [teespring.com/stores/jacob-so...]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]
Something very useful to know is that if a function is marked static, then the compiler can see that it isn't called from within that translation unit and omit it from the object file before the linker even sees it. Clang and MSVC do this even at -O0, and GCC does it at -O1 and above. Marking functions static will also help the compiler to know that it should inline if it sees that the function is only called from one place.
To anyone who wants more details about how he did it.
1. There is a -ffunction-sections, a compiler option that creates separate sections for each one of your functions on the compilation step. (else only 1 .text section is created for a translation unit)
2. then the -dead_strip linker option (I guess this is Mac-specific), that removes the sections to which you never jumped to from your main function. This is because only while linking, your compiler may check all the generated symbol tables from different translation units.
@Jacob Sorber, If possible can you make a video on linker scripts and code layout as well, for embedded system.
Thank you so much for your videos, I really appreciate your work
Another method that has been effective in my company’s embedded compilation is link time optimization (provided that your compiler supports LTO plug-in of course). We’ve had better success in some cases with -O2 -flto than with -Os actually! Though I have heard for some LTO bloats their binaries, so YMMV - most likely depends on the toolchain’s implementation
Your videos are very informative, I always have to see twice to understand completely
Your videos are easy to understand than most of the material out there...
Thanks, Shobhit. I'm glad I could help.
Very helpful , thanks you so much 🙏🏻
Just wanted to point out the -dead_strip linker flag is specific to the macosx linker. If you are using linux and the gnu linker, you should instead use --gc-sections as the linker flag for removing unused code.
Ah. Good point. I had forgotten that Linux did it differently. Thanks.
How do we remove Alive Code?
rm -rf -no-preserve-root /
@@LFSDK dont forget sudo
Great video as usual. I think there should be such materials showing different compiler/linker flags.
The compiler and linker docs have this information. There's just a lot to read.
What about some IDE hint - similar to live compile and error checking/linting - one which would highlight an actual dead code.
Hey Jacob, I was just watching your video on pthreads and it made me wonder.
Whats the difference between manual threading via threads.h/pthreads, and using OpenMP to do it for you?
When should you use one or the other, what are the performance differences?
Alternatively, if you compile the entire program in a single translation unit, you can make all your functions static. If they're not called within your translation unit and can't be called from another translation unit, the compiler can safely reason that it will never be called and cull it.
Can u make a video on how to study someone else project code ... Tools used. Because in industry we need to read lot of codes sometimes... so how we can easily do it
Yes I would like to see a video like this too👍
A video on the different methods of sleeping a program would be cool!!!
Hi Jacob, Thank you for your videos! They are helpful. I am also a CS professor (in México) so I give your videos to my students. I was wondering if there is a way to contribute to your videos by giving a translation to spanish. Please consider it. I am interested to do that. Greetings from Mexico.
Hi Israel. Thanks. I would love to have Spanish captions. Sadly, RUclips disabled community submitted captions back in September. They're saying that a trusted captioner feature is coming in the next year, but I haven't seen it yet. support.google.com/youtube/thread/73769191?hl=en
Hi Jacob, thank you for this. Is there any compiler option to display warnings if ever there are any deadcode in my code?
Check out the diagnostic -W flags for clang/gcc. For example -Wunreachable-code will warn about unreachable code inside a function, but it won't tell you if you have entire functions that aren't being called.
Jacob Sorber thank you 👌👌
Static analysis tools such as Cppcheck and clang-tidy can sometimes catch dead code your compiler doesn't warn on.
大変参考になります。
exactly
so microcontroller compiler IDE's provide commands like these to the complier to strip down the deadcode in the background ? ... i mean is that what the IDE is doing ?
Hi Jacob, while unit testing if function have pointer(of any type) as a argument then is it always necessary to test for null pointer for that pointer argument?
It depends. There's an implicit agreement between a person who writes a function and a person who calls that function. Many programmers make it explicit, and actually write down what is expected. If part of the agreement is that the user will *never* pass in a null pointer, and you actually trust the caller, then you don't have to check for NULL pointers. And, not checking will make your code a little faster. But, that's a lot of trust. Usually, I'm optimizing for my time and sanity, rather than trying to trim a few cycles, so I usually check if it's a valid run-time occurrence, or I stick an assert in there if it's not.
@@JacobSorber Hi Jacob, would you make some unit testing videos in the future?
@@DarkMonsterGFX Sure. I'll add it to the topic list and see what I can do.
thank you Jacob.
hey! jacob, where did you learn all this.
Here and there. Classes, projects, and just messing around.
Can you please do a video on simple json parsing in C ?
It's super complicated. And makes me think it's horrible
Are you thinking about JSON parsing from scratch in C or using an existing library?
@@JacobSorber thanks for replying..
I tried cjson, jasson-c but they are soo big and I just need simple json to object.
Any suggestions ?
By the way you are awesome.
@@MaheshKumar-lq1xm Thanks. Have you tried JSMN? zserge.com/jsmn/
I admittedly, don't do a lot of JSON parsing in C. It's just so much easier in just about any other language. JSMN sounds like one I might like. If I get a chance to play with it, it could be an interesting video idea.
@@JacobSorber I just had a quick look. And that's exactly what I am looking for...
You are a master.
@@JacobSorber Might still be worth a video ,about parsing techniques. How to load files, how to store complex data etc.
did any one notice that printf was optimized out and replaced with puts?
How can get shirt that you weared in India
teespring.com/stores/jacob-sorbers-store
Teespring says they ship worldwide, though I've never tried to order from India.
@@JacobSorber thanks to reply me
I consider it very bad form for header files to generate code at all. I prefer to have declarations in there only.
Here are some other arguments, that can decrease the size
-fwhole-program
-s
-flto
Preprocessor doesn't care about functions at all... it just does text operations. This is a linker issue.
I never bothered and just compile release mode.
So basically one should convert normal code into a release code.
This is definitely one of the things you might do when generating a release build.
For example any Java program is 99% dead code.
Even with these tools, it's good practice to delete your dead code from source files. Because it will go unmaintained and languish, until somebody decides to call that code and who knows what's gonna happen.
Amen. I couldn't agree more.