noGCMap

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

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 4 Imported by: 1

README

noGCMap

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

与fastcache bigcache等相比,无hash冲突问题,并且对键值对增长无限制,不会删除老旧键值对。

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

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

优点:

1)几乎零GC;

2)无hash碰撞问题(对比fastcache);

3)支持增删改( 相比 https://github.com/yudeguang/noGcStaticMap );

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

5)不限制键值对占用内存量(对比fastcache);

缺点: 1)性能比 https://github.com/yudeguang/noGcStaticMap 稍差,内存占用稍高;

package main

import (
	"github.com/yudeguang/noGcMap"
	"log"
)

func main() {
	log.SetFlags(log.Lshortfile | log.Ltime)
	m := noGCMap.New()
	m.SetString("1", "1原始")
	log.Println(m.GetString("1"))
	m.DeleteString("1")
	log.Println(m.GetString("1"))
	m.SetString("1","1修改")
	log.Println(m.GetString("1"))
	m.SetString("1","1修改2")
	log.Println(m.GetString("1"))
}

Documentation

Overview

Copyright 2020 rateLimit Author(https://github.com/yudeguang/noGCMap). 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/noGCMap.

Copyright 2020 rateLimit Author(https://github.com/yudeguang/noGCMap). 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/noGCMap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	//用于定义当删除的键值对占总键值对的百分比达到多少时,开始一轮真正的删除老旧键值对,并回收内存空间操作
	//该值介于1到100之间
	//默认为20
	NeedDeketeKVPairPersent int
	//用于定义chunkSize
	//最终的chunkSize为 MultiplesOf64KForchunkSize * 64 * 1024
	//该值需要介于1到1000之间(1==>64K,1000==>64M)
	//默认为1,即chunkSize=1 * 64 * 1024
	MultiplesOf64KForchunkSize int
}

type NoGcMapAny

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

func New

func New(config ...Config) *NoGcMapAny

初始化 config配置可选

func (*NoGcMapAny) Delete

func (n *NoGcMapAny) Delete(k []byte)

删除数据

func (*NoGcMapAny) DeleteString

func (n *NoGcMapAny) DeleteString(k string)

删除数据 以string的形式

func (*NoGcMapAny) Get

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

取出数据

func (*NoGcMapAny) GetString

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

取出数据,以string的方式

func (*NoGcMapAny) Len

func (n *NoGcMapAny) Len() int

返回长度,但不一定是绝对精确

func (*NoGcMapAny) Set

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

增加数据

func (*NoGcMapAny) SetString

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

增加数据 以string的形式

type NoGcMapInt

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

func NewInt

func NewInt(config ...Config) *NoGcMapInt

初始化 config配置可选

func (*NoGcMapInt) Delete

func (n *NoGcMapInt) Delete(k int)

删除数据

func (*NoGcMapInt) Get

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

取出数据

func (*NoGcMapInt) GetString

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

取出数据,以string的方式

func (*NoGcMapInt) Len

func (n *NoGcMapInt) Len() int

返回长度,但不一定是绝对精确

func (*NoGcMapInt) Set

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

增加数据

func (*NoGcMapInt) SetString

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

增加数据 以string的形式

Jump to

Keyboard shortcuts

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