Created
May 15, 2022 13:06
-
-
Save harrison3000/c0fe0d9504d379be678b76300c905f15 to your computer and use it in GitHub Desktop.
Trivial generic arena alocator for go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arena | |
func ArenaAlloc[T any](n int) <-chan *T { | |
c := make(chan *T, n/2) | |
go func() { | |
for { | |
s := make([]T, n) | |
for k := range s { | |
c <- &s[k] | |
} | |
} | |
}() | |
return c | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arena | |
var intPtrMaker = ArenaAlloc[int](1024) | |
var arrMaker = ArenaAlloc[[8]int](1024) | |
func example() { | |
intPtr := <-intPtrMaker | |
slice := (<-arrMaker)[:0] | |
_, _ = intPtr, slice | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment