‹ BackHN Continuity

Thread

Writing Efficient C++ Code (2013)

176 points · 161 comments · ibobev

  1. 112233 · · focus · HN ↗
    "This article was originally published in Polish in issue 4/2013" — a lot of excellent advice. Sad to see C++ have moved in last decade in a direction that makes writing efficient, simple low level code harder and harder :(
    1. fooblaster · · focus · HN ↗
      How? you can write exactly the same low level code today.
      1. beached_whale · · focus · HN ↗
        My thought too.

        There are so many things that are expressible in C++ now that could not be without writing much more code or using per-compilation tools back then. The ability to run code at compile time that is not run at runtime is huge, #embed lets us make other tools output available without linker scripts or compiler specific tools that.

        Also, most of the code from the past still works(from 10 years ago definitely works)

      2. AlotOfReading · · focus · HN ↗
        Shot in the dark, but maybe the OP is referring to the fact that these code conventions are explicitly discouraged by the C++ core guidelines. The SoA example falls afoul of the rule requiring T* to be used only for singular object pointers, for example.
        1. cjbgkagh · · focus · HN ↗
          Not a regular C++ programmer but wouldn’t you use std::span here instead? Sure it’ll carry a few redundant lengths but it makes using functions that take spans easier. When I do write C++ it’s usually for speed so I’m often working at the intrinsics level, though AI has gotten good enough at it that I now generally delegate this work to an agent.
          1. jandrewrogers · · focus · HN ↗
            Depending on the specific code, the compiler may even eliminate the redundant lengths.
        2. someonebaggy · · focus · HN ↗
          Core guidelines, and any other pattern document, should be downstream of working code, not upstream.
          1. AlotOfReading · · focus · HN ↗
            Okay? The point of the guidelines is to document principles the committee's thinks "good" C++ should follow, within the much larger universe of possible C++ code.
            1. someonebaggy · · focus · HN ↗
              So if you find good code that works a different way, do you update your beliefs on what good code is, or on whether that code is good?
              1. AlotOfReading · · focus · HN ↗
                To summarize, the grandparent comment lamented how it's sad that the modern language has diverged from the code in the article. Someone else pointed out that the code still works. I suggested that the complaint remains valid in spite of that because the language committee's core guidelines prohibit it. Notice I haven't mentioned my opinion at all.

                You've responded to that suggestion with seemingly irrelevant comments. Do you see why I'm confused?

                For what it's worth, my actual opinion is that the committee is usually wrong/misguided.

            2. 112233 · · focus · HN ↗
              Yes? Instead, we have committee declaring large bodies of existing code invalid and non-conformant, because it does not fit their spherical cow C++. You would think, with amount of no-exceptions and no-rtti code out there, and feature being present in most compilers for decades, they would take a hint and reflect actual usage in their standard? No, why would they. Any C++ that has no exceptions, rtti or std:: namespace is invalid. Not even against guidelines.
            3. tcfhgj · · focus · HN ↗
              the CCG isn't from the committee, but from Bjarne Stroustrup and Herb Sutter.

              Bjarne is often in disagreement with the committee

      3. 112233 · · focus · HN ↗
        eh, yes-ish, but only thanks to compiler writers. at one point committee went all-out enforcing their lifetime model, making bit_cast not an option. If your code accesses same data using different types, you are spelunking ruins with snake pits and lava. more and more stuff needs magic support code in std::, making no-lib code less and less possible (it used to be that with no-rtti and no-exceptions, you could use all c++ features and only needed cxa_at_exit, operator delete, and few other little things. NOT ANY MORE).

        On one hand, you have consteval and stuff, letting you FINALLY initialize data at compile time (hey, 20 years late but still!)

        on other hand, it is done in most non-debuggable way possible. try setting breakpoint or adding print to constexpr function that causes your requires clause to fail...

        so no, newer C++ the language is not possible to use for low level work. The dialects that compiler makers support are. We will see for how long

        1. t0mpr1c3 · · focus · HN ↗
          > newer C++ the language is not possible to use for low level work

          This seems like an overstatement. Can you elaborate with some examples from your particular domain?

          1. 112233 · · focus · HN ↗
            In my domain, code size is critical, so we use -fno-exceptions and -fno-rtti from necessity, for a good reason. Which means our code is non-conformant C++. Committee forces everyone to have exceptions and rtti always.
            1. t0mpr1c3 · · focus · HN ↗
              OK. I would expect those flags to be required for most embedded work.

              But I could imagine use cases for new-ish constructs like `constexpr`, `std::variant`, template constraints, and so on.

              I don't quite follow what you are saying about the compilers. Are you saying that in future, compilers will have to choose between supporting new features and allowing non-conformant code?

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.