-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Severe Performance Issue With std.rand.Random
#10037
Comments
Here's a godbolt showing the issue and a potential fix: https://zig.godbolt.org/z/E1ebnYMzM The problem here is that dirty tracking in LLVM is relatively coarse-grained for functions which are not inlined. This is inevitable in any optimizer, using more data for dirty tracking gets multiplied by every parameter of every function in the codebase. In the current code, LLVM peels off the first iteration of the loop and runs it first. In that iteration, it devirtualizes The fix is to store the vtable in a separate provenance which is easier for the compiler to destructure. This would change the use of the random interface from This problem applies to all interfaces in Zig which use the |
These changes have been made to resolve issue ziglang#10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.
These changes have been made to resolve issue ziglang#10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.
These changes have been made to resolve issue #10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.
Zig Version
On
0.9.0-dev.1444+e2a2e6c14
, though should apply to all versions of Zig withstd
which include this interface.Steps to Reproduce
To reproduce this error, an example using the current
Random
interface, and a modified copy is provided. These can be benchmarked against each other using a tool such ashyperfine
to show the significant performance difference. (The while loop is used just to increase the number of times we run the function so that the time taken is easily measurable).Example 1 (Current interface):
Example 2 (Modified interface):
The examples were built with the command
zig build-exe -O ReleaseFast example1.zig && zig build-exe -O ReleaseFast example2.zig
, and benchmarked with the commandhyperfine -w 10 ./example{1,2}
.Behaviour
The poor performance of the current
Random
interface has a significant impact on the performance of the random generators in the standard library. Example 2 performs significantly better than both example 1; on my desktop system, example 2 was measured to run at over 2 times the speed of example 1.cc: @SpexGuy @cryptocode
The text was updated successfully, but these errors were encountered: