Documentation
¶
Overview ¶
Package api manages Lua string values with interning for short strings.
Short strings (≤ MaxShortLen bytes) are interned in a global table. Two short strings with the same content share the same *LuaString pointer, enabling O(1) equality via pointer comparison.
Long strings are not interned and compared by content.
The string table uses strong pointers. Dead interned strings are removed during GC sweep via RemoveString(), matching C Lua's approach where the sweep phase removes dead strings from the string table.
The string table caps its bucket array at maxStrTabSize to prevent OOM from unbounded growth.
Reference: .analysis/07-runtime-infrastructure.md §4 C source: lua-master/lstring.c
Index ¶
Constants ¶
const MaxShortLen = 40
MaxShortLen is the maximum length for interned short strings (LUAI_MAXSHORTLEN = 40).
Variables ¶
This section is empty.
Functions ¶
Types ¶
type StringTable ¶
type StringTable struct {
OnCreate func(object.GCObject) // V5: called when a new string is created (for GC linking)
// contains filtered or unexported fields
}
StringTable interns short strings for pointer-equality lookups. It is owned by GlobalState and shared across all threads.
func NewStringTable ¶
func NewStringTable(seed uint32) *StringTable
NewStringTable creates a string table with the given hash seed. Initial bucket count is 128 (MINSTRTABSIZE).
func (*StringTable) Count ¶
func (st *StringTable) Count() int
Count returns the number of interned short strings.
func (*StringTable) Intern ¶
func (st *StringTable) Intern(s string) *object.LuaString
Intern returns the canonical *LuaString for the given Go string. If the string is short (≤ MaxShortLen), it is interned (deduplicated). If long, a new LuaString is created each time (not interned).
func (*StringTable) InternBytes ¶
func (st *StringTable) InternBytes(b []byte) *object.LuaString
InternBytes is like Intern but accepts a byte slice.
func (*StringTable) RemoveString ¶
func (st *StringTable) RemoveString(ts *object.LuaString)
RemoveString removes a specific interned string from the string table. Called from GC sweep when a dead short string is found. This is the Go equivalent of C Lua's approach where sweep removes dead strings.
func (*StringTable) SweepStrings ¶
func (st *StringTable) SweepStrings()
SweepStrings optionally shrinks the bucket array if too sparse. With strong pointers, dead entries are removed individually by RemoveString() during GC sweep, so no dead-entry scanning is needed.