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 X-Patchwork-Id: 56132 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 29763385CC82 for ; Thu, 14 Sep 2023 18:37:24 +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-Google-Smtp-Source: AGHT+IGjRr99v+T7yHSGjnIgH6r8hSxY0Mximx0l7rVbNycsh7Rl7c0O/GVm6ZQ9sPrWt5iKsclGGw== 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+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 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