Skip to content

perf: introduce SmartString, I64Map, and value interning for memory efficiency#10

Merged
semihalev merged 1 commit intomainfrom
perf/smartstring-i64map-optimizations
Jan 17, 2026
Merged

perf: introduce SmartString, I64Map, and value interning for memory efficiency#10
semihalev merged 1 commit intomainfrom
perf/smartstring-i64map-optimizations

Conversation

@semihalev
Copy link
Copy Markdown
Member

Summary

  • SmartString: Custom SSO string with inline (≤22 bytes), Owned, and Shared variants - replaces CompactString throughout codebase
  • I64Map: High-performance hashmap for i64 keys using sentinel-based design (~45% faster lookups)
  • Value Interning: Shared instances for NULL, booleans, and integers 0-1000 to reduce allocations
  • Aggregation Fix: Fixed hash collision bug in single-column GROUP BY that could cause incorrect groupings
  • SIMD Pattern Matching: memchr::memmem for LIKE '%pattern%' substring search

Changes

New Data Structures

  • SmartString - 24-byte SSO string with O(1) clone for shared variant
  • I64Map - Linear probing hashmap with backward-shift deletion
  • value_interner - Global interning for common Value instances

Performance Optimizations

  • Single-column GROUP BY with primitives uses I64Map directly (~3x faster)
  • 2-column GROUP BY uses tuple keys (30% faster than Vec)
  • Pre-compiled SIMD Finder for contains patterns
  • CompactVec replaces SmallVec in indexes (16 bytes vs 48 bytes)

Bug Fixes

  • Fixed aggregation hash collision bug where different values with same hash were incorrectly grouped together

Code Quality

  • Thorough SAFETY documentation on all unsafe blocks
  • Consistent migration from CompactString to SmartString across 55+ files

Test plan

  • All existing tests pass (cargo nextest run)
  • Clippy passes with no warnings
  • Code review completed

@semihalev semihalev force-pushed the perf/smartstring-i64map-optimizations branch from c8531d9 to c4ff948 Compare January 17, 2026 08:49
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 17, 2026

@semihalev semihalev force-pushed the perf/smartstring-i64map-optimizations branch from c4ff948 to e2ee492 Compare January 17, 2026 09:11
…fficiency

Major optimizations:

**SmartString** - Custom SSO string replacing CompactString
- Inline storage for strings ≤22 bytes (no heap allocation)
- Owned (Box<str>) for computed values, Shared (Arc<str>) for cloned values
- 24-byte size with O(1) clone for shared variant

**I64Map** - High-performance hashmap for i64 keys
- Uses i64::MIN as empty sentinel (row/txn IDs are always ≥0)
- FxHash-based with linear probing and backward-shift deletion
- ~45% faster lookups than FxHashMap<i64, V>

**Value Interning** - Shared instances for common values
- Interned: NULL (all 7 DataTypes), booleans, integers 0-1000
- Reduces allocations for frequently used values in rows

**Aggregation Fixes & Optimizations**
- Fixed hash collision bug in single-column GROUP BY (was using hash as key)
- Added 2-column tuple optimization (30% faster than Vec<Value>)
- Single-column primitive GROUP BY uses I64Map directly

**SIMD Pattern Matching**
- memchr::memmem for LIKE '%pattern%' substring search
- Pre-compiled Finder stored in CompiledPattern

**Other Improvements**
- CompactVec: new insert(), retain(), drain() methods
- All indexes migrated to I64Map and CompactVec
- Thorough SAFETY documentation on unsafe code
@semihalev semihalev force-pushed the perf/smartstring-i64map-optimizations branch from e2ee492 to f7ceb1a Compare January 17, 2026 10:04
@semihalev semihalev merged commit 221606b into main Jan 17, 2026
12 checks passed
@semihalev semihalev deleted the perf/smartstring-i64map-optimizations branch February 27, 2026 05:50
mmacedoeu pushed a commit to mmacedoeu/stoolap that referenced this pull request Mar 2, 2026
…fficiency (stoolap#10)

Major optimizations:

**SmartString** - Custom SSO string replacing CompactString
- Inline storage for strings ≤22 bytes (no heap allocation)
- Owned (Box<str>) for computed values, Shared (Arc<str>) for cloned values
- 24-byte size with O(1) clone for shared variant

**I64Map** - High-performance hashmap for i64 keys
- Uses i64::MIN as empty sentinel (row/txn IDs are always ≥0)
- FxHash-based with linear probing and backward-shift deletion
- ~45% faster lookups than FxHashMap<i64, V>

**Value Interning** - Shared instances for common values
- Interned: NULL (all 7 DataTypes), booleans, integers 0-1000
- Reduces allocations for frequently used values in rows

**Aggregation Fixes & Optimizations**
- Fixed hash collision bug in single-column GROUP BY (was using hash as key)
- Added 2-column tuple optimization (30% faster than Vec<Value>)
- Single-column primitive GROUP BY uses I64Map directly

**SIMD Pattern Matching**
- memchr::memmem for LIKE '%pattern%' substring search
- Pre-compiled Finder stored in CompiledPattern

**Other Improvements**
- CompactVec: new insert(), retain(), drain() methods
- All indexes migrated to I64Map and CompactVec
- Thorough SAFETY documentation on unsafe code
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.

1 participant