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
ImmutableMap should not take O(N) to do a lookup #7559
Comments
Removed Priority-Medium label. |
Set owner to @kodandersson. |
Now that they are efficiently serialized, we can simply reuse the _LinkedHashMapMixin that we use for the default (mutable) maps, but redefining all mutating operations to throw the appropriate error. |
A long time ago we changed immutable maps to keep the entries ordered in source order, so a binary search (as suggested in the TODO comment in the code) does not work. Let's not make this more complicated than necessary. |
As far as I understand Daniel was suggesting the use of the efficient ordered maps as an implementation of ImmutableMap. How best to do this is not entirely clear to me yet. |
Yeah, the bug was more about the fact that key lookup is O(N), not the particular approach described in the TODO. O(N) doesn't match expectations about how Map lookup should work. The workaround is to avoid const maps and/or keep them small. |
Correct, that is not only a workaround but also good practice. Constant maps get evaluated at compile-time. If you have a large map, load times may suffer. (There is another open bug that complains that load time for a script with a > 2000 entry const map is too long.) |
See the code:
V operator [](K key) {
// TODO(hausner): Since the keys are sorted, we could do a binary
// search. But is it worth it?
for (int i = 0; i < kvPairs_.length - 1; i += 2) {
if (key == kvPairs_[i]) {
return kvPairs_[i+1];
}
}
return null;
}
O(N) lookup means you are better off not making const Maps, which is completely counter to the design of const.
The text was updated successfully, but these errors were encountered: