public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tzung-Bi Shih <tzungbi@kernel.org>
To: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: bleung@chromium.org, groeck@chromium.org,
	chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 7/7] platform/chrome: Implement quickselect for median calculation
Date: Fri, 10 Nov 2023 13:57:46 +0800	[thread overview]
Message-ID: <ZU3GWkKczNV-AQA4@google.com> (raw)
In-Reply-To: <20231109185439.1535962-8-visitorckw@gmail.com>

On Fri, Nov 10, 2023 at 02:54:39AM +0800, Kuan-Wei Chiu wrote:
>  /*
>   * cros_ec_sensor_ring_median: Gets median of an array of numbers
>   *
> - * For now it's implemented using an inefficient > O(n) sort then return
> - * the middle element. A more optimal method would be something like
> - * quickselect, but given that n = 64 we can probably live with it in the
> - * name of clarity.
> + * It's implemented using the quickselect algorithm, which achieves an
> + * average time complexity of O(n) the middle element. In the worst case,
> + * the runtime of quickselect could regress to O(n^2). To mitigate this,
> + * algorithms like median-of-medians exist, which can guarantee O(n) even
> + * in the worst case. However, these algorithms come with a higher
> + * overhead and are more complex to implement, making quickselect a
> + * pragmatic choice for our use case.

I am wondering if the patch helps given that n = 64.

>  static s64 cros_ec_sensor_ring_median(s64 *array, size_t length)
>  {
> -	sort(array, length, sizeof(s64), cros_ec_sensor_ring_median_cmp, NULL);
> -	return array[length / 2];
> +	const int k = length / 2;

`k` doesn't help readability.  Could you put `length / 2` to the code inline
or at least give it a better name.

> +	int lo = 0;
> +	int hi = length - 1;
> +
> +	while (lo <= hi) {
> +		int mid = lo + (hi - lo) / 2;
> +		int pivot, pivot_index;
> +		int i = lo - 1;

The be clear, I would prefer to initialize `i` when we really use it (i.e. at
the for-loop).

> +
> +		/* We employ the median-of-three rule to choose the pivot, mitigating

https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting

> +		 * worst-case scenarios such as already sorted arrays and aiming to reduce
> +		 * the expected number of necessary comparisons. This strategy enhances the
> +		 * algorithm's performance and ensures a more balanced partitioning.
> +         */

$ ./scripts/checkpatch.pl --strict ...
ERROR: code indent should use tabs where possible
#284: FILE: drivers/platform/chrome/cros_ec_sensorhub_ring.c:171:
+         */$

> +		if (array[lo] > array[mid])
> +			cros_ec_sensor_ring_median_swap(&array[lo],
> +							&array[mid]);

It can fit into 100-column.

> +		if (array[lo] > array[hi])
> +			cros_ec_sensor_ring_median_swap(&array[lo], &array[hi]);
> +		if (array[mid] < array[hi])
> +			cros_ec_sensor_ring_median_swap(&array[mid],
> +							&array[hi]);

Ditto.

> +
> +		pivot = array[hi];
> +
> +		for (int j = lo; j < hi; j++)
> +			if (array[j] < pivot)
> +				cros_ec_sensor_ring_median_swap(&array[++i],
> +								&array[j]);

Ditto.

> +		cros_ec_sensor_ring_median_swap(&array[i + 1], &array[hi]);
> +		pivot_index = i + 1;
> +		if (pivot_index == k)
> +			return array[pivot_index];
> +		if (pivot_index > k)
> +			hi = pivot_index - 1;
> +		else
> +			lo = pivot_index + 1;

Add a comment and thus `pivot_index` can be eliminated.

  reply	other threads:[~2023-11-10 18:18 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 18:54 [PATCH 0/7] platform/chrome: Implement quickselect for median calculation Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 1/7] platform/chrome: Fix typo 'preceeds' in comment Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 2/7] platform/chrome: Fix typo 'perod' " Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 3/7] platform/chrome: Fix typo 'noone' " Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 4/7] platform/chrome: Fix typo 'lantency' " Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 5/7] platform/chrome: Fix typo 'kifo' in commet Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 6/7] platform/chrome: Fix typo 'change' in comment Kuan-Wei Chiu
2023-11-09 18:54 ` [PATCH 7/7] platform/chrome: Implement quickselect for median calculation Kuan-Wei Chiu
2023-11-10  5:57   ` Tzung-Bi Shih [this message]
2023-11-10  5:58 ` [PATCH 0/7] " Tzung-Bi Shih
2023-11-10 14:52   ` Kuan-Wei Chiu
2023-11-10 16:52   ` [PATCH v2] platform/chrome: sensorhub: Fix typos Kuan-Wei Chiu
2023-11-10 18:34     ` Randy Dunlap
2023-11-13  3:23     ` patchwork-bot+chrome-platform
2023-11-14  6:00     ` patchwork-bot+chrome-platform
2023-11-10 16:53   ` [PATCH v2] platform/chrome: sensorhub: Implement quickselect for median calculation Kuan-Wei Chiu
2023-11-13  4:50     ` patchwork-bot+chrome-platform
2023-11-14  6:00     ` patchwork-bot+chrome-platform

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZU3GWkKczNV-AQA4@google.com \
    --to=tzungbi@kernel.org \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=groeck@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=visitorckw@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox