From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Received: from server2.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 7C6C91F406 for ; Mon, 4 Dec 2023 08:21:09 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZEQlMUCM; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C45C238582A3 for ; Mon, 4 Dec 2023 08:21:08 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7819F38582A3 for ; Mon, 4 Dec 2023 08:20:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7819F38582A3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7819F38582A3 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701678056; cv=none; b=YG0tQJRUlKKlG9TQLJBx0QrqGNj6kEr4fSDJJrGa7vj4yAzSkQ1vy2uKgo2KG4hg25tptin/vJv8BT60WcyPRLzZ56QWGVr8DHvzPxpe2vHzA1h2I/168XcbKDheVBVp1+XZSgEqiZZr0LUifTJnb0VIEXbBHAfC6X1Oha633lc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701678056; c=relaxed/simple; bh=L/p0bmnTQmEqdpwFy+fiib9s/peuQES7kELDUbL+SuU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=js5WevGx1bwuxN1GVqTttV2gfmhChxwBhOULrymMFmG3j7bmUc66bHZgi6/JRQ/UDjubtf2Yz85BQvtEvKEj+3q7egkl50aKfb39CGeGQ1LKOcpjdqdaFLq50399/y21KCkInP/ZuMJaDnlpkXYJGgSSqNotc5OldMmcuTJ7RMM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701678055; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=fm217nWo2wcm+KlqIFUeWoiL0+3EEEqVURs+Cg4UIaY=; b=ZEQlMUCMg7tUYMPtKrliSJsrwVvuWOMunUtAA4rko4u60G8ZjI/1ZCuKnA2/AVa+taBizr avP6QNPQ7biaJPEQb5cAR9iyorPdRveCUFvWt5o+HniLj07cZEnanO2MkR0Hv3p/0xW+DJ fwFD7Q9htTGlfUToA3xGnQB9bdheqIA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-483-WN_41wHYMTKgZQ76burspg-1; Mon, 04 Dec 2023 03:20:53 -0500 X-MC-Unique: WN_41wHYMTKgZQ76burspg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6E211811E7E; Mon, 4 Dec 2023 08:20:53 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.84]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6DA29492BFE; Mon, 4 Dec 2023 08:20:52 +0000 (UTC) From: Florian Weimer To: Kuan-Wei Chiu Cc: libc-alpha@sourceware.org, adhemerval.zanella@linaro.org, goldstein.w.n@gmail.com Subject: Re: [PATCH] stdlib: Optimize number of calls to comparison function References: <20231202214839.2634493-1-visitorckw@gmail.com> Date: Mon, 04 Dec 2023 09:20:50 +0100 In-Reply-To: <20231202214839.2634493-1-visitorckw@gmail.com> (Kuan-Wei Chiu's message of "Sun, 3 Dec 2023 05:48:39 +0800") Message-ID: <87y1eaxtst.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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+e=80x24.org@sourceware.org * Kuan-Wei Chiu: > The current heapsort implementation in the siftdown function follows > the standard textbook version, necessitating two comparisons at each > level. Transitioning to the Bottom-up heapsort version allows us to > decrease the required comparisons to just one per level. On average, > this modification significantly reduces the comparison count from > 2nlog2(n) - 3n + o(n) to nlog2(n) + 0.37*n + o(n). I think the factor in stdlib/tst-qsort5.c needs to be adjusted: /* This is an arbitrary factor which is true for the current implementation across a wide range of sizes. */ TEST_VERIFY (factor <= 4.5); On the other hand, the heapsort code is not really expected to run at all in the current implementation because median-of-three quicksort usually avoids degenerating behavior. If this heapsort variant uses a number of comparisons that is competitive to quicksort, maybe we should use it instead? And use insertion sort for short arrays only. Thanks, Florian