<feed xmlns='http://www.w3.org/2005/Atom'>
<title>src/lib/libc/stdlib/tdelete.c, branch release/14.4.0</title>
<subtitle>FreeBSD source tree</subtitle>
<id>https://cgit-dev.freebsd.org/src/atom?h=release%2F14.4.0</id>
<link rel='self' href='https://cgit-dev.freebsd.org/src/atom?h=release%2F14.4.0'/>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/'/>
<updated>2023-11-27T04:20:09Z</updated>
<entry>
<title>libc: Purge unneeded cdefs.h</title>
<updated>2023-11-27T04:20:09Z</updated>
<author>
<name>Warner Losh</name>
<email>imp@FreeBSD.org</email>
</author>
<published>2023-11-01T22:43:37Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=4025b5b527c870cf095ac9531764f4e4d3feaec2'/>
<id>urn:sha1:4025b5b527c870cf095ac9531764f4e4d3feaec2</id>
<content type='text'>
These sys/cdefs.h are not needed. Purge them. They are mostly left-over
from the $FreeBSD$ removal. A few in libc are still required for macros
that cdefs.h defines. Keep those.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D42385

(cherry picked from commit 559a218c9b257775fb249b67945fe4a05b7a6b9f)
</content>
</entry>
<entry>
<title>Remove $FreeBSD$: one-line .c pattern</title>
<updated>2023-08-16T17:54:42Z</updated>
<author>
<name>Warner Losh</name>
<email>imp@FreeBSD.org</email>
</author>
<published>2023-08-16T17:54:42Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=1d386b48a555f61cb7325543adbbb5c3f3407a66'/>
<id>urn:sha1:1d386b48a555f61cb7325543adbbb5c3f3407a66</id>
<content type='text'>
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
</content>
</entry>
<entry>
<title>Improve typing of POSIX search tree functions.</title>
<updated>2016-10-13T18:25:40Z</updated>
<author>
<name>Ed Schouten</name>
<email>ed@FreeBSD.org</email>
</author>
<published>2016-10-13T18:25:40Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=4ef9bd22ed091dd6513f89cdca2dfd03b6e3ffd2'/>
<id>urn:sha1:4ef9bd22ed091dd6513f89cdca2dfd03b6e3ffd2</id>
<content type='text'>
Back in 2015 when I reimplemented these functions to use an AVL tree, I
was annoyed by the weakness of the typing of these functions. Both tree
nodes and keys are represented by 'void *', meaning that things like the
documentation for these functions are an absolute train wreck.

To make things worse, users of these functions need to cast the return
value of tfind()/tsearch() from 'void *' to 'type_of_key **' in order to
access the key. Technically speaking such casts violate aliasing rules.
I've observed actual breakages as a result of this by enabling features
like LTO.

I've filed a bug report at the Austin Group. Looking at the way the bug
got resolved, they made a pretty good step in the right direction. A new
type 'posix_tnode' has been added to correspond to tree nodes. It is
still defined as 'void' for source-level compatibility, but in the very
far future it could be replaced by a proper structure type containing a
key pointer.

MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D8205
</content>
</entry>
<entry>
<title>Remove an unneeded assignment of the return value.</title>
<updated>2016-01-14T07:27:42Z</updated>
<author>
<name>Ed Schouten</name>
<email>ed@FreeBSD.org</email>
</author>
<published>2016-01-14T07:27:42Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=31969237964b5e529aae30002608713c428965cb'/>
<id>urn:sha1:31969237964b5e529aae30002608713c428965cb</id>
<content type='text'>
tdelete() is supposed to return the address of the parent node that has
been deleted. We already keep track of this node in the loop between
lines 94-107. The GO_LEFT()/GO_RIGHT() macros are used later on as well,
so we must make sure not to change it to something else.
</content>
</entry>
<entry>
<title>Let tsearch()/tdelete() use an AVL tree.</title>
<updated>2015-12-22T18:12:11Z</updated>
<author>
<name>Ed Schouten</name>
<email>ed@FreeBSD.org</email>
</author>
<published>2015-12-22T18:12:11Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=459d04a5eecbe8ea330ef9c6cbba6392b16d317e'/>
<id>urn:sha1:459d04a5eecbe8ea330ef9c6cbba6392b16d317e</id>
<content type='text'>
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
</content>
</entry>
<entry>
<title>Update comment and NetBSD ID tag.</title>
<updated>2015-02-06T14:22:00Z</updated>
<author>
<name>Pedro F. Giffuni</name>
<email>pfg@FreeBSD.org</email>
</author>
<published>2015-02-06T14:22:00Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=02aa7d7b57f1037b0449603b4a20df6939328999'/>
<id>urn:sha1:02aa7d7b57f1037b0449603b4a20df6939328999</id>
<content type='text'>
The NetBSD revisions correspond to changes we have already done
like __P() removal and ANSI-fication of definitions.
</content>
</entry>
<entry>
<title>tdelete(3): don't delete the node we are about to return.</title>
<updated>2015-02-05T23:02:43Z</updated>
<author>
<name>Pedro F. Giffuni</name>
<email>pfg@FreeBSD.org</email>
</author>
<published>2015-02-05T23:02:43Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=b20592de1b2a780f7610dba094fbb7331f31a264'/>
<id>urn:sha1:b20592de1b2a780f7610dba094fbb7331f31a264</id>
<content type='text'>
CID:		272528
Obtained from:	NetBSD (CVS rev. 1.4)
MFC after:	2 weeks
</content>
</entry>
<entry>
<title>No need to include &lt;assert.h&gt; here.</title>
<updated>2003-01-05T02:43:18Z</updated>
<author>
<name>Tim J. Robbins</name>
<email>tjr@FreeBSD.org</email>
</author>
<published>2003-01-05T02:43:18Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=051900864f8b60454e360eb2ef36e245b4d3e647'/>
<id>urn:sha1:051900864f8b60454e360eb2ef36e245b4d3e647</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Style: One space between "restrict" qualifier and "*".</title>
<updated>2002-09-06T11:24:06Z</updated>
<author>
<name>Tim J. Robbins</name>
<email>tjr@FreeBSD.org</email>
</author>
<published>2002-09-06T11:24:06Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=58d38e25205c6ee5ef0796ffa2cd8e2ca6c6e7f3'/>
<id>urn:sha1:58d38e25205c6ee5ef0796ffa2cd8e2ca6c6e7f3</id>
<content type='text'>
</content>
</entry>
<entry>
<title> - Add the 'restrict' qualifier to match the IEEE Std 1003.1-2001</title>
<updated>2002-08-14T21:16:41Z</updated>
<author>
<name>Robert Drehmel</name>
<email>robert@FreeBSD.org</email>
</author>
<published>2002-08-14T21:16:41Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=840b798c83238d9477451fc3f3af180ca6249e59'/>
<id>urn:sha1:840b798c83238d9477451fc3f3af180ca6249e59</id>
<content type='text'>
   prototype of the tdelete(3) function.
 - Remove duplicated space.
 - Use an ANSI-C function definition for tdelete(3).
 - Update the manual page.
</content>
</entry>
</feed>
