Skip to content

Introduce custom internal "cm::String" type

Brad King requested to merge brad.king/cmake:custom-string into master

Internally CMake makes many copies of heap-allocated strings. Some people have proposed string tables to reduce allocations, but the lifetime management of entries in such tables is not clear. Instead we can us shared ownership of immutable strings to avoid extra allocations and copies. std::shared_ptr<std::string const> expresses this but is not ergonomic to use on its own.

Additionally, we have many "string literals" used with APIs accepting std::string, thus causing unnecessary std::string allocation. std::string_view can express such borrowing but does not interact well with APIs using std::string.

Create a new cm::String type that captures both of the above concepts behind a single ergonomic interface with value semantics. Expose views through const char* and std::string const& (via internal mutation as needed) to offer easy inter-operation with existing APIs. This will allow us to incrementally migrate structures and APIs to the new string type over time.

Use a cm::string_view fallback implementation for compilers where std::string_view is not available.

Issue: #18456 (closed)

Merge request reports