[PATCH v3 5/8] lib/string_kunit: extend benchmarks to strnlen and chr searches

Feng Jiang posted 8 patches 2 weeks, 6 days ago
There is a newer version of this series
[PATCH v3 5/8] lib/string_kunit: extend benchmarks to strnlen and chr searches
Posted by Feng Jiang 2 weeks, 6 days ago
Extend the string benchmarking suite to include strnlen(), strchr(),
and strrchr().

For character search functions (strchr and strrchr), the benchmark
targets the null terminator. This ensures the entire string is scanned,
providing a consistent measure of full-length processing efficiency
comparable to strlen().

Suggested-by: Andy Shevchenko <andy@kernel.org>
Suggested-by: Eric Biggers <ebiggers@kernel.org>
Tested-by: Joel Stanley <joel@jms.id.au>
Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
---
 lib/tests/string_kunit.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index e20e924d1c67..9d76777ad753 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -850,6 +850,21 @@ static void string_bench_strlen(struct kunit *test)
 	STRING_BENCH_BUF(test, buf, len, strlen, buf);
 }
 
+static void string_bench_strnlen(struct kunit *test)
+{
+	STRING_BENCH_BUF(test, buf, len, strnlen, buf, len);
+}
+
+static void string_bench_strchr(struct kunit *test)
+{
+	STRING_BENCH_BUF(test, buf, len, strchr, buf, '\0');
+}
+
+static void string_bench_strrchr(struct kunit *test)
+{
+	STRING_BENCH_BUF(test, buf, len, strrchr, buf, '\0');
+}
+
 static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_memset16),
 	KUNIT_CASE(string_test_memset32),
@@ -876,6 +891,9 @@ static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_memtostr),
 	KUNIT_CASE(string_test_strends),
 	KUNIT_CASE(string_bench_strlen),
+	KUNIT_CASE(string_bench_strnlen),
+	KUNIT_CASE(string_bench_strchr),
+	KUNIT_CASE(string_bench_strrchr),
 	{}
 };
 
-- 
2.25.1
Re: [PATCH v3 5/8] lib/string_kunit: extend benchmarks to strnlen and chr searches
Posted by Andy Shevchenko 2 weeks, 6 days ago
On Tue, Jan 20, 2026 at 02:58:49PM +0800, Feng Jiang wrote:
> Extend the string benchmarking suite to include strnlen(), strchr(),
> and strrchr().
> 
> For character search functions (strchr and strrchr), the benchmark

strchr() and strrchr()

> targets the null terminator. This ensures the entire string is scanned,

NUL character

(Also check terminology everywhere: NULL — is for NULL pointers, NUL is for
 '\0' characters.)

> providing a consistent measure of full-length processing efficiency
> comparable to strlen().

Acked-by: Andy Shevchenko <andy@kernel.org>

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH v3 5/8] lib/string_kunit: extend benchmarks to strnlen and chr searches
Posted by Feng Jiang 2 weeks, 5 days ago
On 2026/1/20 15:48, Andy Shevchenko wrote:
> On Tue, Jan 20, 2026 at 02:58:49PM +0800, Feng Jiang wrote:
>> Extend the string benchmarking suite to include strnlen(), strchr(),
>> and strrchr().
>>
>> For character search functions (strchr and strrchr), the benchmark
> 
> strchr() and strrchr()

Will fix.

>> targets the null terminator. This ensures the entire string is scanned,
> 
> NUL character
> 
> (Also check terminology everywhere: NULL — is for NULL pointers, NUL is for
>  '\0' characters.)
> 

Thanks for the correction, I'll fix this and check other places as well.

>> providing a consistent measure of full-length processing efficiency
>> comparable to strlen().
> 
> Acked-by: Andy Shevchenko <andy@kernel.org>
> 

-- 
With Best Regards,
Feng Jiang