From patchwork Mon Oct 2 19:33:04 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: 728558 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1819714wrt; Mon, 2 Oct 2023 12:33:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEOZGGCPlEOT5tPJr2Dz10Z44FvW/PwKEnCyQj0ENC8UmV5O1aYZnU3dQk3VOCGZjOxa33V X-Received: by 2002:a17:906:109e:b0:9a6:572f:597f with SMTP id u30-20020a170906109e00b009a6572f597fmr10011556eju.38.1696275204528; Mon, 02 Oct 2023 12:33:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275204; cv=none; d=google.com; s=arc-20160816; b=UKP9d2DXiFAEr+EJ5k6FilVPUMF3DHLDHDsYcqYWPDbHo8yQ0M9vWUa+Igm86DYUCs SvPxyHIYU6gS1G/kSbFP0cZ2h3u7j8DCsj+2Y6ane6a5uQKysf5jDXeReiVFLMjup+Bs SeoMGaKe4LtkCUt8zNWUoOW30gkN3x/Md9rt8Ph9ZW2l42+ic4ludBmUsh30s/xgNev4 7SkYOFYiFN4/KrHtT0bcSW68uJkPiyGiN7zYzO5LUu90NiK05DWfqPF0breF7nwA1Bw3 du4vC/bvxL40ImGqfekDEKAQhMZ7UlSIqvOJHbDKF4p8JF/OuKasArouxVboDDEDUzgi vwIg== 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=Tefp8KSETvBwSp+XNOqAE7XmhBW0dOChPizDCJgskDM=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=Ufk928myG3qQXBcGurwqEjlSbtSRgmJDNd+nfTreHa2hHFpEph04rTvztDlOzSCRpG IXV6Q/CSRq8NCzneUXt8oHMc7ICMFV7cHZkRainL4aFfJXpRomKFDy4PSAWzmPS6cu/G sIPosUCEejX85JktSu2ILzvLChE68I4BjXHERCKVzMgG0ilPgC9gpxcj39wacHBrY/Oq 6mpRrpFkpm1yYTcjI3z162S27lHO1Kwh+CinZk7nRVMNOf9tUmJMbGOrdTmFFfIBTqnl cpT9xzBh1UTcIATfmvvs2whFMgJZDDzCPui88j8SiuerrWeNbP+JmRaeASlQ3h40QamU 43ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tlMCI1Up; 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 fj22-20020a0564022b9600b00533b6b48070si16251931edb.424.2023.10.02.12.33.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:24 -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=tlMCI1Up; 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 7B2C23858D32 for ; Mon, 2 Oct 2023 19:33:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id EDF233858D32 for ; Mon, 2 Oct 2023 19:33:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EDF233858D32 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-pl1-x632.google.com with SMTP id d9443c01a7336-1c6219307b2so1263575ad.1 for ; Mon, 02 Oct 2023 12:33:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275195; x=1696879995; 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=Tefp8KSETvBwSp+XNOqAE7XmhBW0dOChPizDCJgskDM=; b=tlMCI1Up1VGJZl7JgrEBYm2oMon/I/GGjQpRYLKLK6ursS8W5skUaYt+htgicg1weE jg0KVWDkDgS9RIvLcguXba0nIWbIwLL1GVhEg8h7lOKOPNwNbhqdKtFc59DiFUfrslD7 5peZKOt08n7zAL9ddi50+DP+YTA2vmF3QDTU+dYaGWf8HzH2FWhQ136K5tzOZglO75cz WLFLfpLR5J56uYQG3ZVCq7YSgqN7roBbqQrzQjXpmppsdZKuOIR4/O/H3N4iO45wq5A1 JmFkA2frUnNpbv0k9Y5500nLTMwOjVrApWcYQDO7kId/8mX/oHSxr4qjvJ7xgzHjZg1y 7WYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275195; x=1696879995; 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=Tefp8KSETvBwSp+XNOqAE7XmhBW0dOChPizDCJgskDM=; b=SOKeNQWwOPwkACulT56DVY3KiiDKuOrriWwKXWHoq26spKTBW0t9+zApp8GfKXfvRV BFsFwvLogAigwEu74WEOBK20I6s+NbdBT0rfWuZ9e66dyx5YxgFtPA4Zjvmtu72Y2cEQ CpZ/x/4Vrzrp5aRRnCD0W9LCPnUpzMETVyVAV395ykZ18Qi9pDlEe+Q5+SaEUP6811ZG cb5UQfBTu7kUsNmKfCkn+nrjc/V2NmDX4rS9FUnwSBKJrJ1j45dWkfhXExuaic3mTruX 04/xEnlo1f3UeglYS1gblH1TKr3BurA//+K7kcCnZMop0Tu5YB49k/8BZt1wJ9OdCxYT 7ZRg== X-Gm-Message-State: AOJu0Yx//CUzOnf9Jtam2vqFVJVkwus3hr3sz8pH++BtLIPHz3zqvAJk SttZGFfnSu6hUsL/V0+AbadK/z/D0PRWBI5jOvFdiQ== X-Received: by 2002:a17:902:efc4:b0:1c0:e12e:8e39 with SMTP id ja4-20020a170902efc400b001c0e12e8e39mr7772338plb.35.1696275195452; Mon, 02 Oct 2023 12:33:15 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:feaf:8f32:80e:c10a:4836]) by smtp.gmail.com with ESMTPSA id u10-20020a170902b28a00b001c7453fae33sm6982828plr.280.2023.10.02.12.33.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:14 -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: Mon, 2 Oct 2023 16:33:04 -0300 Message-Id: <20231002193311.3985890-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 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. 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