Skip to content
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

Closed
jmesserly opened this issue Dec 21, 2012 · 7 comments
Closed

ImmutableMap should not take O(N) to do a lookup #7559

jmesserly opened this issue Dec 21, 2012 · 7 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends.

Comments

@jmesserly
Copy link

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.

@iposva-google
Copy link
Contributor

Removed Priority-Medium label.
Added Priority-Unassigned label.

@kodandersson
Copy link
Contributor

Set owner to @kodandersson.

@jmesserly jmesserly added Type-Defect area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. labels Feb 23, 2015
@kodandersson
Copy link
Contributor

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.

@kodandersson kodandersson assigned mhausner and unassigned kodandersson Oct 2, 2015
@mhausner
Copy link
Contributor

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.

@iposva-google
Copy link
Contributor

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.

@jmesserly
Copy link
Author

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.

@mhausner
Copy link
Contributor

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

5 participants