Struct StringId

Struct Documentation

struct StringId

Compile-time string identifier using 64-bit hash for efficient lookups.

StringId enables using strings as identifiers (for resources, entities, events, etc.) while maintaining performance comparable to integer keys. The design philosophy is: the hash is the identity, the string is for debugging.

Two StringId instances are equal if their id (hash) fields match. The string field is purely for logging, debugging, and UI display - it does not participate in equality or hash map operations.

Compile-Time Hashing: Use the STRING_ID() macro for string literals known at compile time. On GCC/Clang, the hash is computed at compile time (constexpr) and baked into the binary, resulting in zero runtime cost. On MSVC, the hash is computed at runtime due to compiler limitations with constexpr, but is still highly optimized (inlined).

Runtime Construction: For runtime strings (user input, loaded data), construct using the same macro: STRING_ID(str). This stores the string in the global StringRegistry for lifetime management and debug lookup.

Hash Collisions: The system assumes 64-bit rapidhash collisions are astronomically unlikely (2^64 keyspace). There is no runtime collision detection - equality is purely hash-based.

Example - Compile-time string IDs:

// Hash computed at compile-time (on GCC/Clang)
constexpr auto player_id = STRING_ID("game/player");
constexpr auto texture_id = STRING_ID("textures/ground.png");

// Use as resource keys
auto* player_mesh = resources.get<Mesh>(player_id);
auto* ground_tex = resources.get<Texture>(texture_id);

Example - Runtime string IDs:

// Runtime string from user input
std::string entity_name = read_from_config();
StringId entity_id = STRING_ID(entity_name);

// String is stored in registry for debugging
LOG_INFO("Created entity: {}", entity_id);  // Prints: Created entity: id("MyEntity")

Example - Using as hash map key:

// Works with both LLVM DenseMap and std::unordered_map
llvm::DenseMap<StringId, Entity*> entities;
std::unordered_map<StringId, Texture*> textures;

entities[STRING_ID("player")] = player_entity;

See also

StringRegistry for string storage and lifetime management

See also

hash::rapidhash for the hashing algorithm (rapidhash V3 based on wyhash)

Public Types

using HashType = uint64_t

Type alias for the hash value (64-bit unsigned integer).

Public Functions

constexpr StringId() = default

Default constructor creating an invalid StringId (id=0, string=”Invalid”).

explicit StringId(HashType id)

Constructs StringId from hash alone, looking up the string in StringRegistry.

This constructor queries the global StringRegistry for the corresponding string. If not found, logs an error and sets string to “Invalid”. This is primarily used during deserialization when receiving a hash from disk/network.

Parameters:

id – The 64-bit hash value

StringId(HashType id, std::string_view string)

Constructs StringId from hash and string, storing the string in StringRegistry.

This stores the string in the global registry (if not already present) and sets the string field to point to the registry’s copy. The string persists for the application’s lifetime. Use this for runtime string construction.

Parameters:
  • id – The 64-bit hash (typically from hash::rapidhash(string))

  • string – The string to associate with this hash

StringId(HashType id, const std::string &string)

Constructs StringId from hash and std::string, storing in StringRegistry.

Parameters:
  • id – The 64-bit hash

  • string – The string to associate with this hash

bool operator==(const StringId &other) const

Equality compares ONLY the hash (id field). String is ignored.

Parameters:

otherStringId to compare against

Returns:

true if hashes match, false otherwise

Public Members

HashType id = 0

The 64-bit hash - this IS the identity. Equality compares only this field.

std::string_view string = INVALID_STRING_VIEW

Human-readable string view for debugging/display. NOT used for equality or hashing. Points to either a string literal in the binary or a string in the StringRegistry.