From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 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-ot1-x330.google.com with SMTP id 46e09a7af769-6b7279544edso612831a34.0 for ; Thu, 13 Jul 2023 06:25:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1689254743; x=1691846743; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=8XuWPNMwND8jUwvFUsHoN7pTe9slr2+oeac9/xuzdYY=; b=byrImU/z1DwjwUOvh3XGJv7E0V/wRfo2V1PYq4yPJQCHHaRdiUcVn80y+AfuAL3EU2 /TrF+V3N4zQ4qyJbRIRAvEiaknhpZytVbJx9/xYMQS/D1ODt/tZOlUWs85L1hAhxK3yj 5GmC17mSan4W89RlP/GLTWGxKw6QuscIaIcg3/D2Ae1AkRXQw2mnxIVDK1/Jq/Ekb+ol MOehiXjZGUVQ/IIITQgfpSNLk6b32guLWEjUWEf15ccW/yymaf6UTeLVUjxZyBUifOlw JawUUHImj8e1C8QJO6adjfNZUVv/jE0OX8+makMKcxdGRW5XNUDEfw4hI7ihWtdsk2TK lyfA== 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) From: Adhemerval Zanella 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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 -- 2.34.1