freebsd-dev/lib/libc
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
..
aarch64 Add support for usermode (vdso-like) gettimeofday(2) and 2015-12-07 12:20:26 +00:00
amd64 Add include for declaration of _set_tp(). Eliminates -Wmissing-prototypes warnings. 2015-09-20 04:59:01 +00:00
arm Add support for usermode (vdso-like) gettimeofday(2) and 2015-12-07 12:20:26 +00:00
capability use .Mt to mark up email addresses consistently (part4) 2014-06-23 08:25:03 +00:00
compat-43 Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-20 20:53:24 +00:00
db Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-20 20:55:00 +00:00
gdtoa META MODE: Don't create .meta files when symlinking sources into the obj directory. 2015-11-25 19:44:43 +00:00
gen libc: Use namespace.h in a few more files. 2015-12-20 23:05:20 +00:00
gmon Add declaration to eliminate -Wmissing-prototypes warning 2015-09-20 03:59:27 +00:00
i386 Switch libc from using _sig{procmask,action,suspend} symbols, which 2015-08-29 14:25:01 +00:00
iconv libc: Use namespace.h in a few more files. 2015-12-20 23:05:20 +00:00
include Switch libc from using _sig{procmask,action,suspend} symbols, which 2015-08-29 14:25:01 +00:00
inet Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-20 20:50:56 +00:00
isc Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-23 16:16:16 +00:00
locale Link localeconv(3) to localeconv_l(3) 2015-11-25 09:12:30 +00:00
md Bring the md5 functions into libc for internal use only. 2014-08-12 12:25:56 +00:00
mips Add include for declaration of _set_tp(). Eliminates -Wmissing-prototypes warnings. 2015-09-20 04:59:01 +00:00
nameser Add missing break. 2014-08-20 14:58:25 +00:00
net We don't need to use a temporary buffer, here. 2015-12-22 12:01:06 +00:00
nls Use mdoc(7) macros to handle parenthesis 2015-04-26 10:54:52 +00:00
posix1e Remove names from some prototypes 2015-09-20 20:16:34 +00:00
powerpc Add include for declaration of _set_tp(). Eliminates -Wmissing-prototypes warnings. 2015-09-20 04:59:01 +00:00
powerpc64 Add include for declaration of _set_tp(). Eliminates -Wmissing-prototypes warnings. 2015-09-20 04:59:01 +00:00
quad Remove MK_ARM_EABI, the armeb issues have been fixed. The code to support 2014-10-01 08:26:51 +00:00
regex Add -static to CFLAGS to unbreak the tests by using a libc.a with 2015-12-13 06:33:52 +00:00
resolv Allow users override DEBUG on the command line via DEBUG_FLAGS="-DDEBUG" with 2015-12-15 05:37:09 +00:00
riscv Start support for the RISC-V 64-bit architecture developed by UC Berkeley. 2015-12-11 22:55:23 +00:00
rpc RPC: populate local address for rendezvous transporter. 2015-12-10 05:17:04 +00:00
secure Remove a stale comment and clarify the original where it was taken from 2015-08-14 14:58:04 +00:00
softfloat
sparc64 Fix unused-but-set-variable 2015-11-15 20:44:22 +00:00
stdio Use stdint.h instead of inttypes.h as the latter pollutes namespace more 2015-12-09 09:14:57 +00:00
stdlib Let tsearch()/tdelete() use an AVL tree. 2015-12-22 18:12:11 +00:00
stdtime Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-20 20:51:52 +00:00
string collate: Fix expansion substitions (broken upstream too) 2015-10-23 23:24:03 +00:00
sys clock_gettime(2),gettimeofday(2): Remove [EFAULT] error. 2015-12-20 15:11:11 +00:00
tests Let tsearch()/tdelete() use an AVL tree. 2015-12-22 18:12:11 +00:00
uuid
xdr Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-20 20:52:13 +00:00
yp If a NIS server has long entries on its database that is bigger than 2015-11-19 13:36:53 +00:00
libc.ldscript
Makefile Move the stack protector to a new "secure" directory 2015-08-14 03:03:13 +00:00
Makefile.depend Update META_MODE dependencies. 2015-09-17 05:06:34 +00:00
Versions.def Fix the inheritance of the FBSDprivate_1.0 namespace. 2014-04-03 17:31:38 +00:00