The map still iterates through the keys if it's a string.
You could expect sizes though, but you should still use `map[*[]byte]`.
Andrew
On Wednesday, March 29, 2023 at 1:51:04 PM UTC-5 Amit Lavon wrote:
> Thank you! Now it makes sense to me.
>
> On Monday, March 27, 2023 at 5:50:02 PM UTC+3 Keith Randall wrote:
>
>> Key sizes 4 and 8 have special case hashing code.
>> They are intended for int32, int64, and pointers, but your [4]byte and
>> [8]byte take advantage of it as well.
>>
>> On Sunday, March 26, 2023 at 3:05:52 AM UTC-7 Amit Lavon wrote:
>>
>>> Hi gophers,
>>>
>>> Some code I am writing uses byte-arrays ([X]byte) as keys in a map. I
>>> benchmarked the performance of map operations using different X's and found
>>> that 4 and 8 are about twice as fast compared to 5, 6, 7 (see below).
>>>
>>> Can someone explain this phenomenon?
>>> I'd like to learn about it so I can take it in consideration when
>>> choosing key types.
>>>
>>>
>>> Code:
>>>
>>> type byteArray interface {
>>> [4]byte | [5]byte | [6]byte | [7]byte | [8]byte
>>> }
>>>
>>> func benchmarkMaps[T byteArray](b *testing.B) {
>>> m := map[T]int{}
>>> var t T
>>> for i := 0; i < b.N; i++ {
>>> m[t]++
>>> }
>>> }
>>>
>>> func BenchmarkMaps(b *testing.B) {
>>> b.Run("4", benchmarkMaps[[4]byte])
>>> b.Run("5", benchmarkMaps[[5]byte])
>>> b.Run("6", benchmarkMaps[[6]byte])
>>> b.Run("7", benchmarkMaps[[7]byte])
>>> b.Run("8", benchmarkMaps[[8]byte])
>>> }
>>>
>>> Results:
>>>
>>> 4: 17.01 ns/op
>>> 5: 36.02 ns/op
>>> 6: 38.24 ns/op
>>> 7: 29.92 ns/op
>>> 8: 16.58 ns/op
>>>
>>> go1.20
>>>
>>
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/2f80fcf6-2341-4c86-818b-f5f86d9ade58n%40googlegroups.com.