Coder Social home page Coder Social logo

practice-bump-allocator's Introduction

Practice Bump Allocator

โš ๏ธ This library is not supported and should not be used as-is for any purpose

This is my first attempt at building a bump / pool / arena allocator in Go 1.16. There were no real goals in mind at the beginning other than to see if I...

  • Could do it
  • Make it faster
  • Make it as safe as possible while retaining maintainability

Unfortunately in my investigation of building this, I found that unless you accessed reflect data directly and unsafely, you would pay heavy allocation and speed costs and so I put together a small subset of the reflect package so I could do this, with tests that check my implementation against the reflect package to ensure parity. I've found if you don't do something like this, your bump allocator will end up allocating anyway via the reflect package and you gain nothing, but I didn't dive too deep, so I'd be happy to be wrong about this.

Results from testing

$ go test ./... -count=1 -bench=.
goos: windows
goarch: amd64
pkg: github.com/silbinarywolf/practice-bump-allocator
cpu: AMD Ryzen 5 3600 6-Core Processor
BenchmarkNativeAlloc1000-12       613857              1879 ns/op            3200 B/op        100 allocs/op
BenchmarkAlloc1000-12            2159748               555.5 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/silbinarywolf/practice-bump-allocator        2.997s
PASS
ok      github.com/silbinarywolf/practice-bump-allocator/internal/inlinetest    0.123s
PASS
ok      github.com/silbinarywolf/practice-bump-allocator/internal/reflectlite   0.062s

For readability convenience, relevant code copy-pasted out of bump_test.go

type Player struct {
	X, Y          int
	Width, Height int
}

// BenchmarkNativeAlloc is here so we can compare native Go
// allocations to our bump allocator
func BenchmarkNativeAlloc1000(b *testing.B) {
	b.ReportAllocs()

	var p1 *Player

	for n := 0; n < b.N; n++ {
		for i := 0; i < 100; i++ {
			p1 = &Player{}
		}
	}

	_ = p1
}

func BenchmarkAlloc1000(b *testing.B) {
	b.ReportAllocs()

	var p1 *Player

	alloc := New(make([]byte, 32678))
	for n := 0; n < b.N; n++ {
		for i := 0; i < 100; i++ {
			p1 = alloc.New(&Player{}).(*Player)
		}
		alloc.Reset()
	}

	_ = p1
}

practice-bump-allocator's People

Contributors

silbinarywolf 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.