Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 BF7B529415 for ; Sun, 19 Jan 2025 06:24:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737267858; cv=none; b=IdUaxGiXnck5zGPCXiyrLnX59if8zZRIpHKLtdSbV8jWm3jOUYUW6hXQGYJZadz/DiPbVffbp+esalbqjTy0wljyzguZf3Qgw4wvG4CUcrL1v47fsiv+3mz/QDGfgee5hJGRXpKqWN+wKf/zXFEmZ3HwpOQHbKvpF6RWijx34gs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737267858; c=relaxed/simple; bh=9/TaqRsSYEKR7NjpO4QTG1a+JJsoU/04gC4U4nj7QoE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=E7cxa/8MVA8rGOhBW68CBPvyGqlUotK3i8yGZc4frBlyDfjWco5go3X9WvfaewtK6CBDB8jKr1wLIrUkqKu96XaU88Yr4gVly7A9DTFsuFYv09H2+ZU5fEcwAKp8hBBatZwLuyChXmRL1/J3R9D6kJjNqi/hKYMZNs2kiylyFwU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ivbzEOmF; arc=none smtp.client-ip=209.85.214.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ivbzEOmF" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-21c2f1b610dso53582435ad.0 for ; Sat, 18 Jan 2025 22:24:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737267856; x=1737872656; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TmfoBw76mkvQJmdrC5kOE8VbXiTmLIDkl1CDUQ7fAXg=; b=ivbzEOmFum4tONr3W4sH1oWZYkvZGLgZabbCDGOovqbj8wScvNj8Ry3sI8pETKbRSx IlAcOlEmPp4lA6vVH2uWjA7ZFSSQQ/istW8hRknLz9+AOdCyQLGsvWShueL+EHL22UHR VXn/DWadRtx/mFeZnuU+b1pyREob9EtSJ5PKkOJ0RDKH+X/RgQhS4vXHGZ7Nvs4PcEj/ y1d8eT1eBhmptI6cdve3NkWZIXOHZUKUkSQifttztyimrsyOLQFd05s+08g4npo/2CfO +Y/dVq+XSWK2HQ56IXnGixDqecMI+9y9zM97ZG2zL1h29+Nq71BUR2N7F+GtsIYa+GJj ZMsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737267856; x=1737872656; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TmfoBw76mkvQJmdrC5kOE8VbXiTmLIDkl1CDUQ7fAXg=; b=PC258xKS8fehlx23CmQRegh7SuEIayTB+t8PtdLtRH1ZSmo+3Gbqa8LqVxuouDm4Qy zKwLJz/mZcqri9/VcHvkhxA8YNsRIEimyz0HtLdreeQbSUsedRuEAbSlcALhNexZZZ+G 7tU0rnzhhfczuGvkeGjJMTYqgACmdl58Q2NA0sPPIkguKXf2QGpoINj2aj4CEPmy9K+C ngtRxOKBBy2fgAioMalygUURww5ju+oWdOcUatd6dhM7tdVxU0yHt/qpYvcry80xqjqe elD9rpELVjzoakRC8u2/SYjMGPocJPnEe9SLgLs4K1ksd5QI+XObE73/jWNoYjaEnbBN ZrFg== X-Forwarded-Encrypted: i=1; AJvYcCVnL6n2qhQsqd8KCty6UHPNISR8ejQYOKcpN04Tw9gIukFVAVswCmaruHdJnuib44A7tWIf3ZjJEtRMfdo=@vger.kernel.org X-Gm-Message-State: AOJu0YxOianOvTEvEoz+QRpU5kWcwZwbpUiW4zQSlRZsn5f/MlSjnBMA qOFKB2+UegB5OS618awRJTGtUYT3Qwkn/mepWFeuRUtwvk4MpO4G X-Gm-Gg: ASbGncvOVIpYccUio7QDty6yZveZSLvvdN430InK1s4gzHK9uY2/zCg2pbjVSUojrSO y665z8CBagsgkqoHt7AFXsi88dgk8VuKLq5BQRdq8gz+oZ9aZFCjLNpe6CFEuX6bogNG4v0IVkQ +cSuiB0Fc5qbAKYnug3FNo7dUe8UgX7KYzFtQUpWul+R43qli4B7vQDg/dEl2R5GQy66N+eG373 mBzY7xH8MT4jRU2EHB/dF1mfWGas9Do4xYYioWSl579c73K8R2W0oJmCtFJmAkeQ4zm4Ja10urC zIq57x89v+YqJQ== X-Google-Smtp-Source: AGHT+IECKHOMk5I3xL1Hk/LJ5+kGh/YP1HMY+iF7YLX82ZOAsL5pAUrW0nW7C7kyC6/onorg+fMiwg== X-Received: by 2002:a05:6a20:734e:b0:1e1:ac4f:d322 with SMTP id adf61e73a8af0-1eb214a0817mr12829275637.14.1737267855805; Sat, 18 Jan 2025 22:24:15 -0800 (PST) Received: from vaxr-BM6660-BM6360.. ([2001:288:7001:2703:7422:aef9:8d07:195b]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72dab9c8e7esm4778259b3a.98.2025.01.18.22.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jan 2025 22:24:15 -0800 (PST) From: I Hsin Cheng To: akpm@linux-foundation.org Cc: jserv@ccns.ncku.edu.tw, linux-kernel@vger.kernel.org, I Hsin Cheng Subject: [PATCH] lib/plist.c: Add shortcut for plist_requeue() Date: Sun, 19 Jan 2025 14:24:08 +0800 Message-ID: <20250119062408.77638-1-richard120310@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In the operation of plist_requeue(), "node" is deleted from the list before queueing it back to the list again, which involves looping to find the tail of same-prio entries. If "node" is the head of same-prio entries which means its prio_list is on the priority list, then "node_next" can be retrieve immediately by the next entry of prio_list, instead of looping nodes on node_list. The shortcut implementation can benefit plist_requeue() regarding to the following test[1], and the test result is shown in the following table. One can observe from the test result that when the number of nodes of same-prio entries is smaller, then the probability of hitting the shortcut can be bigger, thus the benefit can be more significant. While it tends to behave almost the same for long same-prio entries, since the probability of taking the shortcut is much smaller. ----------------------------------------------------------------------- | Test size | 200 | 400 | 600 | 800 | 1000 | ----------------------------------------------------------------------- | new_plist_requeue | 271521| 1007913| 2148033| 4346792| 12200940| ----------------------------------------------------------------------- | old_plist_requeue | 301395| 1105544| 2488301| 4632980| 12217275| ----------------------------------------------------------------------- Signed-off-by: I Hsin Cheng --- [1]: The test is done on x86_64 architecture with v6.9 kernel and Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz. Test script( executed in kernel module mode ): int init_module(void) { unsigned int test_data[test_size]; /* Split the list into 10 different priority * , when test_size is larger, the number of * nodes within each priority is larger. */ for (i = 0; i < ARRAY_SIZE(test_data); i++) { test_data[i] = i % 10; } ktime_t start, end, time_elapsed = 0; plist_head_init(&test_head_local); for (i = 0; i < ARRAY_SIZE(test_node_local); i++) { plist_node_init(test_node_local + i, 0); test_node_local[i].prio = test_data[i]; } for (i = 0; i < ARRAY_SIZE(test_node_local); i++) { if (plist_node_empty(test_node_local + i)) { plist_add(test_node_local + i, &test_head_local); } } for (i = 0; i < ARRAY_SIZE(test_node_local); i += 1) { start = ktime_get(); plist_requeue(test_node_local + i, &test_head_local); end = ktime_get(); time_elapsed += (end - start); } pr_info("plist_requeue() elapsed time : %lld, size %d\n", time_elapsed, test_size); return 0; } lib/plist.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/plist.c b/lib/plist.c index c6bce1226..5071b1eb2 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -171,12 +171,23 @@ void plist_requeue(struct plist_node *node, struct plist_head *head) plist_del(node, head); + /* After plist_del(), iter is the replacement of node + * , if node was on prio_list, then take shortcut to + * find node_next instead of looping. + */ + if (!list_empty(&iter->prio_list)) { + iter = list_entry(iter->prio_list.next, struct plist_node, prio_list); + node_next = &iter->node_list; + goto queue; + } + plist_for_each_continue(iter, head) { if (node->prio != iter->prio) { node_next = &iter->node_list; break; } } +queue: list_add_tail(&node->node_list, node_next); plist_check_head(head); -- 2.43.0