Oct 8, 2019

[C++][CppCon 2019] There Are No Zero-cost Abstractions

Reference:
https://www.reddit.com/r/cpp/comments/degmy1/cppcon_2019_chandler_carruth_there_are_no/

1. google protobuf: adding an arena allocator makes compile times explode
2. unique_ptr: from hidden bugs down to ABI
3. human cost when extracting block of code to a separate function (... and overdoing it)

Arena based memory allocation.
https://vsdmars.blogspot.com/2019/01/golang-golangs-memory-management-eben.html


1. Compile & build time are non-zero costs (of course)
2. std::unique_ptr (same runtime cost as raw pointer)

Each abstraction must provides more benefit than cost.

How to use abstractions safely?

  • Simpler abstractions - don't use a type when a function would do.
  • Think about where you want to pay the inevitable cost.
  • Work to reduce that cost as much as possible.
  • Measure the cost in multiple dimensions
    • Especially if you expect that dimension to be zero.
  • Ensure that in the end the benefit provided outweighs the cost.


Abstractions aren't bad, it's the tool/language that is in bad design...

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.