On Mon, Mar 8, 2021 at 1:26 PM Axel Wagner <axel.wagner...@googlemail.com> wrote: > > On Mon, Mar 8, 2021 at 10:07 PM Ian Lance Taylor <i...@golang.org> wrote: >> >> > 2. When I need to iterate map. >> > Using standard reflect package there is no way to iterate map[string]int >> > without allocation of string header and int for every key-value pair. When >> > key or values is struct, things become worse. >> >> That is how the language works also. Map keys and values are not >> addressable. > > > I'm not sure I fully understand the issue, but > for k, v := range m > only needs to allocate storage for k/v *once*. AIUI, `MapIter.Key` and > `MapIter.Value` need to allocate on every iteration (or at least that's how I > read the comment by Yura). However, if MapIter's methods would have signatures > Key(v Value) > Value(v Value) > we could create *one* value of the key/value type. The methods would then > call `v.Set` to copy the non-addressable internal value into the addressable > iteration variables. This would be closer to the for loop - we'd still > (maybe) need to heap-allocate the iteration variables, but that would only > have to happen once per loop, not once per iteration.
That seems possible, and would help part of the problem. Thanks. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVTev9GwjoVc6OPq8%3D_QSjt%3Dp%2Bo%3DP%3D9UfD1zHsnVn9LLQ%40mail.gmail.com.