From nobody Fri Jan 16 22:27:34 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dtDxf6KcVz6NyLh for ; Fri, 16 Jan 2026 22:27:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dtDxf5ltdz3rrD for ; Fri, 16 Jan 2026 22:27:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1768602454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3FgspVwcq4bO14xlYZtRvmFO/Ap1NCPFVgqG67oRG/Y=; b=UDkz/1Xhf/SGZhpZ840yPUaBswFw/HM80YwFsrpoSv0b5NbEJcgyaC6gV/UaIfSfHbkzev dgrbrFU3EHTsA+KOdxmCzrmnkJzP15ODGR4Zy+tSSJpnseRwVSOBOHMFkQaZxwqN7qb0G1 Ekl4k64zc+5S2nLQnwunqrKO4njEkzF4rQ2wr2QCLuThAzwFTv2YODN2ssCzihGo5YkxnP H5w7voB4rsCuidILWeAuSO2gVFS5OKOquFGZzMoZzrACY/CyqNzJkh5yDxy152xhyIK6Cp AGfjPdNeqCs6U05Md8a/I+FhFP9iLrfHCUwT2KjdsqJzJBgLhYqWh/+bKkDKJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1768602454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3FgspVwcq4bO14xlYZtRvmFO/Ap1NCPFVgqG67oRG/Y=; b=L7wr64R85l9gEA2ROAF08pgk/gZhLwtYu3yKlD47lHi2nFjGA7Su82uQqOZTdppLL2uZyT Ujq8ZsStV31s4l09+6AYpImoRpYddZ5Tu2fGFx3pYvUyEQDR4YpvUiM5ONkuif6x1iFTLi EFtlEi8thbC+BcT2lYNhSGaV6BHum73OnSJmnCXbk8U/0i5MsZ9HWclpDVxA5sXHHMK14l Yv0t+zhhe5QAhQjeHSREN8lOf1T1soOthphejeh68h9LRwF73nrJ6Uwm6Ghlgy9xH90uK+ /lguVKz+bJfWatiFXvxopkfGKb9lP/9KntJZlp5MsKcunN/qAq29mg36BmQX8w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1768602454; a=rsa-sha256; cv=none; b=msJ/2BJATCYxI05TWPPOMvZwpcemQUbC5RJ9ZLiZA44t4GKXKlQTmAiYMiqFbgZZUlGA4w rraiYbqt+7R9mYFJUxj728AJcOwJvYpa2J9hxkSDXOJMuXvU5vouTGfAcsC+qu4kS5DnpS PGUzP6b3TD6XhPyT6J0pd3C7nPWpi9AUgarchcxXrgHsott58ClD+q6Zm1oTOOem7K33Xx urzIXqKazoYtKsz/WlwVFTGIFq+/Q6VgEofwASVFDdttHraGX1ckoCalhzWP2DN9qoKS4T 6fchs1uM/pwlUAIXCtEMu59LoJ6typ+aKjA/yBQKIUY4ezs5eA3zTNdSv9SCmw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dtDxf4PyrzZ6k for ; Fri, 16 Jan 2026 22:27:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 35b8c by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 16 Jan 2026 22:27:34 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Doug Moore Subject: git: 988555e329d0 - main - tdestroy: don't visit one-child node twice List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dougm X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 988555e329d00a47c42e5e849e78c1b8e4ce2e17 Auto-Submitted: auto-generated Date: Fri, 16 Jan 2026 22:27:34 +0000 Message-Id: <696abb56.35b8c.7fd6e468@gitrepo.freebsd.org> The branch main has been updated by dougm: URL: https://cgit.FreeBSD.org/src/commit/?id=988555e329d00a47c42e5e849e78c1b8e4ce2e17 commit 988555e329d00a47c42e5e849e78c1b8e4ce2e17 Author: Doug Moore AuthorDate: 2026-01-16 22:26:09 +0000 Commit: Doug Moore CommitDate: 2026-01-16 22:26:09 +0000 tdestroy: don't visit one-child node twice Change tdestroy() to immediately free a node with no right child as soon as it is encountered. Currently, such nodes are visited twice before deletion. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D54699 --- lib/libc/stdlib/tdestroy.c | 66 ++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/lib/libc/stdlib/tdestroy.c b/lib/libc/stdlib/tdestroy.c index c324e151da11..2aeb02228e46 100644 --- a/lib/libc/stdlib/tdestroy.c +++ b/lib/libc/stdlib/tdestroy.c @@ -16,53 +16,51 @@ nul_node_free(void *node __unused) { } -/* Find the leftmost node. */ -static posix_tnode * -tdestroy_find_leftmost(posix_tnode *tn) -{ - while (tn->llink != NULL) - tn = tn->llink; - return (tn); -} - -/* - * This algorithm for non-recursive non-allocating destruction of the tree - * is described in - * https://codegolf.stackexchange.com/questions/478/free-a-binary-tree/489#489P - * and in https://devblogs.microsoft.com/oldnewthing/20251107-00/?p=111774. - */ void tdestroy(void *rootp, void (*node_free)(void *)) { - posix_tnode *tn, *tn_leftmost, *xtn; + posix_tnode *back, *curr, **front; - tn = rootp; - if (tn == NULL) + if (rootp == NULL) return; if (node_free == NULL) node_free = nul_node_free; - tn_leftmost = tn; - while (tn != NULL) { + back = rootp; + front = &back; + + for (;;) { /* - * Make the right subtree the left subtree of the - * leftmost node, and recalculate the leftmost. + * The sequence of nodes from back to just before *front linked + * by llink have been found to have non-NULL rlink. + * + * Extend *front to (*front)->llink, deleting *front from the + * sequence if it has a NULL rlink. */ - tn_leftmost = tdestroy_find_leftmost(tn_leftmost); - if (tn->rlink != NULL) { - tn_leftmost->llink = tn->rlink; - tn_leftmost = tn_leftmost->llink; + curr = *front; + if (curr->rlink != NULL) + front = &curr->llink; + else { + *front = curr->llink; + node_free(curr->key); + free(curr); } + if (*front != NULL) + continue; + if (back == NULL) + break; /* - * At this point, all children of tn have been - * arranged to be reachable via tn->left. We can - * safely delete the current node and advance to its - * left child as the new root. + * The sequence cannot be extended because *front is NULL. Make + * the rlink of the back node the new *front, the llink of the + * back node the new back, and free the old back node. */ - xtn = tn->llink; - node_free(tn->key); - free(tn); - tn = xtn; + curr = back; + back = curr->llink; + if (back == NULL) + front = &back; + *front = curr->rlink; + node_free(curr->key); + free(curr); } }