Skip to content

Commit

Permalink
Improvements in ReadableNativeMap locking mechanism (a follow up to D…
Browse files Browse the repository at this point in the history
…43398416)

Summary:
[Changelog][Internal]

A follow-up, based on javache comments on D43398416 (9aac13d) (post-land) - just some minor potential improvements for locking efficiency.

Reviewed By: javache

Differential Revision: D43438414

fbshipit-source-id: 0cf807a045a1f132d5481d3f6115a97869e93d65
  • Loading branch information
rshest authored and facebook-github-bot committed Feb 20, 2023
1 parent 1cb46ea commit b77841d
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@ public static int getJNIPassCounter() {
}

private void ensureKeysAreImported() {
if (mKeys != null) {
return;
}
synchronized (this) {
if (mKeys == null) {
mKeys = Assertions.assertNotNull(importKeys());
mJniCallCounter++;
}
mKeys = Assertions.assertNotNull(importKeys());
mJniCallCounter++;
}
}

private HashMap<String, Object> getLocalMap() {
if (mLocalMap != null) {
return mLocalMap;
}
ensureKeysAreImported();
synchronized (this) {
ensureKeysAreImported();
if (mLocalMap == null) {
Object[] values = Assertions.assertNotNull(importValues());
mJniCallCounter++;
Expand All @@ -75,9 +76,8 @@ private HashMap<String, Object> getLocalMap() {
if (mLocalTypeMap != null) {
return mLocalTypeMap;
}
ensureKeysAreImported();
synchronized (this) {
// check that no other thread has already updated
ensureKeysAreImported();
if (mLocalTypeMap == null) {
Object[] types = Assertions.assertNotNull(importTypes());
mJniCallCounter++;
Expand Down Expand Up @@ -190,9 +190,10 @@ public int getInt(@NonNull String name) {

@Override
public @NonNull Iterator<Map.Entry<String, Object>> getEntryIterator() {
ensureKeysAreImported();
final String[] iteratorKeys = mKeys;
synchronized (this) {
ensureKeysAreImported();

final String[] iteratorKeys = mKeys;
final Object[] iteratorValues = Assertions.assertNotNull(importValues());

return new Iterator<Map.Entry<String, Object>>() {
Expand Down

0 comments on commit b77841d

Please sign in to comment.