Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

SharedPreferencesImpl.EditorImpl: Remove redundant map lookups in commitToMemory #585

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kunjan-a
Copy link

  1. Instead of HashMap.contains check before HashMap.remove, directly perform HashMap.remove.

    • When key is not present there is no impact as exactly same code gets executed inside implementation of HashMap.contains and HashMap.remove for this case.
    • When key is present we save one wasteful hash lookup that would have happened due to HashMap.contains.
  2. Instead of HashMap.contains check before HashMap.get, directly perform HashMap.get.

    • When key is not present there is no impact as exactly same getNode method gets invoked in both HashMap.contains and HashMap.remove implementations.
    • When key is present we same one wasteful hash lookup that would have happened due to HashMap.contains.
    • Also we save on a null check by reversing the equals check as we know new value to be added to map is not null.

1. Instead of `HashMap.contains` check before `HashMap.remove`, directly perform `HashMap.remove`.
    - When _key_ is not present there is no impact as exactly same code gets executed inside implementation of `HashMap.contains` and `HashMap.remove` for this case.
    - When _key_ is present we save one wasteful hash lookup that would have happened due to `HashMap.contains`.

1. Instead of `HashMap.contains` check before `HashMap.get`, directly perform `HashMap.get`.
    - When _key_ is not present there is no impact as exactly same `getNode` method gets invoked in both `HashMap.contains` and `HashMap.remove` implementations.
    - When _key_ is present we same one wasteful hash lookup that would have happened due to `HashMap.contains`.
    - Also we save on a null check by reversing the _equals_ check as we know new value to be added to map is not _null_.
@CL-window
Copy link

CL-window commented Jun 11, 2022 via email

@boludoz
Copy link

boludoz commented Oct 23, 2022

Screenshot_20221023-032614_Reddit.png

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants