A short post on how variable names can leak out of macros if there is a name collision with a constant. I thought this was a delightful read!

  • livingcoder@programming.dev
    link
    fedilink
    arrow-up
    7
    ·
    3 months ago

    This was a great post, but is the last state of the macro actually bad for performance in any way? I get that it’s ugly (and we should only choose to make code less readable like this when there’s actually an issue) but is it worse for runtime performance?

    • TehPers@beehaw.org
      link
      fedilink
      English
      arrow-up
      9
      ·
      3 months ago

      Adding a single unused function should no effect on runtime performance. The compiler removes dead code during compilation, and there’s no concept at runtime anyway of “creating a function” since it’s just a compile-time construct to group reusable code (generally speaking - yes the pedants will be right when they say functions appear in the compiled output, to some extent).

      Anyway, this can all be tested on Godbolt anyway if you want to verify yourself. Make a function with and without a nested unused function and check the output.

    • arendjr@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      3 months ago

      Runtime performance is entirely unaffected by the use of macros. It can have a negative impact on compile-time performance though, if you overdo it.

      • livingcoder@programming.dev
        link
        fedilink
        arrow-up
        2
        ·
        3 months ago

        I understand that the macro only affects compile time but I’m talking about the extra function that’s included in the resulting source code when the macro is expanded during compile. Based on other feedback, it looks like the unused function is optimized away.