Это видео недоступно.
Сожалеем об этом.

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...]

Комментарии • 56

  • @notnullnotvoid
    @notnullnotvoid 3 года назад +17

    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.

  • @rohandvivedi
    @rohandvivedi 3 года назад +9

    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

  • @MH-ri2fb
    @MH-ri2fb 3 года назад +5

    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

  • @MsRP2Bang
    @MsRP2Bang 3 года назад +1

    Your videos are very informative, I always have to see twice to understand completely

  • @shobhitmishra92
    @shobhitmishra92 3 года назад +2

    Your videos are easy to understand than most of the material out there...

    • @JacobSorber
      @JacobSorber  3 года назад

      Thanks, Shobhit. I'm glad I could help.

  • @prshntpnd00
    @prshntpnd00 3 года назад

    Very helpful , thanks you so much 🙏🏻

  • @brijohn1
    @brijohn1 3 года назад +4

    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.

    • @JacobSorber
      @JacobSorber  3 года назад +2

      Ah. Good point. I had forgotten that Linux did it differently. Thanks.

  • @codyverdes3289
    @codyverdes3289 3 года назад +17

    How do we remove Alive Code?

    • @LFSDK
      @LFSDK 3 года назад +27

      rm -rf -no-preserve-root /

    • @marusdod3685
      @marusdod3685 3 года назад +7

      @@LFSDK dont forget sudo

  • @rafalmichalski4893
    @rafalmichalski4893 3 года назад +2

    Great video as usual. I think there should be such materials showing different compiler/linker flags.

    • @JacobSorber
      @JacobSorber  3 года назад +4

      The compiler and linker docs have this information. There's just a lot to read.

  • @JonnyRobbie
    @JonnyRobbie 3 года назад +1

    What about some IDE hint - similar to live compile and error checking/linting - one which would highlight an actual dead code.

  • @shekharmaela2308
    @shekharmaela2308 3 года назад +2

    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?

  • @Spiderboydk
    @Spiderboydk 2 года назад +1

    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.

  • @shivam4428
    @shivam4428 3 года назад +2

    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

    • @GeekyMino
      @GeekyMino 3 года назад

      Yes I would like to see a video like this too👍

  • @XenoTravis
    @XenoTravis 3 года назад

    A video on the different methods of sleeping a program would be cool!!!

  • @IsraelBuitronD
    @IsraelBuitronD 3 года назад +2

    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.

    • @JacobSorber
      @JacobSorber  3 года назад +2

      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

  • @artemiocabrillosjr.244
    @artemiocabrillosjr.244 3 года назад +3

    Hi Jacob, thank you for this. Is there any compiler option to display warnings if ever there are any deadcode in my code?

    • @JacobSorber
      @JacobSorber  3 года назад +4

      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.

    • @artemiocabrillosjr.244
      @artemiocabrillosjr.244 3 года назад

      Jacob Sorber thank you 👌👌

    • @not_ever
      @not_ever 3 года назад +2

      Static analysis tools such as Cppcheck and clang-tidy can sometimes catch dead code your compiler doesn't warn on.

  • @user-xm7dt3ec1o
    @user-xm7dt3ec1o 3 года назад

    大変参考になります。

  • @mazenosama8558
    @mazenosama8558 3 года назад

    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 ?

  • @RahulPatil-oi6kk
    @RahulPatil-oi6kk 3 года назад +1

    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?

    • @JacobSorber
      @JacobSorber  3 года назад +2

      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.

    • @DarkMonsterGFX
      @DarkMonsterGFX 3 года назад +1

      @@JacobSorber Hi Jacob, would you make some unit testing videos in the future?

    • @JacobSorber
      @JacobSorber  3 года назад +2

      @@DarkMonsterGFX Sure. I'll add it to the topic list and see what I can do.

    • @RahulPatil-oi6kk
      @RahulPatil-oi6kk 3 года назад

      thank you Jacob.

  • @anuragtiwari1560
    @anuragtiwari1560 3 года назад

    hey! jacob, where did you learn all this.

    • @JacobSorber
      @JacobSorber  3 года назад +1

      Here and there. Classes, projects, and just messing around.

  • @MaheshKumar-lq1xm
    @MaheshKumar-lq1xm 3 года назад

    Can you please do a video on simple json parsing in C ?
    It's super complicated. And makes me think it's horrible

    • @JacobSorber
      @JacobSorber  3 года назад +2

      Are you thinking about JSON parsing from scratch in C or using an existing library?

    • @MaheshKumar-lq1xm
      @MaheshKumar-lq1xm 3 года назад

      @@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.

    • @JacobSorber
      @JacobSorber  3 года назад +1

      @@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.

    • @MaheshKumar-lq1xm
      @MaheshKumar-lq1xm 3 года назад +2

      @@JacobSorber I just had a quick look. And that's exactly what I am looking for...
      You are a master.

    • @EshmesVid
      @EshmesVid 3 года назад +1

      @@JacobSorber Might still be worth a video ,about parsing techniques. How to load files, how to store complex data etc.

  • @samplesandtests
    @samplesandtests Год назад

    did any one notice that printf was optimized out and replaced with puts?

  • @mathslover76
    @mathslover76 3 года назад +1

    How can get shirt that you weared in India

    • @JacobSorber
      @JacobSorber  3 года назад +1

      teespring.com/stores/jacob-sorbers-store
      Teespring says they ship worldwide, though I've never tried to order from India.

    • @mathslover76
      @mathslover76 3 года назад

      @@JacobSorber thanks to reply me

  • @LabyrinthMike
    @LabyrinthMike 3 года назад

    I consider it very bad form for header files to generate code at all. I prefer to have declarations in there only.

  • @cmdlp4178
    @cmdlp4178 3 года назад

    Here are some other arguments, that can decrease the size
    -fwhole-program
    -s
    -flto

  • @nexovec
    @nexovec 3 года назад

    Preprocessor doesn't care about functions at all... it just does text operations. This is a linker issue.

  • @glowiever
    @glowiever 3 года назад

    I never bothered and just compile release mode.

  • @MG-qu9ig
    @MG-qu9ig 3 года назад +1

    So basically one should convert normal code into a release code.

    • @JacobSorber
      @JacobSorber  3 года назад

      This is definitely one of the things you might do when generating a release build.

  • @KoltPenny
    @KoltPenny 2 года назад

    For example any Java program is 99% dead code.

  • @diceandbricks
    @diceandbricks 3 года назад +1

    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.

    • @JacobSorber
      @JacobSorber  3 года назад +2

      Amen. I couldn't agree more.