git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: struct hashmap_entry packing

From
Karsten Blees <karsten.blees@gmail.com>
Date
Aug 4, 2014, 19:20 UTC
Message-ID
<53DFDCE2.9060406@gmail.com>
In-Reply-To
<20140801223739.GA15649@peff.net>
Am 02.08.2014 00:37, schrieb Jeff King:
Show 29 quoted lines
> On Tue, Jul 29, 2014 at 10:40:12PM +0200, Karsten Blees wrote:
> 
>>> The sizeof() has to be the same regardless of whether the hashmap_entry
>>> is standalone or in another struct, and therefore must be padded up to
>>> 16 bytes. If we stored "x" in that padding in the combined struct, it
>>> would be overwritten by our memset.
>>>
>>
>> The struct-packing patch was ultimately dropped because there was no way
>> to reliably make it work on all platforms. See [1] for discussion, [2] for
>> the final, 'most compatible' version.
> 
> Thanks for the pointers; I should have guessed there was more to it and
> searched the archive myself.
> 
>> Hmmm. Now that we have "__attribute__((packed))" in pack-bitmap.h, perhaps
>> we should do the same for stuct hashmap_entry? (Which was the original
>> proposal anyway...). Only works for GCC, but that should cover most builds
>> / platforms.
> 
> I don't see any reason to avoid the packed attribute, if it helps us. As
> you noted, anything using __attribute__ probably supports it, and if
> not, we can conditionally #define PACKED_STRUCT or something, like we do
> for NORETURN. Since it's purely an optimization, if another compiler
> doesn't use it, no big deal.
> 
> That being said, I don't know if those padding bytes are actually
> causing a measurable slowdown. It may not even be worth the trouble.
> 

Its not about performance (or correctness, in case of platforms that don't support unaligned read), just about saving memory (e.g. mapping int to int requires 24 bytes per entry, vs. 16 with packed structs).

The padding at the end of a structure is only needed for proper alignment in arrays. Struct hashmap_entry is always used as prefix of some other structure, never as an array, so there are no alignment issues here.

Typical memory layouts on 64-bit platforms are as follows (note that even in the 'followed by int64' case, all members are properly aligned):

Unpacked struct followed by int32 - wastes 1/3 of memory:
      struct {
        struct hashmap_entry {
00-07     struct hashmap_entry *next;
08-11     int hash;
12-15     // padding
        } ent;
16-19   int32_t value;
20-23   // padding
      }
Packed struct followed by int32:
      struct {
        struct hashmap_entry {
00-07     struct hashmap_entry *next;
08-11     int hash;
        } ent;
12-15   int32_t value;
      }
Packed struct followed by int64:
      struct {
        struct hashmap_entry {
00-07     struct hashmap_entry *next;
08-11     int hash;
        } ent;
12-15   // padding
16-23   int64_t value;
      }
Array of packed struct (not used):
      struct hashmap_entry {
00-07   struct hashmap_entry *next;
08-11   int hash;
      }; // [0]
      struct hashmap_entry {
12-19   struct hashmap_entry *next; // !!!unaligned!!!
20-23   int hash;
      }; // [1]
Previous: Junio C HamanoNext: Jeff King
Message 11 of 12 in “struct hashmap_entry packing”
  1. Jeff KingJul 28, 2014
  2. Karsten BleesJul 29, 2014
  3. Jeff KingAug 1, 2014
  4. pack-bitmap: do not use gcc packed attributeJeff King, Aug 1, 2014
  5. Jeff KingAug 1, 2014
  6. Karsten BleesAug 4, 2014
  7. Vicent MartíAug 5, 2014
  8. Jeff KingAug 5, 2014
  9. Karsten BleesAug 6, 2014
  10. Junio C HamanoAug 6, 2014
  11. Karsten BleesAug 4, 2014
  12. Jeff KingAug 5, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.