Skip to content

Conversation

@dmgcodevil
Copy link
Owner

Implemented a thread-safe intrusive reference counting system for StringRef to solve critical concurrent string access issues in TundraDB. This replaces the previous unsafe string management approach that risked use-after-free bugs during concurrent node updates.


Problem Statement

Before this PR:

  • ❌ No automatic string lifetime management
  • ❌ Risk of use-after-free during concurrent updates
  • ❌ Manual deallocation required (error-prone)
  • ❌ No protection against dangling pointers
  • ❌ Reference counting required expensive map lookups

Specific issue:

// Thread 1: Reading node field
StringRef name = node->get_field("name");

// Thread 2: Updates same field
node->set_field("name", new_value);
// ❌ Old string immediately deallocated
// ❌ Thread 1's StringRef now dangling! 💥

Solution

Implemented intrusive reference counting where the ref count is stored IN the arena memory, directly before the string data. This provides:

  1. Automatic Memory Management: Strings deallocated when last reference destroyed
  2. Two-Phase Deletion: Mark for deletion → deallocate when safe
  3. Zero-Lookup Ref Counting: No map overhead, just pointer arithmetic
  4. Thread-Safe: Lock-free atomic operations for ref counting
  5. Cache-Friendly: Header adjacent to string data (single cache line)

Memory Layout:

Arena Memory:
┌────────────┬────────┬─────────┬─────────┬──────────────────┐
│ ref_count  │ length │  flags  │ padding │  string data...  │
│   (4B)     │  (4B)  │  (4B)   │  (4B)   │                  │
└────────────┴────────┴─────────┴─────────┴──────────────────┘
      ↑                                    ↑
   Header (16 bytes)                   StringRef.data_ points here

Architecture Changes

New Clean Header Structure:

value_type.hpp        ← ValueType enum + type system helpers (no dependencies)
    ↓
string_ref.hpp        ← StringRef class (intrusive ref counting)
    ↓
string_arena.hpp      ← StringPool + StringArena (string storage)
    ↓
types.hpp             ← Value + ValueRef (value containers)

@dmgcodevil dmgcodevil merged commit 7177dc2 into main Oct 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants