• Stars
    star
    109
  • Rank 307,990 (Top 7 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go Pooling Helpers

pool

GoDoc

Tiny memory reuse helpers for Go.

generic

Without use of subpackages, pool allows to reuse any struct distinguishable by size in generic way:

package main

import "github.com/gobwas/pool"

func main() {
	x, n := pool.Get(100) // Returns object with size 128 or nil.
	if x == nil {
		// Create x somehow with knowledge that n is 128.
	}
	defer pool.Put(x, n)
	
	// Work with x.
}

Pool allows you to pass specific options for constructing custom pool:

package main

import "github.com/gobwas/pool"

func main() {
	p := pool.Custom(
        pool.WithLogSizeMapping(),      // Will ceil size n passed to Get(n) to nearest power of two.
        pool.WithLogSizeRange(64, 512), // Will reuse objects in logarithmic range [64, 512].
        pool.WithSize(65536),           // Will reuse object with size 65536.
    )
	x, n := p.Get(1000)  // Returns nil and 1000 because mapped size 1000 => 1024 is not reusing by the pool.
    defer pool.Put(x, n) // Will not reuse x.
	
	// Work with x.
}

Note that there are few non-generic pooling implementations inside subpackages.

pbytes

Subpackage pbytes is intended for []byte reuse.

package main

import "github.com/gobwas/pool/pbytes"

func main() {
	bts := pbytes.GetCap(100) // Returns make([]byte, 0, 128).
	defer pbytes.Put(bts)

	// Work with bts.
}

You can also create your own range for pooling:

package main

import "github.com/gobwas/pool/pbytes"

func main() {
	// Reuse only slices whose capacity is 128, 256, 512 or 1024.
	pool := pbytes.New(128, 1024) 

	bts := pool.GetCap(100) // Returns make([]byte, 0, 128).
	defer pool.Put(bts)

	// Work with bts.
}

pbufio

Subpackage pbufio is intended for *bufio.{Reader, Writer} reuse.

package main

import "github.com/gobwas/pool/pbufio"

func main() {
	bw := pbufio.GetWriter(os.Stdout, 100) // Returns bufio.NewWriterSize(128).
	defer pbufio.PutWriter(bw)

	// Work with bw.
}

Like with pbytes, you can also create pool with custom reuse bounds.

More Repositories

1

ws

Tiny WebSocket library for Go.
Go
5,816
star
2

glob

Go glob
Go
893
star
3

ws-examples

Examples of using github.com/gobwas/ws
Go
198
star
4

gtrace

A code generation tool for instrumenting Go components.
Go
125
star
5

dm.js

Javascript Dependency Injection Manager
JavaScript
109
star
6

graceful

A library for graceful restarts in Go.
Go
90
star
7

httphead

Go
81
star
8

influent.rs

InfluxDB Rust driver
Rust
44
star
9

gulp-sprite-generator

Plugin that generate sprites from your stylesheets.
JavaScript
42
star
10

influent

InfluxDB Javascript driver
JavaScript
38
star
11

flagutil

Easy flags population.
Go
34
star
12

hashring

Consistent hashing hashring implementation.
Go
32
star
13

gws

Go Web Socket
Go
31
star
14

jQuery.buttonCaptcha

Plugin that protects sites from robots using jQuery.
JavaScript
16
star
15

xsync

Priortizable and cancellable synchronization primitives in Go.
Go
14
star
16

cli

Tiny and minimalistic CLI library for Go
Go
14
star
17

schinquirer

schinquirer
JavaScript
8
star
18

jQuery-Loading

Draws animations via DOM elements, with ability to push your own effects&algorithms for animations
JavaScript
8
star
19

vk

A tiny vk.com API library written in Go
Go
6
star
20

ppgo

Experimental templates library for golang
Go
6
star
21

radix

Go
5
star
22

deadline

Simple deadliner in Go.
Go
5
star
23

grunt-marked

Plugin that compiles markdown files into html, using marked parser
JavaScript
5
star
24

.dotfiles

Shell
4
star
25

ps1ws

Golang Talk Slides @ MailRu, April 2017
Go
4
star
26

wrkp

wrk tool report parser
Go
3
star
27

atomicfloat

Atomic float utils
Go
3
star
28

prompt

Ascetic and minimalistic CLI prompt library in Go
Go
3
star
29

json-honey

Yet another super sweet json stringifier-prettyfier-sortifier-sweetifier
JavaScript
3
star
30

avl

Go
3
star
31

jQuery.slideBanjo

Plugin that shows pretty slides at your site.
JavaScript
3
star
32

inherits.js

Backbone's extend inspired standalone inheritance tool.
JavaScript
2
star
33

goproxyd

Go modules local cache server.
Go
2
star
34

articles

2
star
35

xtt

Go
2
star
36

validator.js

ValidatorJS
JavaScript
2
star
37

gracefultalk

Go
2
star
38

tcprpc.rs

Test
Rust
2
star
39

white.vim

Drained of its colours vim color scheme.
Vim Script
1
star
40

mk52

Calculus
Go
1
star
41

kursobot

Go
1
star
42

rk

Rabin-Karp implementation.
Go
1
star
43

QuerySniffer

QuerySniffer – is a command line tool for easy sniffing database queries.
PHP
1
star
44

tcprpc.go

Go
1
star
45

json-compile

Compilation of references in json.
JavaScript
1
star
46

tcprpc.js

Test TCPRPC
JavaScript
1
star
47

hyper_rust_example

Rust
1
star
48

lexicon.js

JavaScript
1
star
49

array

Golang Sorted Immutable Array
Go
1
star
50

rbtree

Go
1
star
51

safely

Tool, that creates filenames for safe rewriting of target file
JavaScript
1
star
52

gulp-safe

Simple gulp plugin for saving backuped existing files.
JavaScript
1
star
53

flagvar

Collection of useful flag.Value implementations.
Go
1
star
54

stream-branch

Node stream branch tool
JavaScript
1
star
55

jquery-megamask

Simple mask plugin for jquery.
JavaScript
1
star
56

Enum.js

Javascript Enum object.
JavaScript
1
star