From patchwork Thu Jul 13 13:25:34 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: 1807305 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=KOcOnNcw; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1wNX6sTfz20bh for ; Thu, 13 Jul 2023 23:26:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0965B3857726 for ; Thu, 13 Jul 2023 13:26:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0965B3857726 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689254761; bh=8XuWPNMwND8jUwvFUsHoN7pTe9slr2+oeac9/xuzdYY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KOcOnNcwU0VeIGWg/NqsiMi3JHr2ksxZtcXx91W0JI5bX/UgY6/70gHWca7D5CYpY 2O5WBTFRd+PoixbbQh63ydQy4eIYrIlext6158FR8F3y33hZ/Lmn6UWeAPxM5Syn/6 wyV+7FVkIVo8v1YVMo6SQNyHIi+kB2c1LPn5d+jg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by sourceware.org (Postfix) with ESMTPS id 1B47C3858D32 for ; Thu, 13 Jul 2023 13:25:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B47C3858D32 Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6b7279544edso612831a34.0 for ; Thu, 13 Jul 2023 06:25:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689254743; x=1691846743; 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=8XuWPNMwND8jUwvFUsHoN7pTe9slr2+oeac9/xuzdYY=; b=aZ0uHTQFJMf8W/sJbajACGOY+eNV9P4Nx5ORX/T4fK86rFTVSUZsQuB9XirrIbdPN1 Xi1bwW/DNScKxUefomUisv/imgOvwXf6cNiQtRS2gHbd3id3oE25m9rgPvEXKxCqnGBW SGeDfWTse48nd9vA6NlrONDJNQw22PNrRE3klBj5RPzn6fXfit2uGUXSm6u/22ViK7IK i0naNO2rRRr05qxVrlocYry7ogoIN43XD5x71O3GDaznJTLwzz1vyP/ou8nYkmBVr1H3 pwSDS1+dr8IpNA4OLAIwO8cqWmkMJdpuO7g6JlLACosXPSOXDZ4zyR8aVmACR+kEnyXL wt9Q== X-Gm-Message-State: ABy/qLbGZG6CQPTitiDuda+mysIf6pY9Eb0bste3u8VcNKL1neh3FTkz kNoMdypMGbMBe3KLUk5c/3rDiNKnckXmcyJiCf1dog== X-Google-Smtp-Source: APBJJlFiOg5TiXRlbfTqxgZJz1Je6oaH8ZqoJEx/z2c57pQKMjuFDujYP3Tj97keoYgY8u7JGpG4Dg== X-Received: by 2002:a05:6870:7aa:b0:1b0:833:597a with SMTP id en42-20020a05687007aa00b001b00833597amr2237018oab.20.1689254743684; Thu, 13 Jul 2023 06:25:43 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:5656:8d9b:78e7:d879:79cf]) by smtp.gmail.com with ESMTPSA id d195-20020a4a52cc000000b00541fbbbcd31sm2888856oob.5.2023.07.13.06.25.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 06:25:43 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 0/6] Use introsort for qsort Date: Thu, 13 Jul 2023 10:25:34 -0300 Message-Id: <20230713132540.2854320-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, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" The main motivation to use introsort is to make it fully AS-Safe and AC-Safe, with a limited stack size requirement, to remove the use of malloc (which troublesome since it seems some program 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, rather I used a well understood introsort (used on libstdc++, for instance) and leverage the current quicksort implementation along with a heapsort one from Linux kernel. Performance-wise, the introsort does fall short compare to mergesort [1]. I have not added the benchmark because I see that this should not be focus of this patch. I have added some simple optimization, also used by Linux kernel heapsort. 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 (6): stdlib: Optimization qsort{_r} swap implementation stdlib: Move insertion sort out qsort stdlib: qsort: Move some macros to inline function stdlib: Implement introsort with 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 | 337 +++++++++++++++++++++++++++++++++----------- stdlib/tst-qsort3.c | 298 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 562 insertions(+), 399 deletions(-) delete mode 100644 stdlib/msort.c create mode 100644 stdlib/tst-qsort3.c