Documentation
¶
Overview ¶
Package intern provides an interning table abstraction to optimize symbol resolution.
Index ¶
- type ID
- type Map
- type Set
- type Stats
- type Table
- func (t *Table) Intern(s string) ID
- func (t *Table) InternBytes(bytes []byte) ID
- func (t *Table) Preload(ids any)
- func (t *Table) Query(s string) (ID, bool)
- func (t *Table) QueryBytes(bytes []byte) (ID, bool)
- func (t *Table) RecordStats(b bool)
- func (t *Table) Stats() Stats
- func (t *Table) Value(id ID) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ID ¶
type ID int32
ID is an interned string in a particular Table.
IDs can be compared very cheaply. The zero value of ID always corresponds to the empty string.
Representation ¶
Interned strings are represented thus. If the high bit is cleared, then this is an index into the stored strings inside of the Table that created it.
Otherwise, it is up to five characters drawn from the LLVM char6 encoding, represented in-line using the bits of the ID.
NOTE: The 32-bit length is chosen because it is small. However, if we used a 64-bit ID this would allow us to inline 10-byte identifiers. We should investigate whether this results in improved memory usage overall.
func (ID) String ¶
String implements fmt.Stringer.
Note that this will not convert the ID back into a string; to do that, you must call Table.Value.
type Map ¶
Map is a map keyed by intern IDs.
type Set ¶
type Set map[ID]struct{}
Set is a set of intern IDs.
func (Set) ContainsID ¶
ContainsID returns whether s contains the given ID.
type Stats ¶
type Stats struct {
Hits int64 // Times [Table.Intern] returns a previously interned string.
Misses int64 // Times [Table.Intern] interns a new string.
Queries int64 // Times [Table.Query] or [Table.Intern] is called.
Inlined int64 // Times [Table.Query] or [Table.Intern] processes an inlined string.
AvgQuery float64 // Average length of queried strings.
AvgIntern float64 // Average length of interned strings (excludes inlined strings).
}
Stats are cache behavior statistics for a Table.
See Table.Stats.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is an interning table.
A table can be used to convert strings into [ID]s and back again.
The zero value of Table is empty and ready to use.
func (*Table) Intern ¶
Intern interns the given string into this table.
This function may be called by multiple goroutines concurrently.
func (*Table) InternBytes ¶
InternBytes interns the given byte string into this table.
This function may be called by multiple goroutines concurrently, but bytes must not be modified until this function returns.
func (*Table) Preload ¶
Preload takes a pointer to a struct type and initializes ID-typed fields with statically-specified strings.
Specifically, every exported field whose type is ID and which has a struct tag "intern" will be set to t.Intern(...) with that tag's value.
Panics if ids is not a pointer to a struct type.
func (*Table) Query ¶
Query will query whether s has already been interned.
If s has never been interned, returns false. This is useful for e.g. querying an intern-keyed map using a string: a failed query indicates that the string has never been seen before, so searching the map will be futile.
If s is small enough to be inlined in an ID, it is treated as always being interned.
func (*Table) QueryBytes ¶
QueryBytes will query whether bytes has already been interned.
This function may be called by multiple goroutines concurrently, but bytes must not be modified until this function returns.
func (*Table) RecordStats ¶
RecordStats sets whether this table records statistics on cache behavior.
Calling RecordStats(true) will reset any records set so far.
func (*Table) Stats ¶
Stats returns recorded statistics.
Panics if Table.RecordStats(true) has not been called.