Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7351242rdb; Wed, 3 Jan 2024 12:53:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnvp8ISbgoDdMPuo2w7t0ShJpWz6RVlGB+A6aAEkgx221V40najWukjiX6qMJRnHPel6ZJ X-Received: by 2002:a05:6830:4799:b0:6db:9adc:fc9c with SMTP id df25-20020a056830479900b006db9adcfc9cmr10809041otb.55.1704315223604; Wed, 03 Jan 2024 12:53:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704315223; cv=none; d=google.com; s=arc-20160816; b=cq/20unfuswfsVPzMx4A8aS9Kd+C/r4VkMwvQnxcEw8YOWoqtPOPYwzk/3pSo+pvWN nwYHKK+9oHx0uY1OxRdKjuMtuJ7bzvDSQU4Hwe1c2U5S7YyEvyrC4hKLzPAoVD1K87QD TQ3+e9ksxcZ2kAqrkNmQqv5qg9Dth6PXjQyoXWN/wmfDC/pFv9wZWFd6u0g2J6PzFNnb E1SUkP0ou1Hv3dIMFBjEdALMq/m/VHP+HBU6wyTHOyVTa59ITDBdI1G6D1qejtUicfRl dQWrCqBMNvZjIrWdUIfUGcnVYQ0lm61NpN1owOCmOblQvO4Nbf9+OLFIJrMnpi4SaRVr tj5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=iYaz82mYYzCwX1xxNWNg9eztYs/P62dbK5BPPUgSW7A=; fh=8W+doEkBJdMCDUJx28+9khJFbLVTBYSnRkl/X7Exs6k=; b=hXjWErvyNTl0jMlJ+ML5oR/yU9jcATOQDx/REGqR0SlsLvXIqPtBcfh1jS2v/zJFoR akizBU/eEDYOoMBvdIihjgfDk5dj8Ni9nlkqvF1yRCcEZ5zf1gwMe2uErh1msoyLGRxc UshhSPjenhp6nb9f+ZnT5oanTAJEFb+ZC/LXelqeNt1bIW3/p1wq6aLaoxHfB4BK9Emq TLe0oC3WKI9y9r9GgU/z48v9xyTooh4G1KiyxVUsl+m4JSMaN+wy3YoFt4+zl+oSL02j 1osZ6yb4NWbS/WylRremeIQl+GHDB+Kb05nN4VBep1TBmgg/bha6SGLhi5tbL2/dT519 ffPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Tdp2Crew; spf=pass (google.com: domain of linux-kernel+bounces-15998-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15998-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id cq6-20020a05622a424600b004283f8e3628si130939qtb.611.2024.01.03.12.53.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 12:53:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15998-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Tdp2Crew; spf=pass (google.com: domain of linux-kernel+bounces-15998-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15998-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5CA441C2457D for ; Wed, 3 Jan 2024 20:53:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6445D1DA3A; Wed, 3 Jan 2024 20:53:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Tdp2Crew" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 595951DA22 for ; Wed, 3 Jan 2024 20:53:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d437a2a4c7so18534505ad.0 for ; Wed, 03 Jan 2024 12:53:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704315196; x=1704919996; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iYaz82mYYzCwX1xxNWNg9eztYs/P62dbK5BPPUgSW7A=; b=Tdp2CrewIbAHRUk4n+pBaxA2OFdUjwdwTw1vpI+fcGI6ktQiHXE3HLoHfByhTt7B9J Qff2O1niNjssRiv3LwLPFONX5rw0/8s6huYLgTzsWeutUJAKrUFFKTJFTX/sX6W3WSGw Cg9e/4ZRjyQK+toqfvcBi4DmVJDc8hbV7woOVSXRgawHKT1dHmY44mCRMkWiYhWlPMue MsoeFd2TeaqQKJ6puVGW1qJvev6YxuPWmxJSnMEZkDuTR6iZRNtvfIo7GOVLu7lro84X LNdbGbdIYQ0Easb0ihtzpWVwlairqqsRErHxigR9bIihssL1cwQS0ilt24s9aYj/SGrH vaWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704315196; x=1704919996; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iYaz82mYYzCwX1xxNWNg9eztYs/P62dbK5BPPUgSW7A=; b=VAkxe9MQ4TwzDZoKSGHb2DWD4UrbtBu7i8nYIcbSBXgXgRkmat+hCObNsnJhj51+a4 iqw1aBehBkj0oTQLZHJ6lc+yEw7Nu28NXTszS6k+3nIzZcSleC3hOlyUZs1WUCJhL32k uUkkPxgfdkPYuDSENsY4xhcPFC0UNViZ12pArl1JOb0WNXZEF/Wa3UsFVHuL14pWZv9d 3Tsuvoc/ZhGEcQ68gsUKumGJFKrxJ6wUMVwiuxsqX8llzzR6OPw5uqsksE6AtOzqvPYu PbusnHCUQEYpdUJJGEpIzoPQuQfuoSu6VIGU1W+sxPuxnVXTo7MAb2Z8R+hC80M4A1qS SOKA== X-Gm-Message-State: AOJu0YzjX/G4qJ8iswcYWvYIdBV9C8vPAYBsNe3veBwTgVZ++/0id2qk 8INqnTvQoSEqSA0wGlD2JMU= X-Received: by 2002:a17:903:18b:b0:1d3:9a7d:a39b with SMTP id z11-20020a170903018b00b001d39a7da39bmr21061978plg.5.1704315195665; Wed, 03 Jan 2024 12:53:15 -0800 (PST) Received: from localhost.localdomain ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id i17-20020a17090332d100b001d47d37d361sm13900434plr.154.2024.01.03.12.53.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 12:53:15 -0800 (PST) From: Kuan-Wei Chiu To: akpm@linux-foundation.org Cc: irogers@google.com, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 2/2] lib min_heap: Optimize number of comparisons in min_heapify() Date: Thu, 4 Jan 2024 04:52:59 +0800 Message-Id: <20240103205259.2108410-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240103205259.2108410-1-visitorckw@gmail.com> References: <20240103205259.2108410-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Optimize the min_heapify() function, resulting in a significant reduction of approximately 50% in the number of comparisons for large random inputs, while maintaining identical results. The current implementation performs two comparisons per level to identify the minimum among three elements. In contrast, the proposed bottom-up variation uses only one comparison per level to assess two children until reaching the leaves. Then, it sifts up until the correct position is determined. Typically, the process of sifting down proceeds to the leaf level, resulting in O(1) secondary comparisons instead of log2(n). This optimization significantly reduces the number of costly indirect function calls and improves overall performance. Signed-off-by: Kuan-Wei Chiu --- include/linux/min_heap.h | 42 +++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index 18a581310eb3..d52daf45861b 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -35,31 +35,33 @@ static __always_inline void min_heapify(struct min_heap *heap, int pos, const struct min_heap_callbacks *func) { - void *left, *right, *parent, *smallest; + void *left, *right; void *data = heap->data; + void *root = data + pos * func->elem_size; + int i = pos, j; + /* Find the sift-down path all the way to the leaves. */ for (;;) { - if (pos * 2 + 1 >= heap->nr) + if (i * 2 + 2 >= heap->nr) break; + left = data + (i * 2 + 1) * func->elem_size; + right = data + (i * 2 + 2) * func->elem_size; + i = func->less(left, right) ? i * 2 + 1 : i * 2 + 2; + } - left = data + ((pos * 2 + 1) * func->elem_size); - parent = data + (pos * func->elem_size); - smallest = parent; - if (func->less(left, smallest)) - smallest = left; - - if (pos * 2 + 2 < heap->nr) { - right = data + ((pos * 2 + 2) * func->elem_size); - if (func->less(right, smallest)) - smallest = right; - } - if (smallest == parent) - break; - func->swp(smallest, parent); - if (smallest == left) - pos = (pos * 2) + 1; - else - pos = (pos * 2) + 2; + /* Special case for the last leaf with no sibling. */ + if (i * 2 + 2 == heap->nr) + i = i * 2 + 1; + + /* Backtrack to the correct location. */ + while (i != pos && func->less(root, data + i * func->elem_size)) + i = (i - 1) / 2; + + /* Shift the element into its correct place. */ + j = i; + while (i != pos) { + i = (i - 1) / 2; + func->swp(data + i * func->elem_size, data + j * func->elem_size); } } -- 2.25.1