Why do the buckets of a map store the 8 key/values in the same byte array?

I saw in the code that the bucket stores, in the same byte array, both the keys and the values. And it does something like this, for example if only 2 key value pairs are present: Key1, Key2, Emtpy, … Empty, Value1, Value2, Empty, … Empty.

This storing in the same byte array is done because of address alignment guarantees. I understand that if it were to store key1, value1, key2, value2,… then there could be some padding, between each key&value, due to address alignment guarantees.

However, why does it not simply have a byte array for the 8 keys and another one for the 8 values ? There will no longer be a possible padding between the key section and values section.

Why was this single array version been chosen ? Does anyone have any idea ?

This might be for performance. By keeping keys and values contiguous in memory they may fit in a single cache memory entry. In one load, you would get key and values. By keeping them separate, one would not be able to benefit from this.

I guess this decision has been made based on benchmarks.

1 Like

Hi @Christophe_Meessen,

Are we sure though ? Is there any possible way for me to ask /double check why they chose this solution.

I have read the code, it has decent documentation, but it explains little about the why or the architectural decisions.

If you want to get feedback from the authors, you should ask your question to the go-nuts google group.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.