noGcStaticMap

package module
v0.0.0-...-9b01147 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 15 Imported by: ? 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.

README

noGcStaticMap

https://github.com/yudeguang/noGcMap 与 https://github.com/yudeguang/noGcStaticMap 为同一系列的无GC类型MAP,两者针对的场景有一定差异,noGcStaticMap性能稍高,内存占用更小,但不支持增删改。

对于大型map,比如总数达到千万级别的map,如果键或者值中包含引用类型(string类型,结构体类型,或者任何基本类型+指针的定义 *int, *float 等),那么这个map在垃圾回收的时候就会非常慢,GC的周期回收时间可以达到秒级甚至分钟级。

对此参考fastcache等,把复杂的不利于GC的复杂map转化为基础类型的map map[uint64]uint32 用于存储索引 和 []byte用于存储实际键值。如此改造之后,基本上实现了零GC,总体而言:

优点:

1)几乎零GC;

2)无hash碰撞问题;

3)内存占用相对较小;

4)提供GetUnsafe,GetValFromDataBeginPosOfKVPairUnSafe等函数以满足高性能场景的要求(不复制内容,直接取值);

5)代码量非常少,适合根据自己需求做二次修改;

缺点:

1)为纯静态map,不能动态新增或删除键值对,即在键值加载完成之前,只允许新增;在键值对加载完成后,则只允许查询;

注意:

对于一些结构体类型,把结构体与[]byte的相互转换,可能会用到convert_help.go 文件中的StructToStr,SliceToStr以及BytesToStruct,BytesToSlice等函数,这些函数需要自己复制后改写实现。

package main

import (
	"github.com/yudeguang/noGcStaticMap"
	"log"
	"strconv"
)
//声明成全局变量
var m1 = noGcStaticMap.NewDefault()
var m2 = noGcStaticMap.NewInt()

func main() {
	log.SetFlags(log.Lshortfile | log.Ltime)
	tAny()
	tInt()
}

