diff options
| author | 2021-08-25 15:34:33 +0200 | |
|---|---|---|
| committer | 2021-08-25 15:34:33 +0200 | |
| commit | 2dc9fc1626507bb54417fc4a1920b847cafb27a2 (patch) | |
| tree | 4ddeac479b923db38090aac8bd9209f3646851c1 /vendor/github.com/vmihailenco/bufpool/README.md | |
| parent | Manually approves followers (#146) (diff) | |
| download | gotosocial-2dc9fc1626507bb54417fc4a1920b847cafb27a2.tar.xz | |
Pg to bun (#148)
* start moving to bun
* changing more stuff
* more
* and yet more
* tests passing
* seems stable now
* more big changes
* small fix
* little fixes
Diffstat (limited to 'vendor/github.com/vmihailenco/bufpool/README.md')
| -rw-r--r-- | vendor/github.com/vmihailenco/bufpool/README.md | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/vendor/github.com/vmihailenco/bufpool/README.md b/vendor/github.com/vmihailenco/bufpool/README.md deleted file mode 100644 index 05a70791c..000000000 --- a/vendor/github.com/vmihailenco/bufpool/README.md +++ /dev/null @@ -1,74 +0,0 @@ -# bufpool - -[](https://travis-ci.org/vmihailenco/bufpool) -[](https://godoc.org/github.com/vmihailenco/bufpool) - -bufpool is an implementation of a pool of byte buffers with anti-memory-waste protection. It is based on the code and ideas from these 2 projects: -- https://github.com/libp2p/go-buffer-pool -- https://github.com/valyala/bytebufferpool - -bufpool consists of global pool of buffers that have a capacity of a power of 2 starting from 64 bytes to 32 megabytes. It also provides individual pools that maintain usage stats to provide buffers of the size that satisfies 95% of the calls. Global pool is used to reuse buffers between different parts of the app. - -# Installation - -``` go -go get github.com/vmihailenco/bufpool -``` - -# Usage - -bufpool can be used as a replacement for `sync.Pool`: - -``` go -var jsonPool bufpool.Pool // basically sync.Pool with usage stats - -func writeJSON(w io.Writer, obj interface{}) error { - buf := jsonPool.Get() - defer jsonPool.Put(buf) - - if err := json.NewEncoder(buf).Encode(obj); err != nil { - return err - } - - _, err := w.Write(buf.Bytes()) - return err -} -``` - -or to allocate buffer of the given size: - -``` go -func writeHex(w io.Writer, data []byte) error { - n := hex.EncodedLen(len(data))) - - buf := bufpool.Get(n) // buf.Len() is guaranteed to equal n - defer bufpool.Put(buf) - - tmp := buf.Bytes() - hex.Encode(tmp, data) - - _, err := w.Write(tmp) - return err -} -``` - -If you need to append data to the buffer you can use following pattern: - -``` go -buf := bufpool.Get(n) -defer bufpool.Put(buf) - -bb := buf.Bytes()[:0] - -bb = append(bb, ...) - -buf.ResetBuf(bb) -``` - -You can also change default pool thresholds: - -``` go -var jsonPool = bufpool.Pool{ - ServePctile: 0.95, // serve p95 buffers -} -``` |
