-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to use zero value of Map/MapOf struct? #89
Comments
First of all, thanks for raising this issue. Such change would add one more atomic read ( As for your use case, you could implement a simple in-house wrapper struct and use it across your code base: type Map struct {
m *xsync.Map
initialized sync.Once
}
func (m *Map) init() {
m.initialized.Do(func() {
m.m = xsync.NewMap()
})
}
func (m *Map) Load(key string) (value interface{}, ok bool) {
m.init()
return m.m.Load(key)
}
func (m *Map) Store(key string, value interface{}) {
m.init()
m.m.Store(key, value)
}
// Other wrapped xsync.Map methods go here...
func main() {
var m Map
m.Store("foobar", 42)
v, _ := m.Load("foobar")
fmt.Println(v)
} According to Go memory model, |
thanks for giving attention. i'll probably just replace directive the package in the project where the performance matters that makes a lot of sense. If we want to reduce the overhead for those already using the New* functions, could doing something like the below possibly be a compromise? (making the comparison for existing users non atomic) It adds some complexity so I could understand any aversion
|
Yes, I guess that approach would do the job. |
ok, i put it here for now https://github.com/elee1766/xsync/blob/elee/map.go this seems to be okay. as for MapOf, making the zero value valid would mean being able to reflect out a hasher function i believe? since i dont think you can switch on a type assertion from the generic, i don't know if it's possible to preserve performance without resorting to unsafe. i believe that the package is currently getting some fancy compiler optimizations or something i dont completely understand out of the generic use in mapof which makes it so much faster. i tried to find out how go themselves picks the function, it looks like it's here? https://github.com/golang/go/blob/master/src/cmd/compile/internal/reflectdata/alg.go#L51 not too sure. |
Yes, |
hi,
followed over here from the golang issue.
was testing using the library as a drop in replacement for existing sync.Map and a generic sync.MapOf[K,V] wrapper that we use, but a big issue is that the zero value isn't valid, which would result in a lot of incompatible refactoring for our code. it worked great when i did initialize them.
ideally, something like this would not panic.
I have a naive solution that uses a sync.Once, happy to open a PR if this is something you would consider changing.
all i did was just just add a sync.Once
adding init function
changing NewMapPresized function
then i added init to these entrypoints:
and the same thing for the generic.
The text was updated successfully, but these errors were encountered: