-
Notifications
You must be signed in to change notification settings - Fork 5
Specialize BitArray equality to improve performance. #21
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
Comments
Here's what I came up with: // Inside of BitArray
// We specialize equality here for performance.
@override
bool operator ==(Object other) {
if (other is BitArray) {
if (identical(this, other)) {
return true;
}
if (length == other.length) {
final aData = _data;
final bData = other._data;
if (aData.length == bData.length) {
for (int i = 0; i < aData.length; i++) {
if (aData[i] != bData[i]) {
return false;
}
}
return true;
} else {
throw Exception("Invalid State?");
}
} else {
return super == other;
}
} else {
return super == other;
}
}
// We specialize hashCode here to use an explicit for-loop for performance.
@override
int get hashCode {
int total = 0;
for (int i = 0; i < _data.length; i++) {
total ^= _data[i].hashCode;
}
return total ^ length.hashCode;
} Here are some measurements with that change applied:
without:
@isoos what do you think? I'm not entirely sure that I've completely understood the invariants that you are trying to maintain (e.g., is the |
I think a bit of defensive programming never hurts, and throwing e.g.
another note: I think equals operator should return true if it is the same object type and internals, not when it represents the same abstract bitset. If we want to have a semantic check that would allow mixing different bit sets and check their set bits, it should be a separate method, not the |
I've noticed that BitArray equality could be specialized to be more efficient when two BitArray instances are being compared.
An
is
check would probably be fine (i.e. #18 (comment)) which could be used to introduce a fast path that uses for loops over the underlying buffers instead of having to use iterators (as is currently being done in the base BitSet implementation).Furthermore, I feel like inlining the fold in
hashCode
might give us some easy gains, too. I'm not sure about the overhead of fold and the closure... JIT might be able to deal with it just fine, but I have doubts about AOT.The text was updated successfully, but these errors were encountered: