luastring

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 1 Imported by: 0

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

View Source
const MaxShortLen = 40

MaxShortLen is the maximum length for interned short strings (LUAI_MAXSHORTLEN = 40).

Variables

This section is empty.

Functions

func Hash

func Hash(data string, seed uint32) uint32

Hash computes the Lua string hash for the given bytes with the given seed. This matches C's luaS_hash exactly — iterates ALL bytes from end to start.

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) Seed

func (st *StringTable) Seed() uint32

Seed returns the hash seed.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL