named-data-iot/ndn-lite

Wasted memory in ndn_metainfo_t

Closed this issue · 2 comments

As of 513e020, ndn_metainfo_t type is declared as:

typedef struct ndn_metainfo {
  uint8_t content_type;
  uint32_t freshness_period;
  name_component_t final_block_id;
  uint8_t enable_ContentType;
  uint8_t enable_FreshnessPeriod;
  uint8_t enable_FinalBlockId;
} ndn_metainfo_t;

Due to memory alignment rules, the compiler inserts a 3-byte padding before freshness_period field. This struct would consume 55 bytes of memory (name_component_t has 44 bytes).

A better arrangement is:

typedef struct ndn_metainfo {
  uint32_t freshness_period;
  name_component_t final_block_id;
  uint8_t content_type;
  uint8_t enable_ContentType;
  uint8_t enable_FreshnessPeriod;
  uint8_t enable_FinalBlockId;
} ndn_metainfo_t;

It needs 52 bytes of memory, fewer than the previous layout.

Pesa commented

If you want to save even more space, the uint8_t enable_* fields, that are used as boolean flags, can be grouped in a single bitfield.

the uint8_t enable_* fields, that are used as boolean flags, can be grouped in a single bitfield.

Moreover, they can be packed into high bits of FreshnessPeriod. 24 bits are enough for FreshnessPeriod.