https://github.com/uptutu/bufp
buffer pool lib
https://github.com/uptutu/bufp
Last synced: over 1 year ago
JSON representation
buffer pool lib
- Host: GitHub
- URL: https://github.com/uptutu/bufp
- Owner: uptutu
- Created: 2021-09-10T11:10:30.000Z (almost 5 years ago)
- Default Branch: dev
- Last Pushed: 2021-09-14T06:09:49.000Z (almost 5 years ago)
- Last Synced: 2025-02-10T03:47:54.571Z (over 1 year ago)
- Language: Go
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bufp

[](https://github.com/uptutu/bufp)
[](https://pkg.go.dev/github.com/uptutu/bufp)
[](https://goreportcard.com/report/github.com/uptutu/bufp)
[](https://github.com/uptutu/bufp/actions)
[](https://coveralls.io/github/uptutu/bufp?branch=master)
💪 Useful utils Buffer Pool for decrease Go GC stress.
## Usage
The principle of this package is based on `sync.Pool`. A layer of `manager` is wrapped around it for buffer size
management.
### Install
```bash
$ go get github.com/uptutu/bufp
```
### Use
#### Manager
The manager is a `deque` at the Underlying logic , with each node managing a `sync.pool` of one size
package has a default global manager managing a 1kib Pool node.
```go
package main
import (
"github.com/uptutu/bufp/manager"
"sync"
)
func main() {
// Initialize pools of different sizes in Kib unit or Mib unit
// Of cares, you don't have to initialize
manager.InitKibPools(1, 2, 3, 4)
manager.InitMibPools(1, 2, 3, 4)
// get pool of the size you want from this manager
// _, ok := pool.(*sync.Pool); ok == true
size := manager.DefaultSize1KiB
pool, ok := manager.Get(size)
// here is 2 way to Set diff size pool to manager
p := &sync.Pool{New: func() interface{} { return &bufp.Buffer{bs: make([]byte, 0, 1023)} }}
manager.Set(1023, p)
// if *sync.Pool is nil then manager will accord your size new a Pool.
// this size will trim to 1024
manager.Set(1023, nil)
// ok == true
pool, ok := manager.Get(1024)
// this is a magic func.
// accord your passing on references size and trim it to find a suit size pool in manager to you
// if no suit pool this will return a nil pointer and false flag
// if you try multi times, manager will new this size pool for you
p, ok = manager.RightOne(sizeYouWant)
// this func serve you Service Logic in the lambda func
manager.Serve(len("content"), func(buf *bufp.Buffer) err {
buf.WriteString("content")
return nil
})
}
```
#### Buffer
```go
size := 1024
buffer := bufp.NewBuffer(size)
// append
buffer.AppebdByte(byte(oneByteUnit))
buffer.AppendString(string(s))
buffer.AppendInt(int64(10))
buffer.AppentTime(time.Now(), "2021-2-21 15:30:23")
buffer.AppendUint(uint64(10))
buffer.AppendBool(false)
buffer.AppendFloat(3.1415, 10)
buffer.Write([]byte("content"))
buffer.WriteByte('c')
buffer.WriteString("string")
buffer.TrimNewline()
// get buffer content
buffer.Len()
buffer.Cap()
buffer.Bytes()
buffer.String()
```
### Pool
this package has a default 1kib size buffer global pool named simplePool.
```go
size := 2014
// New: return *bufp.Buffer
pool := bufp.NewPool(size) // *sync.Pool
simplePool := bufp.Pool()
// this buffer default was 1kib size buffer
buffer := simplePool.Get()
// easy way get this default 1kib buffer
buffer := Get()
// easy way put buffer to simplePool
bufp.Put(buffer)
// you can change the default 1 kib buffer size by this func
bufp.Set(&sync.Pool{New:func (){return &bufp.Buffer{bs: make([]byte, 0, sizeYouWant)}}})
```
## License
[MIT](LICENSE)