]> git.ipfire.org Git - thirdparty/glibc.git/commit
Reduce memory size of tsearch red-black tree.
authorMark Wielaard <mark@klomp.org>
Thu, 25 Aug 2016 21:47:19 +0000 (23:47 +0200)
committerMark Wielaard <mjw@redhat.com>
Thu, 25 Aug 2016 21:48:05 +0000 (23:48 +0200)
commit9d6861b8c3edcb74faedcebb0a6960c01bb6f34d
treefaf9e620c2b6e28269b087aff73eebfdcca3ffdc
parent7ed2b544511e8ad69ac95634388037ec264d52a7
Reduce memory size of tsearch red-black tree.

A tsearch red-black tree node contains 3 pointers (key, left, right)
and 1 bit to hold the red-black flag. When allocating new nodes
this 1 bit is expanded to a full word. Causing the overhead per node
to be 3 times the key size.

We can reduce this overhead to just 2 times the key size.
malloc returns naturally aligned memory. All nodes are internally
allocated with malloc and the left/right node pointers are used
as implementation details. So we can use the low bits of the
left/right node pointers to store extra information.

Replace all direct accesses of the struct node_t node pointers and
red-black value with defines that take care of the red-black flag in
the low bit of the (left) node pointer. This reduces the size of the
nodes on 32-bit systems from 16 to 12 bytes and on 64-bit systems
from 32 to 24 bytes.

Also fix a call to CHECK_TREE so the code can be build (and tested)
with DEBUGGING defined again.

V2 changes:

- Add assert after malloc to catch any odd pointers from bad
  interposed mallocs.
- Rename implementation flag to USE_MALLOC_LOW_BIT.

ChangeLog:

       * misc/tsearch.c (struct node_t): Reduce to 3 pointers if
       USE_MALLOC_LOW_BIT.  Define pointer/value accessors.
       (check_tree_recurse): Use newly defined accessors.
       (check_tree): Likewise.
       (maybe_split_for_insert): Likewise.
       (__tfind): Likewise.
       (__tdelete): Likewise.
       (trecurse): Likewise.
       (tdestroy_recurse): Likewise.
       (__tsearch): Likewise. And add asserts for malloc alignment.
       (__twalk): Cast root to node in case CHECK_TREE is defined.
ChangeLog
misc/tsearch.c