intern

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package intern provides an interning table abstraction to optimize symbol resolution.

Index

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

func (id ID) GoString() string

GoString implements fmt.GoStringer.

func (ID) String

func (id ID) String() 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

type Map[T any] map[ID]T

Map is a map keyed by intern IDs.

func (Map[T]) Add

func (m Map[T]) Add(table *Table, key string, v T) (mapped T, inserted bool)

Add adds a string to m, and returns whether it was added.

func (Map[T]) AddID

func (m Map[T]) AddID(id ID, v T) (mapped T, inserted bool)

AddID adds an ID to m, and returns whether it was added.

func (Map[T]) Get

func (m Map[T]) Get(table *Table, key string) (T, bool)

Get returns the value that key maps to.

type Set

type Set map[ID]struct{}

Set is a set of intern IDs.

func (Set) Add

func (s Set) Add(table *Table, key string) (inserted bool)

Add adds a string to s, and returns whether it was added.

func (Set) AddID

func (s Set) AddID(id ID) (inserted bool)

AddID adds an ID to s, and returns whether it was added.

func (Set) Contains

func (s Set) Contains(table *Table, key string) bool

Contains returns whether s contains the given string.

func (Set) ContainsID

func (s Set) ContainsID(id ID) bool

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

func (t *Table) Intern(s string) ID

Intern interns the given string into this table.

This function may be called by multiple goroutines concurrently.

func (*Table) InternBytes

func (t *Table) InternBytes(bytes []byte) ID

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

func (t *Table) Preload(ids any)

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

func (t *Table) Query(s string) (ID, bool)

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

func (t *Table) QueryBytes(bytes []byte) (ID, bool)

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

func (t *Table) RecordStats(b bool)

RecordStats sets whether this table records statistics on cache behavior.

Calling RecordStats(true) will reset any records set so far.

func (*Table) Stats

func (t *Table) Stats() Stats

Stats returns recorded statistics.

Panics if Table.RecordStats(true) has not been called.

func (*Table) Value

func (t *Table) Value(id ID) string

Value converts an ID back into its corresponding string.

If id was created by a different Table, the results are unspecified, including potentially a panic.

This function may be called by multiple goroutines concurrently.

Jump to

Keyboard shortcuts

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