map_of_shame

package module
v0.0.0-...-4555243 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

map-of-shame

⚠️ EXPERIMENTAL - DO NOT USE IN REAL SYSTEMS ⚠️

map-of-shame provides direct access to Go's internal map implementation through unsafe pointers.

Some code needs to perform operations on maps whose type is not known at compile time. Normally, this is done using reflection, which enforces various safety guarantees and prevents consumers from violating expectations of the runtime. However, this requires causes a large performance overhead - mainly by forcing most operations to allocate memory. The only alternative to this is to break the encapsulation of the runtime and expose internal functions and reimplement private types.

This library provides a thin wrapper around these exposed operations, so that consumers can merely focus on writing unsafe code.

You should not use this library. It serves a niche purpose that you do not have. If you do have it, you should write the subset of it that you need yourself. If you think you'll do a worse job than me, I can't stop you.

Why is this shameful?

If you read code for the Go runtime, especially code related to maps, you will find comments referencing libraries as members of the "hall of shame". These libraries exposed internal functions of the runtime for this purpose, so that they could write efficient code. The Go maintainers, not wanting these libraries to break, have had to quietly mark these methods as an unofficial backdoor API and maintain them in perpetuity. The whole situation is messy, and I find it easy to sympathize with people on both sides.

However, if many people repeatedly write the same code to break into the runtime and expose functionality because it's the best way to write efficient code, it seems obvious to me that there should be an officially-supported way to do this. This library exists mainly as a rough-draft proposal for an API extension for the unsafe package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator provides iteration over map key-value pairs.

func (*Iterator) InitAny

func (i *Iterator) InitAny(m any)

InitAny initializes the iterator for any map value. It panics if m is not a map.

func (*Iterator) InitMap

func (i *Iterator) InitMap(m *Map)

InitMap initializes the iterator for a Map.

func (*Iterator) InitReflectTypePtr

func (i *Iterator) InitReflectTypePtr(mapType reflect.Type, m unsafe.Pointer)

InitReflectTypePtr initializes the iterator for a map given its type and pointer. It panics if mapType is not a map type.

func (*Iterator) Key

func (i *Iterator) Key() unsafe.Pointer

Key returns a pointer to the current key.

func (*Iterator) Next

func (i *Iterator) Next() bool

Next advances the iterator to the next key-value pair and reports whether there is a pair available.

func (*Iterator) Reset

func (i *Iterator) Reset()

Reset resets the iterator to its zero state.

func (*Iterator) Value

func (i *Iterator) Value() unsafe.Pointer

Value returns a pointer to the current value.

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map provides map operations for an unsafe.Pointer to a map.

func NewFromAny

func NewFromAny(m any) Map

NewFromAny creates a Map from any map value. It panics if m is not a map.

func NewFromTypeAndPointer

func NewFromTypeAndPointer(mapType reflect.Type, m unsafe.Pointer) Map

NewFromTypeAndPointer creates a Map from a map type and unsafe.Pointer to the map. It panics if mapType is not a map type.

func NewFromValue

func NewFromValue(m reflect.Value) Map

NewFromValue creates a Map from a reflect.Value. It panics if m is not a map.

func (*Map) All

func (m *Map) All(yield func(k, v unsafe.Pointer) bool)

All iterates over all key-value pairs in the map.

func (*Map) Clear

func (m *Map) Clear()

Clear removes all elements from the map.

func (*Map) Delete

func (m *Map) Delete(key unsafe.Pointer)

Delete deletes key from the map.

func (*Map) Get

func (m *Map) Get(key unsafe.Pointer) unsafe.Pointer

Get returns a pointer to the value associated with key, or nil if the key is not present. The returned pointer points directly into the map and is only valid until the map is modified. Retaining this pointer may prevent the whole map from being garbage-collected.

func (*Map) Iter

func (m *Map) Iter() Iterator

Iter returns an Iterator over the map.

func (*Map) Keys

func (m *Map) Keys(yield func(k unsafe.Pointer) bool)

Keys iterates over all keys in the map.

func (*Map) Len

func (m *Map) Len() int

Len returns the number of elements in the map.

func (*Map) Set

func (m *Map) Set(key, value unsafe.Pointer)

Set sets the value for key in the map.

func (*Map) Values

func (m *Map) Values(yield func(v unsafe.Pointer) bool)

Values iterates over all values in the map.

Jump to

Keyboard shortcuts

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