-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Another array access performance issue. #13938
Comments
I guess .field_ptr should be generated in fieldAccess in this case. working on it. |
Fix reverted in f93a36c |
I've been running into this quite often on platforms where llvm isn't great at optimizing (powerpc). The following snipped worked great as a test example to show this bug happening and not happening even though all examples index into the array. const std = @import("std");
const E = enum(u8){
a,
b,
c,
d,
};
var array: std.enums.EnumArray(E, u32) = std.enums.EnumArray(E, u32).initFill(0);
noinline fn handle(id: u32) u32 {
// Causes odd stack pushing and popping with 4 or less enum members, but optimized out the memcpy
// LLVM is unable to optimize out the memcopy when the enum has more than 4 members.
return array.get(@enumFromInt(id));
// These both cause correct code generation, and do not generate a call to memcpy in llvm ir
//return array.values[id];
//return array.getPtrConst(@enumFromInt(id)).*;
}
export fn square(id: u32) u32 {
array.set(.a, 5);
array.set(.c, 8);
return handle(id);
} I'm compiling this snipped with |
A good explanation can be found here: #17580 (comment) Summary: When an array is indexed, the expression on the left (the array, which is a value) is evaluated before (by design) the expression on the right (the function that returns the index). If there's any possibility that the function call modifies the array (which is the default assumption), we have no choice but to make a copy of the array before calling that function. return array.get(@enumFromInt(id));
For the general case, AFAIK possible solutions are:
|
…k.Neighbor ziglang/zig#13938 strikes back
…k.Neighbor ziglang/zig#13938 strikes back
…k.Neighbor ziglang/zig#13938 strikes back
Zig Version
0.11.0-dev.753+331861161
Steps to Reproduce and Observed Behavior
I thought this was covered by #12215, but now that it is fixed I still get terrible performance.
Here is a simple benchmark:
Even in release-fast the performance is terrible:
godbolt reveals that, like in #12215, there is a
memcpy
when accessing the array.Expected Behavior
When applying the workaround
the performance is significantly better:
The text was updated successfully, but these errors were encountered: