From patchwork Tue Oct 3 12:22:44 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: 728725 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp2112445wrt; Tue, 3 Oct 2023 05:23:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IErV6LQx2l3HI0qIcUlivi3AAWCLk7liQXLJ5DFhfqEW1hBbERnQrhHwyNzNhUt3OmJ1AaQ X-Received: by 2002:a05:6402:12c1:b0:523:38f5:c5ad with SMTP id k1-20020a05640212c100b0052338f5c5admr12454815edx.20.1696335785801; Tue, 03 Oct 2023 05:23:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696335785; cv=none; d=google.com; s=arc-20160816; b=vJrTFZz+eKTJ1tVymgjMdAJhPbHIFwT0M3tHu9qP3F1V4WzHB9LNYdXiQXwJ8w0lEs hYPRe1OBseokp9jhymYR/icacX8xOR8AxNrfdjshuCqk7rnWBpFwROTnx0+f2ygkhf7c 1LwCqj4290N547pVSfTPmein1pTOfvCQISqWf1WzLfmBAHlqUC6lY/TqgAjgiuMBd716 K6ml2Q57DlsSDRPj/uQfGFjVt7GSVlEHZaHtUPCwepiCJupL+bjxLrD4Q3KbMmkA5Q7z mfWlCskcroIX0Cyt+3LyPZcFvnZ+EVI/zhC+LMq1s5HewJSwI0ELtC/Kj3L3YVa7VhQZ FaeA== 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=GJfZpofFnDKVX3fOiTwXneA5UvTOL8gUjy493f+e2VE=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=tce49VISzluaPtJJHysj0V5TCclJsBmocsHwvffGQEGygPq2sjaf4an0+sSNO0hUYN 5GqZTTjgJofseqnehKVFq6WTA8F20VJ0lMz13wDSMYIk8z+J0L2zodmclaY50nSh+6hv Q4Slc9/MYr1TMAsmi33U4/1D6fS2svRdalIPjENX1MV7CSv/2lWe/NWD76MLUHQ96tiW KwWU7v5XFSlcPh6q2BLnYxiv3fYMfM6crgBYEaQ7r/sf/u8PMT5M5oM3rEtCUKDS51DU hNyWd7Nv4j2oD61USqm/biRQOV7ngQZjCOX/aWNC4S9AyfsUA9r5CUkMkwiilkiVovKq ThVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NRYyE3dE; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w22-20020a50fa96000000b005333484e43asi563590edr.93.2023.10.03.05.23.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 05:23:05 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NRYyE3dE; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 82E233857728 for ; Tue, 3 Oct 2023 12:23:04 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id C31713858D28 for ; Tue, 3 Oct 2023 12:22:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C31713858D28 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-pf1-x429.google.com with SMTP id d2e1a72fcca58-690fa0eea3cso663706b3a.0 for ; Tue, 03 Oct 2023 05:22:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696335776; x=1696940576; 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=GJfZpofFnDKVX3fOiTwXneA5UvTOL8gUjy493f+e2VE=; b=NRYyE3dEAdYEXGsKonM69mrtSv7QVxw0TkVuTgLYoEjYu2+xWRcOEHt4jFVrnf7XBt mHF3Oc2G176bjUAZglJw9gilVgpxW56lqh0sPsIyiomFHhkeZrazVA92ouIisAqGNanI qsJe10RyOxZiyZNS1+9Cp2QRYjr3uVlI2vfrHt6+TjdOwPb4rofk97iJY8wmtXDi8OjO V6oQNw3qFnfHluBsAJYCRA2Rva/Z3zdEkYf68mBx2d/iGvoN5aoc6oLBTppwTf2FHWwh S/6VwqDZjZk2D95T0l1D60m4xHMgr6S2kcAD1s9pLFnSa9zYjpG09fBSGt0SBgN8ESD/ 9v1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696335776; x=1696940576; 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=GJfZpofFnDKVX3fOiTwXneA5UvTOL8gUjy493f+e2VE=; b=DFEwKJK++hPlo+6k9q1DDr/CDTcWNVYw8rDVBKRqDo6PkuBuki7p13kgZAzB9lHSRT 0H1JQH0mTMjiVulSwFGxUAjpSP6FFDeRiDjcV9WcL8oS4hV0JIlnbcmZFIk2DM89Xgap L5cOs2AtcaslepRP1iQW6ZGR+TLOMcTG0uQqcWaXUN/hqUhecghpNG9Nm4XMCRHb3BfU 4jkvxKBPqQy5naQm0ssFnzA2nbS7UdZLZu7oVAxtTqBODhdh3LvqxoTWb/wt8aOLnF+v 7xnZ6F1ZpHT7upoc+K3Z255cQJ2HpX4+MhYeOslJsVj75I7tn92Bnx1ro9Xx8EcvHCKZ 0nWQ== X-Gm-Message-State: AOJu0YzGSGoy7E1WxMft81BbEc2vyxTAXTK2C/x3/HzvI7xgL8NEJe0M ZoljUobTkYNkbxKYWbm6/3gPOV5mVs/6MkkLNRDZHw== X-Received: by 2002:a05:6a20:258a:b0:153:56b1:8417 with SMTP id k10-20020a056a20258a00b0015356b18417mr17112430pzd.21.1696335775996; Tue, 03 Oct 2023 05:22:55 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:feaf:31ef:b40c:b4e5:77c]) by smtp.gmail.com with ESMTPSA id b1-20020a170902d30100b001c5de2f1686sm1403881plc.99.2023.10.03.05.22.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 05:22:55 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 0/7] Use introsort for qsort Date: Tue, 3 Oct 2023 09:22:44 -0300 Message-Id: <20231003122251.3325435-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 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 v7: - Move __memswap to a static inline on its own header. - Improve some comments. 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. PS: this is a update for https://sourceware.org/pipermail/libc-alpha/2023-October/151887.html which should be ignored. 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 - manual/argp.texi | 2 +- manual/locale.texi | 3 +- manual/search.texi | 7 +- stdlib/Makefile | 3 +- stdlib/msort.c | 309 -------------------------------- stdlib/qsort.c | 318 +++++++++++++++++++++++++-------- stdlib/tst-qsort3.c | 366 ++++++++++++++++++++++++++++++++++++++ string/Makefile | 12 ++ string/test-memswap.c | 192 ++++++++++++++++++++ sysdeps/generic/memswap.h | 41 +++++ 11 files changed, 856 insertions(+), 399 deletions(-) delete mode 100644 stdlib/msort.c create mode 100644 stdlib/tst-qsort3.c create mode 100644 string/test-memswap.c create mode 100644 sysdeps/generic/memswap.h Reviewed-by: Noah Goldstein