From: Ivan Orlov <ivan.orlov0322@gmail.com>
To: David Laight <David.Laight@ACULAB.COM>,
"paul.walmsley@sifive.com" <paul.walmsley@sifive.com>,
"palmer@dabbelt.com" <palmer@dabbelt.com>,
"aou@eecs.berkeley.edu" <aou@eecs.berkeley.edu>
Cc: "conor.dooley@microchip.com" <conor.dooley@microchip.com>,
"ajones@ventanamicro.com" <ajones@ventanamicro.com>,
"samuel@sholland.org" <samuel@sholland.org>,
"alexghiti@rivosinc.com" <alexghiti@rivosinc.com>,
"linux-riscv@lists.infradead.org"
<linux-riscv@lists.infradead.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"skhan@linuxfoundation.org" <skhan@linuxfoundation.org>
Subject: Re: [PATCH] riscv: lib: Optimize 'strlen' function
Date: Sun, 17 Dec 2023 22:52:47 +0000 [thread overview]
Message-ID: <f495519c-c681-4445-aedc-0f44fbd192e8@gmail.com> (raw)
In-Reply-To: <86d3947bce1f49c395224998e7d65dc2@AcuMS.aculab.com>
On 12/17/23 17:00, David Laight wrote:
> From: Ivan Orlov
>> Sent: 13 December 2023 15:46
>>
>> The current non-ZBB implementation of 'strlen' function iterates the
>> memory bytewise, looking for a zero byte. It could be optimized to use
>> the wordwise iteration instead, so we will process 4/8 bytes of memory
>> at a time.
> ...
>> 1. If the address is unaligned, iterate SZREG - (address % SZREG) bytes
>> to align it.
>
> An alternative is to mask the address and 'or' in non-zero bytes
> into the first word - might be faster.
>
Hi David,
Yeah, it might be an option, I'll test it. Thanks!
> ...
>> Here you can find the benchmarking results for the VisionFive2 board
>> comparing the old and new implementations of the strlen function.
>>
>> Size: 1 (+-0), mean_old: 673, mean_new: 666
>> Size: 2 (+-0), mean_old: 672, mean_new: 676
>> Size: 4 (+-0), mean_old: 685, mean_new: 659
>> Size: 8 (+-0), mean_old: 682, mean_new: 673
>> Size: 16 (+-0), mean_old: 718, mean_new: 694
> ...
>
> Is that 32bit or 64bit?
> The word-at-a-time strlen() is typically not worth it for 32bit.
>
I tested it on 64-bit board only as it is the only board I have...
I assume the performance gain would be less noticeable on 32bit,
probably the word-oriented function could be even slower than the
byte-oriented one for shorter strings.
However, I'm not sure if any physical 32-bit risc-v boards with Linux
support actually exist at the moment... So the only way to test the
solution on the 32-bit system would be QEMU, and probably it wouldn't be
really representative, right?
But it definitely worth a try and probably I could include a separate
implementation for 32-bit RISC-V which will simply iterate the bytes in
case if QEMU 32-bit test will show significant overhead for
word-oriented function.
> I'd also guess that pretty much all the calls in-kernel are short.
I'm 99% sure they are! However, I believe if word-oriented solution
doesn't introduce performance overhead for shorter strings but works
much faster for longer strings, it still worth an implementation! :)
> You might try counting as: histogram[ilog2(strlen_result)]++
> and seeing what it shows for some workload.
> I bet you (a beer if I see you!) that you won't see many over 1k.
Sounds like a funny experiment, and I accept a bet! Beer is more than
doable as I'm also located in the UK (Manchester).
--
Kind regards,
Ivan Orlov
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2023-12-17 22:54 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-13 15:45 [PATCH] riscv: lib: Optimize 'strlen' function Ivan Orlov
2023-12-17 17:00 ` David Laight
2023-12-17 22:52 ` Ivan Orlov [this message]
2023-12-18 1:41 ` Ivan Orlov
2023-12-18 9:20 ` David Laight
2023-12-18 10:03 ` Ivan Orlov
2023-12-18 10:12 ` David Laight
2023-12-17 18:10 ` David Laight
2023-12-17 23:23 ` Ivan Orlov
2023-12-18 9:12 ` David Laight
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=f495519c-c681-4445-aedc-0f44fbd192e8@gmail.com \
--to=ivan.orlov0322@gmail.com \
--cc=David.Laight@ACULAB.COM \
--cc=ajones@ventanamicro.com \
--cc=alexghiti@rivosinc.com \
--cc=aou@eecs.berkeley.edu \
--cc=conor.dooley@microchip.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=samuel@sholland.org \
--cc=skhan@linuxfoundation.org \
/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;
as well as URLs for NNTP newsgroup(s).