freebsd kernel with SKQ
Go to file
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
bin sh/mknodes: Close files and check for errors when writing. 2015-12-20 16:40:36 +00:00
cddl Add a trailing newline to the expected output for tst.walltimestamp.ksh. 2015-12-07 21:59:10 +00:00
contrib Don't adjust the program counter to an invalid address after reaching a 2015-12-22 17:18:40 +00:00
crypto Incorrect length in calloc() call, already fixed upstream. 2015-12-17 19:36:25 +00:00
etc Integrate tools/regression/mac/mac_bsdextended and 2015-12-21 21:24:03 +00:00
gnu Fix LDADD/DPADD that should be LIBADD. 2015-12-04 03:17:47 +00:00
include Let tsearch()/tdelete() use an AVL tree. 2015-12-22 18:12:11 +00:00
kerberos5 DIRDEPS_BUILD: Update dependencies. 2015-12-07 23:53:01 +00:00
lib Let tsearch()/tdelete() use an AVL tree. 2015-12-22 18:12:11 +00:00
libexec rtld: Use common NT_FREEBSD_* note types introduced in r291909 2015-12-21 22:40:29 +00:00
release Fix tmpfs(5) '/tmp' mount point. 2015-11-25 19:10:04 +00:00
rescue Fix the build after ifconfig was converted over to lib80211 in r291470 2015-11-30 16:34:13 +00:00
sbin Fix "mount -a" for NFS and ZFS filesystems with shared mountpoints 2015-12-21 22:19:22 +00:00
secure Build engines in parallel. 2015-12-15 19:57:56 +00:00
share Restore a day of the week in date(1) output for Japanese locales. 2015-12-20 14:59:30 +00:00
sys Fix speed setting by NVRAM for 24xx and above chips. 2015-12-22 17:01:30 +00:00
targets local.meta.sys.mk already defines TARGET_ARCHES_arm 2015-12-08 00:22:24 +00:00
tests Dump out the output from flock_helper on failure so failures with the 2015-12-22 02:10:31 +00:00
tools For embedded platforms that require it, use mtools to copy the 2015-12-22 06:36:00 +00:00
usr.bin RIP Juergen Lock (nox@FreeBSD.org), you and your contributions won't be forgotten. 2015-12-17 10:37:11 +00:00
usr.sbin Undo change from r292607 that was not meant to be there 2015-12-22 15:42:53 +00:00
.arcconfig Add repository.callsign, to help arcanist figure out what repo it's 2015-07-02 22:23:52 +00:00
.arclint phabricator related changes: 2015-04-20 20:33:22 +00:00
COPYRIGHT Bump copyright year. 2014-12-31 10:00:43 +00:00
LOCKS
MAINTAINERS Refer to creating Phabricator Herald notifications 2015-12-22 17:46:14 +00:00
Makefile Update advice on obj disk space requirements 2015-12-14 19:20:18 +00:00
Makefile.inc1 Always expose LOCALBASE, not just when CROSS_TOOLCHAIN is defined 2015-12-20 05:52:03 +00:00
ObsoleteFiles.inc Correct r291591: Don't remove /usr/lib/libelf*, these are still installed. 2015-12-01 16:16:40 +00:00
README README: changes and fixups 2015-04-19 07:16:44 +00:00
UPDATING pxeboot: make the tftp loader use the option root-path directive 2015-12-16 17:13:09 +00:00

This is the top level of the FreeBSD source directory.  This file
was last revised on:
$FreeBSD$

For copyright information, please see the file COPYRIGHT in this
directory (additional copyright information also exists for some
sources in this tree - please see the specific source directories for
more information).

The Makefile in this directory supports a number of targets for
building components (or all) of the FreeBSD source tree.  See build(7)
and http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/makeworld.html
for more information, including setting make(1) variables.

The `buildkernel` and `installkernel` targets build and install
the kernel and the modules (see below).  Please see the top of
the Makefile in this directory for more information on the
standard build targets and compile-time flags.

Building a kernel is a somewhat more involved process.  See build(7), config(8),
and http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/kernelconfig.html
for more information.

Note: If you want to build and install the kernel with the
`buildkernel` and `installkernel` targets, you might need to build
world before.  More information is available in the handbook.

The kernel configuration files reside in the sys/<arch>/conf
sub-directory.  GENERIC is the default configuration used in release builds.
NOTES contains entries and documentation for all possible
devices, not just those commonly used.


Source Roadmap:
---------------

bin		System/user commands.

cddl		Various commands and libraries under the Common Development
		and Distribution License.

contrib		Packages contributed by 3rd parties.

crypto		Cryptography stuff (see crypto/README).

etc		Template files for /etc.

games		Amusements.

gnu		Various commands and libraries under the GNU Public License.
		Please see gnu/COPYING* for more information.

include		System include files.

kerberos5	Kerberos5 (Heimdal) package.

lib		System libraries.

libexec		System daemons.

release		Release building Makefile & associated tools.

rescue		Build system for statically linked /rescue utilities.

sbin		System commands.

secure		Cryptographic libraries and commands.

share		Shared resources.

sys		Kernel sources.

tests		Regression tests which can be run by Kyua.  See tests/README
		for additional information.

tools		Utilities for regression testing and miscellaneous tasks.

usr.bin		User commands.

usr.sbin	System administration commands.


For information on synchronizing your source tree with one or more of
the FreeBSD Project's development branches, please see:

  http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/synching.html