Skip to content

Commit fee28fb

Browse files
committed
perf: register objects concurrently on every API level
ObjectTable was only concurrent where StampedLock exists (API 24+). With a monitor there is no reason to keep API 21-23 on the slower paths: argument objects resolved natively under the isolate lock, and new ids registered through a separate path. findIdOptimistic is now findId, and getOrCreateObjectId and packageArgs register through findOrRegister alone.
1 parent 7f7ad3a commit fee28fb

3 files changed

Lines changed: 19 additions & 93 deletions

File tree

‎test-app/runtime/src/main/java/com/tns/ObjectTable.java‎

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
* holds its object strongly while the JS wrapper is reachable and through a WeakReference
88
* after the wrapper was collected (until the object is collected too).
99
*
10-
* When concurrent, any thread may read and findOrRegister: every access synchronizes on the
11-
* table. Otherwise every mutation and every read must come from the thread that holds the
12-
* runtime's isolate lock. Neither table allocates per entry.
10+
* Any thread may read and findOrRegister: every access synchronizes on the table. Neither
11+
* table allocates per entry.
1312
*
1413
* A monitor rather than java.util.concurrent locks: those live in the boot classpath but
1514
* outside the boot image, so on device they run interpreted, and an app's profile can't
@@ -25,30 +24,6 @@ interface IdSource {
2524
int newId();
2625
}
2726

