slice_map

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2021 License: MIT Imports: 0 Imported by: 0

README

slice_map

The slice_map is designed to impove the performance for the case that you need to hold a large map in memory. It uses a slice and an extra index map to replace the go-standard map such as map[int]interface for better add/iter performance.

If you have a large map (more than 500k keys and use complex struct as map value), slice_map saves lots of cpu time when every GC excuting.

Adding/deleting elements:

type OBJ struct {
	// ...
    int id
}

// Need to implement a Id interface.
func (o *Obj) LMapId() int {
	return o.id
}

lm := NewLMap()
newObj := OBJ{ id: 1024 }
lm.Add()
lm.Del(newObj.id)
lm.Len()

Iter the slice_map: When use FastIter, you can't delete keys in the map. And get 10 times faster than Iter which you can delete keys safely during the iterting.

lm.FastIter(func(tmp LMapObj) {
	obj, ok := tmp.(*OBJ)
	...
})

lm.Iter(func(tmp LMapObj) {
	obj, ok := tmp.(*OBJ)
	...
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LMap

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

func NewLMap

func NewLMap() *LMap

func (*LMap) Add

func (lmap *LMap) Add(obj LMapObj)

func (*LMap) Del

func (lmap *LMap) Del(id TKey)

func (*LMap) FastIter

func (lmap *LMap) FastIter(f func(LMapObj))

Iterting map, as you must not delete any element in user-specified function.

func (*LMap) Get

func (lmap *LMap) Get(id TKey) LMapObj

func (*LMap) Iter

func (lmap *LMap) Iter(f func(LMapObj))

Iterating map with the ability of deleting element. But some element would not be travelled, if you deleted keys.

func (*LMap) Len

func (lmap *LMap) Len() int

func (*LMap) Shrink

func (lmap *LMap) Shrink()

type LMapObj

type LMapObj interface {
	LMapId() TKey
}

type TKey added in v0.0.2

type TKey uint

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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