November 10th, 2024

Speculations on arenas and custom strings in C++

The author explores arena allocation and custom strings in C++, aiming to simplify complexity by minimizing advanced features. Challenges with static initialization are noted, with proposed workarounds for compile-time construction.

Read original articleLink Icon
Speculations on arenas and custom strings in C++

The article discusses the author's exploration of arena allocation and custom string implementations in C++, building on previous techniques used in C. The author aims to simplify the complexity of C++ by minimizing the use of advanced features and focusing on a C-style approach. Key points include avoiding C++ standard library headers, limiting the use of keywords like class and const, and minimizing template usage to reduce compile times. The author introduces a streamlined arena allocation method and a custom string type, emphasizing the importance of explicit memory management and initialization. The article also highlights challenges faced with C++ constructors, particularly regarding static initialization and compile-time construction. The author proposes workarounds for these issues, such as using unions for compile-time string construction. Ultimately, while the new features offer advantages, the author expresses concern over the static data problem and plans to test these implementations in larger projects.

- The author experiments with arena allocation and custom strings in C++ while minimizing complexity.

- A streamlined arena allocation method is introduced, focusing on explicit memory management.

- Custom string types are developed, but challenges with static initialization in C++ constructors are noted.

- Workarounds for compile-time string construction are proposed, including the use of unions.

- The author plans to evaluate the practicality of these implementations in larger projects.

Link Icon 4 comments
By @amluto - 5 months
If I were doing this, I would accept one more C++-ism and implement a user-defined string literal, so one could do:

    "Some text"_s8
The underlying mechanism is even polite enough to supply a length without a horrible array template hack. (A somewhat unusual case where the C mechanism is a complete fail, the older C++ mechanism works but is incredibly ugly, and the newer C++ mechanism is actually straightforward and comprehensible.)

https://en.cppreference.com/w/cpp/language/user_literal

By @einpoklum - 5 months
> the C++ standard library offers me little.

Lost me there already.

> Its concepts regarding ownership and memory management are irreconcilable (move semantics, smart pointers, etc.), so I have to build from scratch anyway.

Absolutely not. The standard library has a zillion things, most pairs of which are independent and even unrelated. No type traits, no pairs and tuples, no std::arrays etc. - all things that are completely independent from memory management and ownership.

By @olliej - 5 months
Regarding the template make function, there’s a c++ proposal I’ve been working on: https://wg21.link/p2719

This provides the type being allocated to the operator new implementation.

If you want to experiment here’s the implementation: https://github.com/llvm/llvm-project/pull/113510

By @plainlee - 5 months