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: 56194 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A893385B528 for ; Tue, 3 Oct 2023 12:23:12 +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-Google-Smtp-Source: AGHT+IHspyYFl7MjRlK69359D5Iuj1hYI4X+EwTCYqWUtfpnl6zIJQcVDNjbB80zOAquzuIgO9/7kg== 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+patchwork=sourceware.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