Mojo module
inline_string
Implements a string optimized for storing small strings.
The main type is InlineString
which implements small-string optimization to avoid
heap allocations for short strings. For strings shorter than SMALL_CAP
(24 bytes),
the data is stored inline in the struct's memory layout. For longer strings, it
falls back to heap allocation.
Key Features:
- Small-string optimization avoiding heap allocations for short strings
- Efficient concatenation and mutation operations
- UTF-8 encoding support
- Memory safety through bounds checking
The module also provides _FixedString
, an internal type used by InlineString
for
the small string optimization case. _FixedString
provides a fixed-capacity string
implementation with inline storage.
Example:
from collections.string.inline_string import InlineString
var s = InlineString("Hello") # Stored inline, no heap allocation
s += " World" # Still inline
s += " with a much longer string" # Now uses heap allocation
from collections.string.inline_string import InlineString
var s = InlineString("Hello") # Stored inline, no heap allocation
s += " World" # Still inline
s += " with a much longer string" # Now uses heap allocation
Structs
-
InlineString
: A string that performs small-string optimization to avoid heap allocations for short strings.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!