Menu

📰
0

Why is go pooling worse than not trying to optimize anything?

Reddit r/golang·u/canadiancoding·about 1 month ago
#qVdDPEMS
#pointers#struct#pooling#worse#user#article
Reading 0:00
15s threshold

Why is go pooling worse than not trying to optimize anything? EDIT: After looking at the responses, the code basically just had some copy-paste coding errors that were giving the result. I've left the original contents in-tact for people interested below, but after fixing the issues and shifting things around I started getting results in-line with what I would expect. --- I'm building a caching layer and wanted to test go's struct pooling to make sure I understood it before I used it, and see if it was worth messing around with. I setup a little test that just counts the unique pointers: ```go package main import ( "fmt" "sync" ) type User struct { Name string Age int } type Set map[string]struct{} func AllocateNormally(n int) Set { res := make(Set) for range n { q := User{Name: "kieran", Age: 27} res[fmt.Sprintf("%p", &q)] = struct{}{} // Store value with an empty flag } return res } var userPool = sync.Pool{ New: func() any { return &User{} }, } func AllocateViaPool(n int) Set { res := make(Set) for range…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More