From patchwork Tue Jul 18 14:15:37 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: 1809307 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=Aq/WhhSl; 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 4R51G10vvWz20FX for ; Wed, 19 Jul 2023 00:16:08 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B33BE385C017 for ; Tue, 18 Jul 2023 14:16:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B33BE385C017 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689689766; bh=I5sB6T3WnBQ4QIGdv4yvbfiQFBo/OvMtyChquMPSSR8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Aq/WhhSla7S6lICwhEz+JkDjV/4tqHhjnpAaes+ZS9BpBNKhQUJHygL8aDIa+OcoE rCdDrRjzNorRXbDgIkwOZEZ3Fy289idDXmzvS5sO3/oG5LdqqSj6RkX+xVEACsjK2y 5TIc7UFDPgJ9Rz1MU9z6ershO9BqZ/FdeMbLc70Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc2a.google.com (mail-oo1-xc2a.google.com [IPv6:2607:f8b0:4864:20::c2a]) by sourceware.org (Postfix) with ESMTPS id CD34538582B0 for ; Tue, 18 Jul 2023 14:15:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CD34538582B0 Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-56597d949b1so3202793eaf.1 for ; Tue, 18 Jul 2023 07:15:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689689749; x=1690294549; 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=I5sB6T3WnBQ4QIGdv4yvbfiQFBo/OvMtyChquMPSSR8=; b=RUwzngwb9+kBWvNhvcHDrm1BfW1HePIHygBDI8GM+MoDofKwpRAHMo9MLkgFWn0Ivj oPG1A4kyuzj56qCDVrIB3MmkmRffUukUod/rSbAzd15BPq2PHZlb1T14B3XQEiFpQWEo G8hl8Odqo3/KCUvLuZVN2bF5spuiVeDMBCZT7QwwnZjquSHl62h3cN3t0U2irYhXzsgR dQ5f7H0m3H8BedhyWWBKjE08GLtZCKciJ1q1KXE4VzSg9lNG4kjCruj4uffD6zsGGS2P 1ivTDdGJQzPOUiYPUTkUipKV17Ebsa98bvdCfsNryBt0DAzzSVC9K0S51Fzej3QvYDWs m2Jg== X-Gm-Message-State: ABy/qLZnJf6se60BkQ5NdxzHq+YZQC6iEj5t7vKnkE3M4zXA6bvz3LuI WmC0fSixF5O95TD3FJRfYmN0uE+kpCjKVcNNNPdO5g== X-Google-Smtp-Source: APBJJlHzMu8AGuPV2AoFS0HG7ZeiPqJib9JN0UqKi6mTlypM02NDR0hXA6OuBEckKXs6UrBEZFxAUg== X-Received: by 2002:a4a:621e:0:b0:566:f9ff:57f with SMTP id x30-20020a4a621e000000b00566f9ff057fmr6441676ooc.8.1689689749469; Tue, 18 Jul 2023 07:15:49 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:5656:745f:3154:510d:b668]) by smtp.gmail.com with ESMTPSA id 188-20020a4a11c5000000b00565fcfabab8sm847843ooc.21.2023.07.18.07.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 07:15:48 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert , Alexander Monakov Subject: [PATCH v6 0/6] Use introsort for qsort Date: Tue, 18 Jul 2023 11:15:37 -0300 Message-Id: <20230718141543.3925254-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-5.4 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. Changes from v5: - Rewrite heapsort to a custom implementation. - Use may_alias attribute on swap optimization. Changes from v4 - Use enum for swap function selection. - Simplify is_aligned. - Renamed insertsort. [1] https://sourceware.org/pipermail/libc-alpha/2021-September/130698.html Adhemerval Zanella (6): stdlib: Optimization qsort{_r} swap implementation stdlib: Move insertion sort out qsort stdlib: qsort: Move some macros to inline function stdlib: Implement introsort for qsort (BZ 19305) stdlib: Remove use of mergesort on qsort (BZ 21719) stdlib: Add more qsort{_r} coverage include/stdlib.h | 2 - manual/argp.texi | 2 +- manual/locale.texi | 3 +- manual/search.texi | 7 +- stdlib/Makefile | 3 +- stdlib/msort.c | 309 ---------------------------------------- stdlib/qsort.c | 337 +++++++++++++++++++++++++++++++++----------- stdlib/tst-qsort3.c | 298 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 562 insertions(+), 399 deletions(-) delete mode 100644 stdlib/msort.c create mode 100644 stdlib/tst-qsort3.c