Development Day 6: String Pool

My first language was Java. So C++ does a few things where I said “why is C++ that way”, and there are other things where I saw “Java should’ve been this way”.

Java uses a String pool – for a touch more detail, see this StackOverflow question. The basic idea is that Strings are saved ahead of time in order to save memory.

ChronoWatch does things “by name”, meaning that it certainly would be nice if we used a String pool. But C++ doesn’t provide one.

The following code provides ChronoWatch with a String pool-like implementation. It’s a little slow because it’s a set, but it helps drastically lower the profiler’s memory usage.

#include <string>
#include <set>

using CW_Atom = const char*;

namespace ChronoTools
  static std::set<std::string> interned;

  inline CW_Atom atom_str(std::string const& value)
    return ChronoTools::interned.insert(value).first->c_str();

Time spent: 5 hours. (Other minor architecture changes were made to allow the string pool to work in the first place.)

Leave a Reply

Your email address will not be published. Required fields are marked *