perf: optimize GenerateRandomString() #66

Merged
Tachi107 merged 1 commit from perf-generate-random-string into main 2022-08-26 06:53:42 -04:00
Tachi107 commented 2022-08-26 06:29:58 -04:00 (Migrated from github.com)

The previous implementation used an std::stringstream to concatenate the generated random string.

The new one uses a simple preallocated std::string, as the size of the output is already known - it is the length parameter.

It also uses std::generate_n() instead of an explicit loop, making the code more concise and potentially faster, as no calls to std::string::operator+= are needed.

Calling GenerateRandomString(1'000'000) with the std::stringstream-based implementation allocated 16 times, for a total of 3'173'516 bytes. The new one cuts this down to 4 allocs, for a total of 1'076'864 bytes.

Inspired by #64

The previous implementation used an `std::stringstream` to concatenate the generated random string. The new one uses a simple preallocated `std::string`, as the size of the output is already known - it is the `length` parameter. It also uses `std::generate_n()` instead of an explicit loop, making the code more concise and potentially faster, as no calls to `std::string::operator+=` are needed. Calling `GenerateRandomString(1'000'000)` with the `std::stringstream`-based implementation allocated 16 times, for a total of 3'173'516 bytes. The new one cuts this down to 4 allocs, for a total of 1'076'864 bytes. Inspired by #64
Exzap (Migrated from github.com) reviewed 2022-08-26 06:41:00 -04:00
Exzap (Migrated from github.com) commented 2022-08-26 06:36:39 -04:00

This can be made even more efficient with two simple changes. The first is to use .resize here

This can be made even more efficient with two simple changes. The first is to use .resize here
Exzap (Migrated from github.com) commented 2022-08-26 06:38:40 -04:00

And replace this with result[i] = characters[index_dist(gen)];
That lets you avoid the overhead that comes with appending (size tracking and checks)

And replace this with `result[i] = characters[index_dist(gen)];` That lets you avoid the overhead that comes with appending (size tracking and checks)
Tachi107 (Migrated from github.com) reviewed 2022-08-26 06:50:27 -04:00
Tachi107 (Migrated from github.com) commented 2022-08-26 06:50:27 -04:00

I've changed this to std::generate_n(), so that no loop is needed at all.

I've changed this to std::generate_n(), so that no loop is needed at all.
Tachi107 (Migrated from github.com) reviewed 2022-08-26 06:52:00 -04:00
Tachi107 (Migrated from github.com) commented 2022-08-26 06:51:59 -04:00

Done

Done
Exzap commented 2022-08-26 06:53:47 -04:00 (Migrated from github.com)

thanks!

thanks!
Tachi107 (Migrated from github.com) reviewed 2022-08-26 06:58:21 -04:00
Tachi107 (Migrated from github.com) commented 2022-08-26 06:58:21 -04:00

Just curious, how does .resize() improve things compared to .reserve()?

Just curious, how does `.resize()` improve things compared to `.reserve()`?
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: cemu-project_Mirror/Cemu-2024-03-05#66
No description provided.