freebsd-nq/include
Ed Schouten 459d04a5ee Let tsearch()/tdelete() use an AVL tree.
The existing implementations of POSIX tsearch() and tdelete() don't
attempt to perform any balancing at all. Testing reveals that inserting
100k nodes into a tree sequentially takes approximately one minute on my
system.

Though most other BSDs also don't use any balanced tree internally, C
libraries like glibc and musl do provide better implementations. glibc
uses a red-black tree and musl uses an AVL tree.

Red-black trees have the advantage over AVL trees that they only require
O(1) rotations after insertion and deletion, but have the disadvantage
that the tree has a maximum depth of 2*log2(n) instead of 1.44*log2(n).
My take is that it's better to focus on having a lower maximum depth,
for the reason that in the case of tsearch() the invocation of the
comparator likely dominates the running time.

This change replaces the tsearch() and tdelete() functions by versions
that create an AVL tree. Compared to musl's implementation, this version
is different in two different ways:

- We don't keep track of heights; just balances. This is sufficient.
  This has the advantage that it reduces the number of nodes that are
  being accessed. Storing heights requires us to also access all of the
  siblings along the path.

- Don't use any recursion at all. We know that the tree cannot 2^64
  elements in size, so the height of the tree can never be larger than
  96. Use a 128-bit bitmask to keep track of the path that is computed.
  This allows us to iterate over the same path twice, meaning we can
  apply rotations from top to bottom.

Inserting 100k nodes into a tree now only takes 0.015 seconds. Insertion
seems to be twice as fast as glibc, whereas deletion has about the same
performance. Unlike glibc, it uses a fixed amount of memory.

I also experimented with both recursive and iterative bottom-up
implementations of the same algorithm. This iterative top-down version
performs similar to the recursive bottom-up version in terms of speed
and code size.

For some reason, the iterative bottom-up algorithm was actually 30%
faster for deletion, but has a quadratic memory complexity to keep track
of all the parent pointers.

Reviewed by:	jilles
Obtained from:	https://github.com/NuxiNL/cloudlibc
Differential Revision:	https://reviews.freebsd.org/D4412
2015-12-22 18:12:11 +00:00
..
arpa Add META_MODE support. 2015-06-13 19:20:56 +00:00
gssapi Add META_MODE support. 2015-06-13 19:20:56 +00:00
protocols Add META_MODE support. 2015-06-13 19:20:56 +00:00
rpc Add META_MODE support. 2015-06-13 19:20:56 +00:00
rpcsvc Add META_MODE support. 2015-06-13 19:20:56 +00:00
xlocale Commit log from Dragonfly: 2015-10-13 20:43:49 +00:00
_ctype.h Commit log from Dragonfly: 2015-10-13 20:43:49 +00:00
a.out.h
ar.h
assert.h
bitstring.h
complex.h
cpio.h
ctype.h Commit log from Dragonfly: 2015-10-13 20:43:49 +00:00
db.h
dirent.h
dlfcn.h
elf-hints.h
elf.h
err.h
fmtmsg.h
fnmatch.h
fstab.h
fts.h
ftw.h
getopt.h
glob.h
grp.h
gssapi.h
hesiod.h
iconv.h
ieeefp.h
ifaddrs.h
inttypes.h
iso646.h
kenv.h
langinfo.h
libgen.h
limits.h Merge from HEAD 2015-08-09 00:15:17 +00:00
link.h
locale.h
Makefile Move obscure lib/ installation of /usr/lib/include symlink to include/. 2015-12-04 03:18:02 +00:00
Makefile.depend new depends 2015-06-16 23:37:19 +00:00
malloc_np.h Use bool rather than _Bool for C++ compatibility. 2015-08-19 18:32:12 +00:00
malloc.h
memory.h
mk-osreldate.sh Stop building vers.c in include/ and only build the needed osreldate.h. 2015-11-25 19:10:59 +00:00
monetary.h
mpool.h
mqueue.h
ndbm.h
netconfig.h
netdb.h addrinfo.ai_family is an address family, not a protocol family. 2015-12-20 15:18:50 +00:00
nl_types.h
nlist.h
nss.h
nsswitch.h
paths.h
printf.h
proc_service.h
pthread_np.h
pthread.h
pwd.h
ranlib.h
readpassphrase.h
regex.h
res_update.h
resolv.h resolver: preserve binary compatibility; reduce header pollution 2015-12-14 17:21:06 +00:00
runetype.h
search.h Let tsearch()/tdelete() use an AVL tree. 2015-12-22 18:12:11 +00:00
semaphore.h
setjmp.h
signal.h
spawn.h
stab.h
stdalign.h
stdbool.h
stddef.h
stdio.h Add _flags2 per jhb@ suggestion since no room left in _flags. 2015-10-28 14:40:02 +00:00
stdlib.h
stdnoreturn.h
string.h
stringlist.h
strings.h
sysexits.h
tar.h
termios.h
tgmath.h
time.h
timeconv.h
timers.h
ttyent.h
uchar.h
ulimit.h
unistd.h Rename __sentinel to __null_sentinel 2015-11-05 14:55:58 +00:00
unwind.h
utime.h
utmpx.h
uuid.h
varargs.h
wchar.h
wctype.h Commit log from Dragonfly: 2015-10-13 20:43:49 +00:00
wordexp.h
xlocale.h