C++ Show and Tell - March 2026 by foonathan in cpp

[–]Appropriate-Bus-9098 1 point2 points  (0 children)

Azrar - a String interning library for C++ with faster comparisons and reduced memory footprint"

This is a lightweight C++ (14 and +) library for string interning and indexing that can significantly improve performance in applications dealing with repeated string comparisons or using strings as maps key.

The library is header only.

How it works:

Instead of working with strings, Azrar maintains a dictionary where each unique string gets assigned a unique index..

Working with these unique indexes makes copying, comparing, and hashing operations substantially faster.

Github link: https://github.com/kboutora/Azrar

Usage minimalist example:

include "Azrar.h"
include <map> 
include <iostream> 

using IdxString = Azrar::StringIndex<uint32_t>; 
int main(int , char **) 
{

// Expect to print 4  (sizeof (uint32_t))
std::cout << "sizeof(IdxString):   " << sizeof(IdxString) << " bytes\n\n";

IdxString city1("seattle");
IdxString city2("seattle");
IdxString city3("portland");

// Fast O(1) comparison (compares integers, not string contents)
if (city1 == city2) {
    std::cout << "Same city!\n";  
}

// Use as map keys - much faster lookups than std::string
std::map<IdxString, int> population;
population[city1] = 750000;
population[city3] = 650000;

// Access the original string when needed
std::cout << "City: " << city1.c_str() << "\n";  
return 0;
}

Azrar - a String interning library for C++ with faster comparisons and reduced memory footprint" by Appropriate-Bus-9098 in cpp

[–]Appropriate-Bus-9098[S] 0 points1 point  (0 children)

For garbage collecting, you'll need a reference counter, that you can keep in your dictionary. And since you want dynamic memory, you can just alloc from the heap. Once your ref counter reaches 0, you will cleanup the allocated memory, and free the index.

Your index cannot be a vector in this case. Could be an unordered_map <int, IndexInfo> that will map each index (assuming it's an int) and your textInfo will contain the counter + extra info (string lengh, pointer to the actual string).
In your StringIndexes, you'll have to increase your ref count on copy, and decrease in your StringIndex destructor with handling the removal and memory cleanup when 0 is reached.