28-
private final boolean concurrent;
29-
30-
ObjectTable() {
31-
this(false);
32-
}
33-
34-
ObjectTable(boolean concurrent) {
35-
this.concurrent = concurrent;
36-
}
37-
38-
/**
39-
* The id obj is registered under, from any thread, without the isolate lock; NO_ID when
40-
* the table isn't concurrent. A hit was correct at the time of the read; the object may
41-
* have been released since, which the caller must check under the isolate lock.
42-
*/
43-
int findIdOptimistic(Object obj) {
44-
if (!concurrent) {
45-
return NO_ID;
46-
}
47-
synchronized (this) {
48-
return findIdLocked(obj);
49-
}
50-
}
51-
5227
private static Object deref(Object value) {
5328
return value instanceof WeakRef ? ((WeakRef) value).get() : value;
5429
}
@@ -94,10 +69,6 @@ private static final class Chunk {
9469

9570
/** Registers obj under id, held strongly, replacing whatever id held before. */
9671
void putStrong(int id, Object obj) {
97-
if (!concurrent) {
98-
putStrongLocked(id, obj);
99-
return;
100-
}
10172
synchronized (this) {
10273
putStrongLocked(id, obj);
10374
}
@@ -130,15 +101,10 @@ private void putStrongLocked(int id, Object obj, int hash) {
130101
identityCount++;
131102
}
132103

133-
boolean allowsConcurrentRegistration() {
134-
return concurrent;
135-
}
136-
137104
/**
138105
* The id obj is registered under, registering it strongly under a new id from ids when
139106
* it has none; FRESH is set in the result when this call registered it. The lookup and
140-
* the insert happen under one lock, so concurrent calls agree on one id. Only when
141-
* allowsConcurrentRegistration().
107+
* the insert happen under one lock, so concurrent calls agree on one id.
142108
*/
143109
long findOrRegister(Object obj, IdSource ids) {
144110
synchronized (this) {
@@ -153,11 +119,11 @@ long findOrRegister(Object obj, IdSource ids) {
153119
}
154120
}
155121

156-
/** The id obj is registered under, or NO_ID. */
122+
/**
123+
* The id obj is registered under, or NO_ID. From a thread without the isolate lock, the
124+
* object may have been released since, which the caller must check under the lock.
125+
*/
157126
int findId(Object obj) {
158-
if (!concurrent) {
159-
return findIdLocked(obj);
160-
}
161127
synchronized (this) {
162128
return findIdLocked(obj);
163129
}
@@ -185,9 +151,6 @@ private int findIdLocked(Object obj, int hash) {
185151

186152
/** The object registered under id; null if there is none or it was collected. */
187153
Object get(int id) {
188-
if (!concurrent) {
189-
return getLocked(id);
190-
}
191154
synchronized (this) {
192155
return getLocked(id);
193156
}
@@ -199,9 +162,6 @@ private Object getLocked(int id) {
199162
}
200163

201164
boolean contains(int id) {
202-
if (!concurrent) {
203-
return rawLocked(id) != null;
204-
}
205165
synchronized (this) {
206166
return rawLocked(id) != null;
207167
}
@@ -212,9 +172,6 @@ boolean contains(int id) {
212172
* the object is still alive; an entry whose object was already collected is removed.
213173
*/
214174
boolean weakenAndCheckAlive(int id) {
215-
if (!concurrent) {
216-
return weakenAndCheckAliveLocked(id);
217-
}
218175
synchronized (this) {
219176
return weakenAndCheckAliveLocked(id);
220177
}
@@ -228,10 +185,6 @@ void weakenAndCheckAlive(int[] ids, int count) {
228185
if (count == 0) {
229186
return;
230187
}
231-
if (!concurrent) {
232-
weakenAndCheckAliveAllLocked(ids, count);
233-
return;
234-
}
235188
synchronized (this) {
236189
weakenAndCheckAliveAllLocked(ids, count);
237190
}
@@ -263,10 +216,6 @@ private boolean weakenAndCheckAliveLocked(int id) {
263216
}
264217

265218
void remove(int id) {
266-
if (!concurrent) {
267-
removeLocked(id);
268-
return;
269-
}
270219
synchronized (this) {
271220
removeLocked(id);
272221
}

‎test-app/runtime/src/main/java/com/tns/Runtime.java‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ public void run() {
181181
"Primitive types need to be manually wrapped in their respective Object wrappers.\n" +
182182
"If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument.";
183183

184-
// On API 24+ any thread may read it and register new objects through findOrRegister
185-
// (see ObjectTable); every other mutation comes from the thread holding this runtime's
184+
// Any thread may read it and register new objects through findOrRegister (see
185+
// ObjectTable); every other mutation comes from the thread holding this runtime's
186186
// isolate lock.
187187
private final ObjectTable objectTable;
188188

@@ -334,7 +334,7 @@ public Runtime(ClassResolver classResolver, GcListener gcListener, StaticConfigu
334334
}
335335

336336
public Runtime(StaticConfiguration config, DynamicConfiguration dynamicConfiguration) {
337-
this.objectTable = new ObjectTable(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N);
337+
this.objectTable = new ObjectTable();
338338
synchronized (Runtime.currentRuntime) {
339339
ManualInstrumentation.Frame frame = ManualInstrumentation.start("new Runtime");
340340
try {
@@ -1186,19 +1186,9 @@ private int getOrCreateJavaObjectID(Object obj) {
11861186

11871187
// obj's id (low 32 bits), registering it strongly under a new id if it has none, with
11881188
// ObjectTable.FRESH set then. Threads registering call arguments (packageArgs) do not
1189-
// hold the isolate lock, so where they may, the lookup and insert are one atomic step.
1189+
// hold the isolate lock, so the lookup and insert are one atomic step.
11901190
private long getOrCreateObjectId(Object obj) {
1191-
// Exact: only the isolate-lock holder (this thread) removes entries.
1192-
int id = objectTable.findId(obj);
1193-
if (id != ObjectTable.NO_ID) {
1194-
return id & 0xFFFFFFFFL;
1195-
}
1196-
if (objectTable.allowsConcurrentRegistration()) {
1197-
return objectTable.findOrRegister(obj, objectIds);
1198-
}
1199-
id = generateNewObjectId(getRuntimeId());
1200-
makeInstanceStrong(obj, id);
1201-
return (id & 0xFFFFFFFFL) | ObjectTable.FRESH;
1191+
return objectTable.findOrRegister(obj, objectIds);
12021192
}
12031193

12041194
public static Object callJSMethodFromPossibleNonMainThread(Object javaObject, String methodName, Class<?> retType, Object... args) throws NativeScriptException {
@@ -1383,11 +1373,8 @@ private PackagedArgs packageArgs(Object... args) {
13831373
id = bound.getObjectId__ns();
13841374
}
13851375
}
1386-
if (id == ObjectTable.NO_ID) {
1387-
id = objectTable.findIdOptimistic(value);
1388-
}
13891376
long fresh = 0;
1390-
if (id == ObjectTable.NO_ID && objectTable.allowsConcurrentRegistration()) {
1377+
if (id == ObjectTable.NO_ID) {
13911378
// Registered here rather than under the isolate lock, so threads calling
13921379
// in with new objects only serialize on creating their wrappers.
13931380
long found = objectTable.findOrRegister(value, objectIds);
@@ -1396,8 +1383,6 @@ private PackagedArgs packageArgs(Object... args) {
13961383
fresh = FRESH_ARG;
13971384
}
13981385
}
1399-
// Otherwise the id is a hint, resolved or registered natively under the
1400-
// isolate lock.
14011386
values[i] = packIdAndClass(id, value) | fresh;
14021387
}
14031388

@@ -1513,7 +1498,7 @@ private Object dispatchCallJSMethodNative(final Object javaObject, int objectIdH
15131498
// A hint: other threads may be writing the table, so native re-checks it under the
15141499
// isolate lock and resolves the id itself on a miss.
15151500
ret = callJSMethodNative(getRuntimeId(), javaObject,
1516-
objectIdHint != ObjectTable.NO_ID ? objectIdHint : objectTable.findIdOptimistic(javaObject), methodName, MethodNameIds.idOf(methodName), returnType, isConstructor, packagedArgs.count, packagedArgs.values, packagedArgs.refs, primitiveResult);
1501+
objectIdHint != ObjectTable.NO_ID ? objectIdHint : objectTable.findId(javaObject), methodName, MethodNameIds.idOf(methodName), returnType, isConstructor, packagedArgs.count, packagedArgs.values, packagedArgs.refs, primitiveResult);
15171502
ret = takePrimitiveResult(ret, returnType, primitiveResult);
15181503
} catch (NativeScriptException e) {
15191504
// With multithreaded JS the owner can be torn down between the

‎test-app/runtime/src/test/java/com/tns/ObjectTableTest.java‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void aRegisteredWeakReferenceIsNotMistakenForAWeakEntry() {
7979

8080
@Test
8181
public void concurrentReadsFindStableEntriesWhileAnotherThreadWrites() throws Exception {
82-
final ObjectTable table = new ObjectTable(true);
82+
final ObjectTable table = new ObjectTable();
8383
final int stableCount = 64;
8484
final Object[] stable = new Object[stableCount];
8585
for (int i = 0; i < stableCount; i++) {
@@ -124,7 +124,7 @@ public void run() {
124124
Random random = new Random(seed);
125125
while (!done.get()) {
126126
int expected = random.nextInt(stableCount);
127-
int id = table.findIdOptimistic(stable[expected]);
127+
int id = table.findId(stable[expected]);
128128
if (id == expected) {
129129
hits.incrementAndGet();
130130
} else {
@@ -152,7 +152,7 @@ public void run() {
152152

153153
@Test
154154
public void concurrentRegistrationsAgreeOnOneIdPerObject() throws Exception {
155-
final ObjectTable table = new ObjectTable(true);
155+
final ObjectTable table = new ObjectTable();
156156
final java.util.concurrent.atomic.AtomicInteger nextId = new java.util.concurrent.atomic.AtomicInteger();
157157
final ObjectTable.IdSource ids = new ObjectTable.IdSource() {
158158
@Override
@@ -215,7 +215,7 @@ public void run() {
215215

216216
@Test
217217
public void exactReadsStayExactWhileAnotherThreadRegisters() throws Exception {
218-
final ObjectTable table = new ObjectTable(true);
218+
final ObjectTable table = new ObjectTable();
219219
final java.util.concurrent.atomic.AtomicInteger nextId = new java.util.concurrent.atomic.AtomicInteger(1000);
220220
final ObjectTable.IdSource ids = new ObjectTable.IdSource() {
221221
@Override
@@ -309,7 +309,7 @@ public void keepsSurvivorsOfReleasedRangesAndOldIdsRegisteredLate() {
309309

310310
@Test
311311
public void idsPastIntMaxAreUnsigned() {
312-
ObjectTable table = new ObjectTable(true);
312+
ObjectTable table = new ObjectTable();
313313
Object a = new Object();
314314
Object b = new Object();
315315
table.putStrong(Integer.MIN_VALUE + 3, a);
@@ -335,14 +335,6 @@ public int newId() {
335335
assertEquals(1, table.size());
336336
}
337337

338-
@Test
339-
public void optimisticReadsMissWhenDisabled() {
340-
ObjectTable table = new ObjectTable(false);
341-
Object a = new Object();
342-
table.putStrong(1, a);
343-
assertEquals(ObjectTable.NO_ID, table.findIdOptimistic(a));
344-
}
345-
346338
@Test
347339
public void matchesReferenceMapsUnderRandomOperations() {
348340
Random random = new Random(42);

0 commit comments

Comments
 (0)