Skip to content

Commit

Permalink
chg: dev: Optimize memory layout of core structs
Browse files Browse the repository at this point in the history
Reduce memory footprint by:
- Reordering struct fields to minimize padding.
- Using exact-sized atomic types instead of `*_least`/`*_fast` variants
- Downsizing integer fields where possible

Affected structs:
- dns_name_t
- dns_slabheader_t 
- dns_rdata_t
- qpcnode_t
- qpznode_t

Closes #5022

Merge branch '5022-reduce-metadata-overhead-by-struct-packing' into 'main'

See merge request isc-projects/bind9!9721
  • Loading branch information
Alessio Podda committed Nov 27, 2024
2 parents b72a230 + 32c7060 commit d94e882
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 36 deletions.
4 changes: 4 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,10 @@ cross-version-config-tests:
# also from the $BIND_BASELINE_VERSION.
- find bin/tests/system/ -mindepth 1 -maxdepth 1 -type d -exec sh -c 'test -e ../"$0" || rm -rfv -- "$0"' {} \;
- cd bin/tests/system
# System tests that employ binary drivers will fail on ABI change and
# should not be run.
- rm -r dlzexternal
- rm -r dyndb
# Run the setup phase of all system tests in the most recently tagged BIND 9
# release using the binaries built for the current BIND 9 version. This
# intends to detect obvious backward compatibility issues with the latter.
Expand Down
2 changes: 0 additions & 2 deletions lib/dns/dnssec.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ dns_dnssec_sign(const dns_name_t *name, dns_rdataset_t *set, dst_key_t *key,
* Digest the length of the rdata.
*/
isc_buffer_init(&lenbuf, &len, sizeof(len));
INSIST(rdatas[i].length < 65536);
isc_buffer_putuint16(&lenbuf, (uint16_t)rdatas[i].length);
isc_buffer_usedregion(&lenbuf, &lenr);
ret = dst_context_adddata(ctx, &lenr);
Expand Down Expand Up @@ -537,7 +536,6 @@ dns_dnssec_verify(const dns_name_t *name, dns_rdataset_t *set, dst_key_t *key,
* Digest the rdata length.
*/
isc_buffer_init(&lenbuf, &len, sizeof(len));
INSIST(rdatas[i].length < 65536);
isc_buffer_putuint16(&lenbuf, (uint16_t)rdatas[i].length);
isc_buffer_usedregion(&lenbuf, &lenr);

Expand Down
8 changes: 4 additions & 4 deletions lib/dns/include/dns/name.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ ISC_LANG_BEGINDECLS
* for whatever purpose the client desires.
*/
struct dns_name {
unsigned int magic;
unsigned char *ndata;
unsigned int length;
unsigned int labels;
unsigned int magic;
uint8_t length;
uint8_t labels;
struct dns_name_attrs {
bool absolute : 1; /*%< Used by name.c */
bool readonly : 1; /*%< Used by name.c */
Expand All @@ -116,6 +115,7 @@ struct dns_name {
bool update : 1; /*%< Used by client. */
bool hasupdaterec : 1; /*%< Used by client. */
} attributes;
unsigned char *ndata;
unsigned char *offsets;
isc_buffer_t *buffer;
ISC_LINK(dns_name_t) link;
Expand Down
11 changes: 6 additions & 5 deletions lib/dns/include/dns/rdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ ISC_LANG_BEGINDECLS
*/
struct dns_rdata {
unsigned char *data;
unsigned int length;
dns_rdataclass_t rdclass;
dns_rdatatype_t type;
unsigned int flags;
uint16_t length;
uint16_t flags;
ISC_LINK(dns_rdata_t) link;
};

#define DNS_RDATA_INIT \
{ \
NULL, 0, 0, 0, 0, { (void *)(-1), (void *)(-1) } \
#define DNS_RDATA_INIT \
{ \
.data = NULL, \
.link = ISC_LINK_INITIALIZER, \
}

#define DNS_RDATA_CHECKINITIALIZED
Expand Down
30 changes: 15 additions & 15 deletions lib/dns/include/dns/rdataslab.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,32 @@ struct dns_slabheader_proof {
};

struct dns_slabheader {
_Atomic(uint16_t) attributes;

/*%
* Locked by the owning node's lock.
*/
uint32_t serial;
dns_ttl_t ttl;
dns_typepair_t type;
atomic_uint_least16_t attributes;
dns_trust_t trust;
dns_trust_t trust;
uint32_t serial;
dns_ttl_t ttl;
dns_typepair_t type;

unsigned int heap_index;
_Atomic(uint16_t) count;
/*%<
* Used for TTL-based cache cleaning.
* Monotonically increased every time this rdataset is bound so that
* it is used as the base of the starting point in DNS responses
* when the "cyclic" rrset-order is required.
*/

isc_stdtime_t resign;
unsigned int resign_lsb : 1;

atomic_uint_fast16_t count;
isc_stdtime_t resign;
unsigned int heap_index;
/*%<
* Monotonically increased every time this rdataset is bound so that
* it is used as the base of the starting point in DNS responses
* when the "cyclic" rrset-order is required.
* Used for TTL-based cache cleaning.
*/

atomic_uint_fast32_t last_refresh_fail_ts;
isc_stdtime_t last_used;
_Atomic(uint32_t) last_refresh_fail_ts;

dns_slabheader_proof_t *noqname;
dns_slabheader_proof_t *closest;
Expand Down Expand Up @@ -122,7 +123,6 @@ struct dns_slabheader {
* this rdataset, if any.
*/

isc_stdtime_t last_used;
ISC_LINK(struct dns_slabheader) link;

/*%
Expand Down
1 change: 0 additions & 1 deletion lib/dns/journal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,6 @@ dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
isc_buffer_putuint16(&buffer, t->rdata.type);
isc_buffer_putuint16(&buffer, t->rdata.rdclass);
isc_buffer_putuint32(&buffer, t->ttl);
INSIST(t->rdata.length < 65536);
isc_buffer_putuint16(&buffer, (uint16_t)t->rdata.length);
INSIST(isc_buffer_availablelength(&buffer) >= t->rdata.length);
isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
Expand Down
6 changes: 2 additions & 4 deletions lib/dns/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ dns_name_isvalid(const dns_name_t *name) {
return false;
}

if (name->length > DNS_NAME_MAXWIRE ||
name->labels > DNS_NAME_MAXLABELS)
{
if (name->labels > DNS_NAME_MAXLABELS) {
return false;
}

Expand Down Expand Up @@ -616,7 +614,7 @@ dns_name_getlabel(const dns_name_t *name, unsigned int n, dns_label_t *label) {
SETUP_OFFSETS(name, offsets, odata);

label->base = &name->ndata[offsets[n]];
if (n == name->labels - 1) {
if (n == (unsigned int)name->labels - 1) {
label->length = name->length - offsets[n];
} else {
label->length = offsets[n + 1] - offsets[n];
Expand Down
2 changes: 1 addition & 1 deletion lib/dns/nsec3.c
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ void
dns_nsec3param_toprivate(dns_rdata_t *src, dns_rdata_t *target,
dns_rdatatype_t privatetype, unsigned char *buf,
size_t buflen) {
REQUIRE(buflen >= src->length + 1);
REQUIRE(buflen >= (unsigned int)src->length + 1);

REQUIRE(DNS_RDATA_INITIALIZED(target));

Expand Down
3 changes: 2 additions & 1 deletion lib/dns/qpcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ struct qpcnode {
unsigned int nsec : 2; /*%< range is 0..3 */
uint8_t : 0;

uint16_t locknum;

isc_refcount_t references;
isc_refcount_t erefs;
uint16_t locknum;
void *data;

/*%
Expand Down
2 changes: 1 addition & 1 deletion lib/dns/qpzone.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ struct qpznode {
isc_refcount_t references;
isc_refcount_t erefs;
uint16_t locknum;
void *data;
atomic_uint_fast8_t nsec;
atomic_bool wild;
atomic_bool delegating;
atomic_bool dirty;
void *data;
};

struct qpzonedb {
Expand Down
5 changes: 3 additions & 2 deletions lib/dns/zone.c
Original file line number Diff line number Diff line change
Expand Up @@ -17203,7 +17203,8 @@ save_nsec3param(dns_zone_t *zone, nsec3paramlist_t *nsec3list) {
{
next = ISC_LIST_NEXT(nsec3p, link);

if (nsec3p->length == rdata.length + 1 &&
if (nsec3p->length ==
(unsigned int)rdata.length + 1 &&
memcmp(rdata.data, nsec3p->data + 1,
nsec3p->length - 1) == 0)
{
Expand Down Expand Up @@ -23774,7 +23775,7 @@ rss_post(void *arg) {
dns_rdata_init(&rdata);
dns_rdataset_current(&nrdataset, &rdata);

if (np->length == (rdata.length + 1) &&
if (np->length == ((unsigned int)rdata.length + 1) &&
memcmp(rdata.data, np->data + 1, np->length - 1) ==
0)
{
Expand Down

0 comments on commit d94e882

Please sign in to comment.