From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS17314 8.43.84.0/22 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 8BE691F8C6 for ; Mon, 6 Sep 2021 20:51:50 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B0DE8385AC3F for ; Mon, 6 Sep 2021 20:51:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0DE8385AC3F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630961509; bh=JuTiloOMyzbhpfuRJ/Z8pSXcH57UIaLwZlw4YwBEzlg=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Zl1uNBkRrMcOr7JzvOHWWmoVvSg+1rtQtupkjjD53FiIZQYiJCw3dMw1WdPyoP9Gi gGwDz9C6K7L6v26FwSRjMBM3bIKIkvoS+zKViCujQe/zyfatpZLxXzgeI+MMSqepwv Zdw2Y+9di1AwNhyaLh1szvAJ2tWEC9fD/sP9KfJ4= Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 0E3D5385BF9C for ; Mon, 6 Sep 2021 20:48:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0E3D5385BF9C Received: by mail-pl1-x62d.google.com with SMTP id v1so4493279plo.10 for ; Mon, 06 Sep 2021 13:48:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=JuTiloOMyzbhpfuRJ/Z8pSXcH57UIaLwZlw4YwBEzlg=; b=n4QKbfWqZ6ZxNeVNwZ8dffFdVNuQR08Ig31SaPgzJvy8HWXKybyY6gPR7FdXJS71V7 JXgusZO+oiIQA6ZLysrWu0dJW4EK8ub1tHsZ386kH7MazS1O1GBxTi3etBvxCyIEPnDV ge/MjOktKX6zFDpvZh+SMhxNMb7uDruMqnCIE0NlfGLlJvioegQmmVCZpbt1shtFNx/y xLbzx1xbbPqkl5yGdLyHQUkqwn5QcLKaNbARgB1vhQ2sMYVckmJXi2CseJ1nzzKMOOE9 e3ptwP4DgAn5osuOUKFzQp5jV360yAqvhmAbHDu/9UOghwq2eRnCGzaHTRKPfQmAIRU4 YSSg== X-Gm-Message-State: AOAM531EIXRI3rwf94Zdryzp6P9A9gGe58DYqtGQzF8p2ZCL0VWLG3AG u8vzQQB7F8ld+8vrbqWPmeCnHg== X-Google-Smtp-Source: ABdhPJyOpD4gN1qVKbxdjfRweDAG+xRDDJjL0Wde1WfU/GxjENJjFvJWBangjCseMYVvM2BAwauSSw== X-Received: by 2002:a17:902:ea07:b0:136:25e:298d with SMTP id s7-20020a170902ea0700b00136025e298dmr12132934plg.25.1630961284948; Mon, 06 Sep 2021 13:48:04 -0700 (PDT) Received: from google.com ([2620:15c:2ce:200:5e58:ee8:7bca:ad2e]) by smtp.gmail.com with ESMTPSA id z9sm8314511pfa.2.2021.09.06.13.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Sep 2021 13:48:04 -0700 (PDT) Date: Mon, 6 Sep 2021 13:48:01 -0700 To: Adhemerval Zanella Subject: Re: [PATCH v3 4/7] stdlib: Move insertion sort out qsort Message-ID: <20210906204801.rwwf7azfmo7dis24@google.com> References: <20210903171144.952737-1-adhemerval.zanella@linaro.org> <20210903171144.952737-5-adhemerval.zanella@linaro.org> <20210906203514.oazjrmwr4cksgsc3@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20210906203514.oazjrmwr4cksgsc3@google.com> 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: , From: Fangrui Song via Libc-alpha Reply-To: Fangrui Song Cc: libc-alpha@sourceware.org Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" On 2021-09-06, Fangrui Song wrote: >On 2021-09-03, Adhemerval Zanella via Libc-alpha wrote: >>--- >>stdlib/qsort.c | 100 ++++++++++++++++++++++++++----------------------- >>1 file changed, 53 insertions(+), 47 deletions(-) >> >>diff --git a/stdlib/qsort.c b/stdlib/qsort.c >>index 59458d151b..b69417dedd 100644 >>--- a/stdlib/qsort.c >>+++ b/stdlib/qsort.c >>@@ -150,6 +150,58 @@ typedef struct >> smaller partition. This *guarantees* no more than log (total_elems) >> stack size is needed (actually O(1) in this case)! */ >> >>+static void >>+insertion_sort (void *const pbase, size_t total_elems, size_t size, >>+ swap_func_t swap_func, >>+ __compar_d_fn_t cmp, void *arg) >>+{ >>+ char *base_ptr = (char *) pbase; >>+ char *const end_ptr = &base_ptr[size * (total_elems - 1)]; >>+ char *tmp_ptr = base_ptr; >>+#define min(x, y) ((x) < (y) ? (x) : (y)) >>+ const size_t max_thresh = MAX_THRESH * size; > >But I think MAX_THRESH being 4 is unfortunate. >All modern architectures want a value larger than 4 :) > >Reviewed-by: Fangrui Song > >>+ char *thresh = min(end_ptr, base_ptr + max_thresh); >>+ char *run_ptr; >>+ >>+ /* Find smallest element in first threshold and place it at the >>+ array's beginning. This is the smallest array element, >>+ and the operation speeds up insertion sort's inner loop. */ >>+ >>+ for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size) >>+ if (cmp (run_ptr, tmp_ptr, arg) < 0) >>+ tmp_ptr = run_ptr; >>+ >>+ if (tmp_ptr != base_ptr) >>+ do_swap (tmp_ptr, base_ptr, size, swap_func); >>+ >>+ /* Insertion sort, running from left-hand-side up to right-hand-side. */ >>+ >>+ run_ptr = base_ptr + size; >>+ while ((run_ptr += size) <= end_ptr) >>+ { >>+ tmp_ptr = run_ptr - size; >>+ while (cmp (run_ptr, tmp_ptr, arg) < 0) >>+ tmp_ptr -= size; >>+ >>+ tmp_ptr += size; >>+ if (tmp_ptr != run_ptr) >>+ { >>+ char *trav; >>+ >>+ trav = run_ptr + size; >>+ while (--trav >= run_ptr) >>+ { >>+ char c = *trav; >>+ char *hi, *lo; >>+ >>+ for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo) >>+ *hi = *lo; >>+ *hi = c; >>+ } > >The bytewise move is a bit unfortunate and may slow down the insertion sort >quite a bit... But without allocation or code duplication I don't know a >better approach... If we want to optimize insertion sort for the common case, perhaps also optimize the cases when the element size is <= SWAP_GENERIC_SIZE. Use an unsigned char tmp[SWAP_GENERIC_SIZE]; as you do in another patch. There will be a bit code bloat, though... > >>+ } >>+ } >>+} >>+ >>void >>_quicksort (void *const pbase, size_t total_elems, size_t size, >> __compar_d_fn_t cmp, void *arg) >>@@ -272,51 +324,5 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, >> for partitions below MAX_THRESH size. BASE_PTR points to the beginning >> of the array to sort, and END_PTR points at the very last element in >> the array (*not* one beyond it!). */ >>- >>-#define min(x, y) ((x) < (y) ? (x) : (y)) >>- >>- { >>- char *const end_ptr = &base_ptr[size * (total_elems - 1)]; >>- char *tmp_ptr = base_ptr; >>- char *thresh = min(end_ptr, base_ptr + max_thresh); >>- char *run_ptr; >>- >>- /* Find smallest element in first threshold and place it at the >>- array's beginning. This is the smallest array element, >>- and the operation speeds up insertion sort's inner loop. */ >>- >>- for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size) >>- if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0) >>- tmp_ptr = run_ptr; >>- >>- if (tmp_ptr != base_ptr) >>- do_swap (tmp_ptr, base_ptr, size, swap_func); >>- >>- /* Insertion sort, running from left-hand-side up to right-hand-side. */ >>- >>- run_ptr = base_ptr + size; >>- while ((run_ptr += size) <= end_ptr) >>- { >>- tmp_ptr = run_ptr - size; >>- while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0) >>- tmp_ptr -= size; >>- >>- tmp_ptr += size; >>- if (tmp_ptr != run_ptr) >>- { >>- char *trav; >>- >>- trav = run_ptr + size; >>- while (--trav >= run_ptr) >>- { >>- char c = *trav; >>- char *hi, *lo; >>- >>- for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo) >>- *hi = *lo; >>- *hi = c; >>- } >>- } >>- } >>- } >>+ insertion_sort (pbase, total_elems, size, swap_func, cmp, arg); >>} >>-- >>2.30.2 >>