Coder Social home page Coder Social logo

slab's Introduction

Introduction

Go Report Card Build Status codecov GoDoc

Slab allocation memory pools for Go.

Usage

Use lock-free memory pool:

pool := slab.NewAtomPool(
	64,          // The smallest chunk size is 64B.
	64 * 1024,   // The largest chunk size is 64KB.
	2,           // Power of 2 growth in chunk size.
	1024 * 1024, // Each slab will be 1MB in size.
)

buf1 := pool.Alloc(64)

    ... use the buf ...
	
pool.Free(buf)

Use chan based memory pool:

pool := slab.NewChanPool(
	64,          // The smallest chunk size is 64B.
	64 * 1024,   // The largest chunk size is 64KB.
	2,           // Power of 2 growth in chunk size.
	1024 * 1024, // Each slab will be 1MB in size.
)

buf1 := pool.Alloc(64)

    ... use the buf ...
	
pool.Free(buf)

Use sync.Pool based memory pool:

pool := slab.NewSyncPool(
	64,          // The smallest chunk size is 64B.
	64 * 1024,   // The largest chunk size is 64KB.
	2,           // Power of 2 growth in chunk size.
)

buf := pool.Alloc(64)

    ... use the buf ...
	
pool.Free(buf)

Performance

Result of GOMAXPROCS=16 go test -v -bench=. -benchmem:

Benchmark_AtomPool_AllocAndFree_128-8   	20000000	       104 ns/op	       0 B/op	       0 allocs/op
Benchmark_AtomPool_AllocAndFree_256-8   	20000000	       106 ns/op	       0 B/op	       0 allocs/op
Benchmark_AtomPool_AllocAndFree_512-8   	20000000	       109 ns/op	       0 B/op	       0 allocs/op

Benchmark_ChanPool_AllocAndFree_128-8   	 5000000	       246 ns/op	       0 B/op	       0 allocs/op
Benchmark_ChanPool_AllocAndFree_256-8   	 5000000	       221 ns/op	       0 B/op	       0 allocs/op
Benchmark_ChanPool_AllocAndFree_512-8   	 5000000	       304 ns/op	       0 B/op	       0 allocs/op

Benchmark_LockPool_AllocAndFree_128-8   	 5000000	       275 ns/op	       0 B/op	       0 allocs/op
Benchmark_LockPool_AllocAndFree_256-8   	 5000000	       265 ns/op	       0 B/op	       0 allocs/op
Benchmark_LockPool_AllocAndFree_512-8   	 5000000	       280 ns/op	       0 B/op	       0 allocs/op

Benchmark_SyncPool_AllocAndFree_128-8   	100000000	        22.9 ns/op	      32 B/op	       1 allocs/op
Benchmark_SyncPool_AllocAndFree_256-8   	100000000	        23.4 ns/op	      32 B/op	       1 allocs/op
Benchmark_SyncPool_AllocAndFree_512-8   	50000000	        23.3 ns/op	      32 B/op	       1 allocs/op

Benchmark_SyncPool_CacheMiss_128-8      	10000000	       196 ns/op	     160 B/op	       2 allocs/op
Benchmark_SyncPool_CacheMiss_256-8      	 5000000	       228 ns/op	     288 B/op	       2 allocs/op
Benchmark_SyncPool_CacheMiss_512-8      	10000000	       251 ns/op	     544 B/op	       2 allocs/op

Benchmark_Make_128-8                    	100000000	        17.9 ns/op	     128 B/op	       1 allocs/op
Benchmark_Make_256-8                    	50000000	        31.7 ns/op	     256 B/op	       1 allocs/op
Benchmark_Make_512-8                    	20000000	        63.6 ns/op	     512 B/op	       1 allocs/op

slab's People

Contributors

bg5sbk avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.