func tAny() {
	log.Println("开始")

	//增加
	m1.Set([]byte(""), []byte("键为空的值"))               //键为空
	m1.Set([]byte(strconv.Itoa(1000000)), []byte("")) //值为空
	for i := 0; i < 1000; i++ {
		m1.Set([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
	}
	//加载完成 加载完成后不允许再加载 未加载完成前,不允许查询
	m1.SetFinished()
	//查询键为空
	val, exist := m1.GetString("")
	log.Println("key:", "", "值:", val, exist)
	//查询键为空
	val, exist = m1.GetString(strconv.Itoa(1000000))
	log.Println("key:", 1000000, "值:", val, exist)
	for i := 0; i < 10; i++ {
		val, exist = m1.GetString(strconv.Itoa(i))
		log.Println("key:", i, "值:", val, exist)
	}
	log.Println("完成查询")
}
func tInt() {
	log.Println("开始")

	m2.Set(1000000, []byte("")) //值为空
	for i := 0; i < 1000; i++ {
		m2.Set(i, []byte(strconv.Itoa(i)))
	}
	//加载完成 加载完成后不允许再加载 未加载完成前,不允许查询
	m2.SetFinished()
	//查询空值
	val, exist := m2.GetString(1000000)
	log.Println("key:", 1000000, "值:", val, exist)
	//查询普通值
	for i := 0; i < 10; i++ {
		val, exist := m2.GetString(i)
		log.Println("key:", i, "值:", val, exist)
	}
	log.Println("完成查询")
}

Documentation

Overview

Copyright 2022 rateLimit Author(https://github.com/yudeguang/noGcStaticMap). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/noGcStaticMap.

Copyright 2022 rateLimit Author(https://github.com/yudeguang/noGcStaticMap). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/noGcStaticMap.

Copyright 2022 rateLimit Author(https://github.com/yudeguang/noGcStaticMap). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/noGcStaticMap.

Copyright 2022 rateLimit Author(https://github.com/yudeguang/noGcStaticMap). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/noGcStaticMap.

Copyright 2022 rateLimit Author(https://github.com/yudeguang/noGcStaticMap). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/noGcStaticMap.

Index

Constants

This section is empty.

Variables

View Source
var SplitSep = []byte("`")

默认的文本分隔符,一般文本中不会有这个字符

Functions

func JoinInterface

func JoinInterface(sep string, elems ...interface{}) string

func SliceToStr

func SliceToStr(p []NoGcStructExample) string

把结构体切片转换成字符串

func StructToStr

func StructToStr(p NoGcStructExample) string

把结构体转换成字符串

Types

type NoGcStaticMapAny

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

func NewDefault

func NewDefault(tempFileName ...string) *NoGcStaticMapAny

初始化 默认类型,键值的最大长度为65535

func (*NoGcStaticMapAny) Get

func (n *NoGcStaticMapAny) Get(k []byte) (v []byte, exist bool)

取出数据

func (*NoGcStaticMapAny) GetDataBeginPosOfKVPair

func (n *NoGcStaticMapAny) GetDataBeginPosOfKVPair(k []byte) (uint32, bool)

取出键值对在数据中存储的开始位置

func (*NoGcStaticMapAny) GetString

func (n *NoGcStaticMapAny) GetString(k string) (v string, exist bool)

取出数据,以string的方式

func (*NoGcStaticMapAny) GetUnsafe

func (n *NoGcStaticMapAny) GetUnsafe(k []byte) (v []byte, exist bool)

取出数据 警告:返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapAny) GetValFromDataBeginPosOfKVPairUnSafe

func (n *NoGcStaticMapAny) GetValFromDataBeginPosOfKVPairUnSafe(dataBeginPos int) (v []byte)

从内存中的某个位置取出键值对中值的数据 警告: 1)传入的dataBeginPos必须是真实有效的,否则有可能会数据越界; 2)返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapAny) Len

func (n *NoGcStaticMapAny) Len() int

返回键值对个数

func (*NoGcStaticMapAny) Set

func (n *NoGcStaticMapAny) Set(k, v []byte)

增加数据

func (*NoGcStaticMapAny) SetFinished

func (n *NoGcStaticMapAny) SetFinished()

完成存储把存储到硬盘上的文件复制到内存

func (*NoGcStaticMapAny) SetString

func (n *NoGcStaticMapAny) SetString(k, v string)

增加数据,以string的方式

type NoGcStaticMapHuge

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

其它类型,值最长为65535,此类型无此限制

func NewHuge

func NewHuge(tempFileName ...string) *NoGcStaticMapHuge

初始化 对键值的长度不做限制,除非是存储值的长度超长的情况,否则不建议使用此类型,因为会占用更多的空间

func (*NoGcStaticMapHuge) Get

func (n *NoGcStaticMapHuge) Get(k []byte) (v []byte, exist bool)

取出数据

func (*NoGcStaticMapHuge) GetDataBeginPosOfKVPair

func (n *NoGcStaticMapHuge) GetDataBeginPosOfKVPair(k []byte) (uint32, bool)

取出键值对在数据中存储的开始位置

func (*NoGcStaticMapHuge) GetString

func (n *NoGcStaticMapHuge) GetString(k string) (v string, exist bool)

取出数据,以string的方式

func (*NoGcStaticMapHuge) GetUnsafe

func (n *NoGcStaticMapHuge) GetUnsafe(k []byte) (v []byte, exist bool)

取出数据 警告:返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapHuge) GetValFromDataBeginPosOfKVPairUnSafe

func (n *NoGcStaticMapHuge) GetValFromDataBeginPosOfKVPairUnSafe(dataBeginPos int) (v []byte)

从内存中的某个位置取出键值对中值的数据 警告: 1)传入的dataBeginPos必须是真实有效的,否则有可能会数据越界; 2)返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapHuge) Len

func (n *NoGcStaticMapHuge) Len() int

返回键值对个数

func (*NoGcStaticMapHuge) Set

func (n *NoGcStaticMapHuge) Set(k, v []byte)

增加数据

func (*NoGcStaticMapHuge) SetFinished

func (n *NoGcStaticMapHuge) SetFinished()

完成存储把存储到硬盘上的文件复制到内存

func (*NoGcStaticMapHuge) SetString

func (n *NoGcStaticMapHuge) SetString(k, v string)

增加数据,以string的方式

type NoGcStaticMapInt

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

func NewInt

func NewInt(tempFileName ...string) *NoGcStaticMapInt

