From patchwork Tue Jul 11 19:07:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1806490 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Awv0xFbc; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R0r3m4P47z20WT for ; Wed, 12 Jul 2023 05:07:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7435A3858408 for ; Tue, 11 Jul 2023 19:07:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7435A3858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689102464; bh=nrryifY8ZixdTPCMbYWIdvtOzQXwDdv+359GJ6wgTRE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Awv0xFbcmbROmqbV98r9Ymgj6iEWxdOmXjtPGjtEA0CeEeZhxI9QZGK1L3brDbPfc BWRRHw0KW0/16BOSvHPipWOHxKxes2F9vc/t66Ilpkhl64SZySaZ8QwR6lrJrRSlpt JiI9KY8QBxAIS4FvhhkfH0kRNfvQM3HBTxaW4Fco= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by sourceware.org (Postfix) with ESMTPS id 58A383858D20 for ; Tue, 11 Jul 2023 19:07:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58A383858D20 Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-1b0419f758eso4443123fac.1 for ; Tue, 11 Jul 2023 12:07:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689102447; x=1691694447; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nrryifY8ZixdTPCMbYWIdvtOzQXwDdv+359GJ6wgTRE=; b=ZjGAa3bisvHYTPtXnsUFwIj8KhPKbjMbjDolOUindrpX/DOFntvES/fjusE34zqdlM yb8kS37gcd4OxPnzpRZ4Q2vHaaX/IXzeYZJdfLx6MXep6eNsANPz1EAX1sSq5sl7pAui hoEKOVoFH0rtwrgAtkLdpC6jd1MvdIKhWoa8/5HRjGTy1xi3Q1YPyGRSOHTO9fHYTVYx 19o6HASUIcg5YnJkpUCZpG8jPE6dD2F8xYrH2snR52ND5GJQNq/JU141TL52q+1Nvz4j 94ELZYmttAgtHdYZ/9oKQawkeSSsu/SQKxaRx+w2HHpO6/oS5qE9ytZFnLL0soFdFx4t HoAA== X-Gm-Message-State: ABy/qLYkQgwPxlZIgDD3D4W+4XhQKbeoZlgyol2y+zlEy+0xVTBC8HLS nbgnEfh9Re/0FfARamwYERd99KoauCFh5mToBcv63g== X-Google-Smtp-Source: APBJJlG59lCKyv3BvVgXVlsz5TlAHZrqrtOhKHK/AKE0AaXxV2XoIe8TUIfiEV6N/aRkLIrpnO4EPg== X-Received: by 2002:a05:6870:c0d2:b0:1b7:4e91:b8b4 with SMTP id e18-20020a056870c0d200b001b74e91b8b4mr2433830oad.15.1689102446963; Tue, 11 Jul 2023 12:07:26 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:e0c8:30b2:9ea1:dcf5:7755]) by smtp.gmail.com with ESMTPSA id du9-20020a0568703a0900b001b3d67934e9sm1286128oab.26.2023.07.11.12.07.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 12:07:26 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v4 0/6] Use introsort for qsort Date: Tue, 11 Jul 2023 16:07:16 -0300 Message-Id: <20230711190722.4028821-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" The main motivation to use introsort is to make it fully AS-Safe and AC-Safe, with a limited stack size requirement, to remove the use of malloc (which troublesome since it seems some program do longjmp from the callback comparison program), and keep worst-case scenario bounded to O(n*ln(n)) (instead of potentially quadradic as for the quicksort). The implementation does not aim to be the state-of-the-art sort algorithm, rather I used a well understood introsort (used on libstdc++, for instance) and leverage the current quicksort implementation along with a heapsort one from Linux kernel. Performance-wise, the introsort does fall short compare to mergesort [1]. I have not added the benchmark because I see that this should not be focus of this patch. I have added some simple optimization, also used by Linux kernel heapsort. [1] https://sourceware.org/pipermail/libc-alpha/2021-September/130698.html Adhemerval Zanella (6): stdlib: Optimization qsort{_r} swap implementation (BZ 19305) stdlib: Move insertion sort out qsort stdlib: qsort: Move some macros to inline function stdlib: Implement introsort with qsort stdlib: Remove use of mergesort on qsort (BZ 21719) stdlib: Add more qsort{_r} coverage manual/argp.texi | 2 +- manual/locale.texi | 3 +- manual/search.texi | 7 +- stdlib/Makefile | 3 +- stdlib/msort.c | 309 ---------------------------------------- stdlib/qsort.c | 338 ++++++++++++++++++++++++++++++++++---------- stdlib/tst-qsort3.c | 298 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 566 insertions(+), 394 deletions(-) delete mode 100644 stdlib/msort.c create mode 100644 stdlib/tst-qsort3.c