From patchwork Thu Sep 14 18:36:57 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: 722581 Delivered-To: patch@linaro.org Received: by 2002:adf:f0d1:0:b0:31d:da82:a3b4 with SMTP id x17csp551666wro; Thu, 14 Sep 2023 11:37:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7DO1o3JcAuMf2VBibkTw95iijZAE5vuaHlvMznol2h0mKCE2J/Q+jgY/YpBghTYiaGbyy X-Received: by 2002:a05:6402:524c:b0:530:4f4b:9d1a with SMTP id t12-20020a056402524c00b005304f4b9d1amr1422037edd.5.1694716638499; Thu, 14 Sep 2023 11:37:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694716638; cv=none; d=google.com; s=arc-20160816; b=nli76GXNnG8iDRmF+CzLnpp8mcNtXalbVsoGHgkC6KgbBr2n7t6OZEAGxRx+q+ykMC cevqCMt78udKRIks6+rOb7AW6prwraJB6oK2NPvjuErYXoz5O54PhNt/i8X6mDFo1auL DCmVIEmLeJH4il6s0IbNEQDR/BZvFDd44N+/gdChttCPv0YqnHaZGiop3Yavt9RWzWrJ 80dCsf19c5ZNhc5kgj9EQruUozYBZ5zA2vICyngADm1foZh4IAJ3BprJuD2sWhPERWPo kgTEaVn9of8Fjg/TIVhPZKQFGf4g/MLBCZp8kzeqNLJYCxBBw/Y1Jd/5fZS1o3Fl3r40 yELQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:delivered-to; bh=Z+J9I2XhyJahXNL9SjsrXEBUN9xPBpuaZevLFfamdb8=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=N89kbYDwsUQH7CfrPBuVdaWlhqJTXE3+NIfspFor9TfizLDBxqCJKulmxtHLJI2Z9y NDVAwg242s+XqiNEyBsKcGRDnnIbNnufEiwaamoaApI5B9SeHMz31xWWdvfFFTECbBqH QiweGXYlDbj8x34aC8EDCXLrjAW9Zm6SH7szk8TBTIVZQWrzPIS+gT7Lj70oFNX7+uzw 3ESx87klWuy7aDBv1EmQ0KJmbf6ueUdpPhCdFlvO/ZnWD3Qc8OaZ726O+d6eY7R0ht7I 6LByZ3l1ttjZKy1KWIHkZDJxt3yw0UEZnLn0+3MHIF1ReMTtX1xhQwEctAX28XbEMr0J kEWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ninpk9qD; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s22-20020aa7cb16000000b005259d57fb82si1893534edt.3.2023.09.14.11.37.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 11:37:18 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ninpk9qD; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7EE6D3857C66 for ; Thu, 14 Sep 2023 18:37:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by sourceware.org (Postfix) with ESMTPS id 972BF3858CDA for ; Thu, 14 Sep 2023 18:37:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 972BF3858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-1c4cd0f6cb2so703175fac.0 for ; Thu, 14 Sep 2023 11:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694716629; x=1695321429; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=Z+J9I2XhyJahXNL9SjsrXEBUN9xPBpuaZevLFfamdb8=; b=Ninpk9qDHvH8puOZgY1Jr1YlYCk4lO+kzKxuaaENp57nseVC6wm1gVNtuaZe5XOoVA pODzLaAuZMcoCBjJ5V2munknt/eaGCj82Qk03ypUdzDC+tYUptRKBNk1EBU3j3TRtLG4 sS+hM2h3jvmFLHs1FIqHLNKMoDpb4xjNw1XrrGMv3eR8LMgUaeSNYI8aHnI3jEXajsTN rCSh64/CKRqXrmNqkbD6JuwOFEbLIU8rcr9mh829ekb6xfOdCuasAI2UGcQIEywYdRzI tFEn30TPH5/TWkMtF55T14KjIEm2vLowTFgirLlSrGXmbOMyEoukAUEzZyPIW6/rqyX5 dcJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694716629; x=1695321429; 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=Z+J9I2XhyJahXNL9SjsrXEBUN9xPBpuaZevLFfamdb8=; b=wMkKM1AZIuiYmO9Gp/ASJo4jsKFEwK4zGmHXGRLoLL4ME8vIflAqnodZooIM0OZaUy TYwfLSjNywTfvR43aRhbfD9O4JcmkhPoy2Q6S+N3ryjB57KPtb+8S1NvBeJ0eX9c4bzT 2eP7eN8a7x9hylLTnUX+c63E0TAaV/Wdg8fp3TYOE0b7xfutpUBE9hwtRWGm9Ghq98lh D6mMuV8uBPXwOuUuteZbHW7QSGlG3J7qSlIFdwqIf68QPUIefOrPcjAd4E/9YsWgB3lX UkipkCqLnO5ACYr7P4jrJv5VCUfMr/DMAEYPMbllvA+yK+hRmt1A7uDwY/lRwPdbaba1 sq6w== X-Gm-Message-State: AOJu0YxN+1q1mscEY5iB3NcAUysZvkWbfNamPnS/WGT/HdX9nNDRs/7d HXSsCEbmNW3nM9CDjJPqyR+kWQVYxPDJt9pGsMGXZg== X-Received: by 2002:a05:6870:d1cf:b0:1d5:a22d:3020 with SMTP id b15-20020a056870d1cf00b001d5a22d3020mr1850771oac.3.1694716629164; Thu, 14 Sep 2023 11:37:09 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:5173:d70:d8d:4d2c]) by smtp.gmail.com with ESMTPSA id cc6-20020a056871e18600b001b3d67934e9sm1056930oac.26.2023.09.14.11.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 11:37:08 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v7 0/7] Use introsort for qsort Date: Thu, 14 Sep 2023 15:36:57 -0300 Message-Id: <20230914183704.1386534-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.3 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 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org The motivation for using introsort is to make it fully AS-safe and AC-Safe, with a limited stack size requirement, to remove the use of malloc (which is troublesome since it seems some programs 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, instead it uses used a well-understood introsort (used on libstdc++, for instance) and leveraged the current quicksort implementation along with a heapsort one from Linux kernel. Performance-wise, the introsort does fall short compared to the mergesort [1]. I have not added a benchmark because I think this should not be the focus of this patch. Changes from v6: - Added internal __memswap symbol. - Improved tst-qsort3 with a reference implementation and a new duplicated input. 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 (7): string: Add internal memswap implementation 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 - include/string.h | 3 + manual/argp.texi | 2 +- manual/locale.texi | 3 +- manual/search.texi | 7 +- stdlib/Makefile | 3 +- stdlib/msort.c | 309 ----------------------------------- stdlib/qsort.c | 315 +++++++++++++++++++++++++++--------- stdlib/tst-qsort3.c | 366 ++++++++++++++++++++++++++++++++++++++++++ string/Makefile | 13 ++ string/memswap.c | 41 +++++ string/test-memswap.c | 191 ++++++++++++++++++++++ 12 files changed, 856 insertions(+), 399 deletions(-) delete mode 100644 stdlib/msort.c create mode 100644 stdlib/tst-qsort3.c create mode 100644 string/memswap.c create mode 100644 string/test-memswap.c