初始化 键的类型为int,值的最大长度为65535,与默认类型相比,速度稍快,稍微节省存储空间

func (*NoGcStaticMapInt) Get

func (n *NoGcStaticMapInt) Get(k int) (v []byte, exist bool)

取出数据

func (*NoGcStaticMapInt) GetDataBeginPosOfKVPair

func (n *NoGcStaticMapInt) GetDataBeginPosOfKVPair(k int) (uint32, bool)

取出键值对在数据中存储的开始位置

func (*NoGcStaticMapInt) GetString

func (n *NoGcStaticMapInt) GetString(k int) (v string, exist bool)

取出数据,以string的方式

func (*NoGcStaticMapInt) GetUnsafe

func (n *NoGcStaticMapInt) GetUnsafe(k int) (v []byte, exist bool)

取出数据 警告:返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值s

func (*NoGcStaticMapInt) GetValFromDataBeginPosOfKVPairUnSafe

func (n *NoGcStaticMapInt) GetValFromDataBeginPosOfKVPairUnSafe(dataBeginPos int) (v []byte)

从内存中的某个位置取出键值对中值的数据 警告: 1)传入的dataBeginPos必须是真实有效的,否则有可能会数据越界; 2)返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapInt) Len

func (n *NoGcStaticMapInt) Len() int

返回键值对个数

func (*NoGcStaticMapInt) Set

func (n *NoGcStaticMapInt) Set(k int, v []byte)

增加数据

func (*NoGcStaticMapInt) SetFinished

func (n *NoGcStaticMapInt) SetFinished()

完成存储把存储到硬盘上的文件复制到内存

func (*NoGcStaticMapInt) SetString

func (n *NoGcStaticMapInt) SetString(k int, v string)

增加数据,以string的方式

type NoGcStaticMapUint32

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

func NewUint32

func NewUint32(tempFileName ...string) *NoGcStaticMapUint32

初始化 键的类型为int32,值的最大长度为65535,与默认类型相比,速度稍快,稍微节省存储空间

func (*NoGcStaticMapUint32) Get

func (n *NoGcStaticMapUint32) Get(k uint32) (v []byte, exist bool)

取出数据

func (*NoGcStaticMapUint32) GetDataBeginPosOfKVPair

func (n *NoGcStaticMapUint32) GetDataBeginPosOfKVPair(k uint32) (uint32, bool)

取出键值对在数据中存储的开始位置

func (*NoGcStaticMapUint32) GetString

func (n *NoGcStaticMapUint32) GetString(k uint32) (v string, exist bool)

取出数据,以string的方式

func (*NoGcStaticMapUint32) GetUnsafe

func (n *NoGcStaticMapUint32) GetUnsafe(k uint32) (v []byte, exist bool)

取出数据 警告:返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapUint32) GetValFromDataBeginPosOfKVPairUnSafe

func (n *NoGcStaticMapUint32) GetValFromDataBeginPosOfKVPairUnSafe(dataBeginPos int) (v []byte)

从内存中的某个位置取出键值对中值的数据 警告: 1)传入的dataBeginPos必须是真实有效的,否则有可能会数据越界; 2)返回的数据是hash表中值的引用,而非值的复制品,要注意不要在外部改变该返回值

func (*NoGcStaticMapUint32) Len

func (n *NoGcStaticMapUint32) Len() int

返回键值对个数

func (*NoGcStaticMapUint32) Set

func (n *NoGcStaticMapUint32) Set(k uint32, v []byte)

增加数据

func (*NoGcStaticMapUint32) SetFinished

func (n *NoGcStaticMapUint32) SetFinished()

完成存储把存储到硬盘上的文件复制到内存

func (*NoGcStaticMapUint32) SetString

func (n *NoGcStaticMapUint32) SetString(k uint32, v string)

增加数据,以string的方式

type NoGcStructExample

type NoGcStructExample struct {
	Col1 int
	Col2 string
	Col3 string
	Col4 string
}

结构体案例

func BytesToSlice

func BytesToSlice(data []byte) (ps []NoGcStructExample)

数据转换为切片

func BytesToStruct

func BytesToStruct(data []byte) (p NoGcStructExample)

高效的对数据进行分割,相比于用系统的Split减少gc的产生,速度更快

Jump to

Keyboard shortcuts

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