Skip to content

Commit 8e6de67

Browse files
committed
Gets rid of the variable map, using only the Marker pool, reducing allocations by 66%.
1 parent ecb4985 commit 8e6de67

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

src/main/java/com/amazon/ion/impl/LazyEExpressionArgsReader.java

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.amazon.ion.impl;
44

55
import com.amazon.ion.IonCursor;
6+
import com.amazon.ion.IonException;
67
import com.amazon.ion.IonType;
78
import com.amazon.ion.MacroAwareIonWriter;
89
import com.amazon.ion.SymbolToken;
@@ -58,10 +59,7 @@ abstract class LazyEExpressionArgsReader {
5859
// Whether to produce a tape that preserves the invocation verbatim.
5960
private boolean verbatimTranscode = false;
6061

61-
//private Marker[][] markerMapPool = new Marker[4][];
62-
//private int markerMapPoolIndex = -1;
63-
64-
private Marker[] markerPool = new Marker[16];
62+
private Marker[] markerPool = new Marker[256];
6563
private int markerPoolIndex = -1;
6664

6765
/**
@@ -81,9 +79,9 @@ abstract class LazyEExpressionArgsReader {
8179
});
8280
}
8381

84-
private Marker getMarker() {
85-
int index = ++markerPoolIndex;
86-
if (index == markerPool.length) {
82+
private Marker getMarker(int index) {
83+
markerPoolIndex = Math.max(markerPoolIndex, index);
84+
while (index >= markerPool.length) {
8785
markerPool = Arrays.copyOf(markerPool, markerPool.length * 2);
8886
}
8987
Marker marker = markerPool[index];
@@ -94,24 +92,6 @@ private Marker getMarker() {
9492
return marker;
9593
}
9694

97-
/* NOTE: pooling the marker map cuts allocation rate by a lot, but is slower because of the need to Arrays.fill
98-
private Marker[] getMarkerMap(int minimumSize) {
99-
int index = ++markerMapPoolIndex;
100-
if (index == markerMapPool.length) {
101-
markerMapPool = Arrays.copyOf(markerMapPool, markerMapPool.length * 2);
102-
}
103-
Marker[] markerMap = markerMapPool[index];
104-
if (markerMap == null || markerMap.length < minimumSize) {
105-
markerMap = new Marker[minimumSize];
106-
markerMapPool[index] = markerMap;
107-
} else {
108-
Arrays.fill(markerMap, null);
109-
}
110-
return markerMap;
111-
}
112-
113-
*/
114-
11595
/**
11696
* @return true if the value upon which the reader is positioned represents a macro invocation; otherwise, false.
11797
*/
@@ -251,12 +231,10 @@ protected void readValueAsExpression(boolean isImplicitRest) {
251231
}
252232
}
253233

254-
private int collectAndFlattenComplexEExpression(int numberOfParameters, ExpressionTape.Core macroBodyTape, List<Macro.Parameter> signature, PresenceBitmap presenceBitmap) {
234+
private int collectAndFlattenComplexEExpression(int numberOfParameters, ExpressionTape.Core macroBodyTape, List<Macro.Parameter> signature, PresenceBitmap presenceBitmap, int markerPoolStartIndex) {
255235
// TODO drop expression groups? No longer needed after the variables are resolved. Further simplifies the evaluator. Might miss validation? Also, would need to distribute field names over the elements of the group.
256236
int macroBodyTapeIndex = 0;
257237
int numberOfVariables = macroBodyTape.getNumberOfVariables();
258-
// Key: signature index. Value: Marker for the location of the argument in the stream or tape.
259-
Marker[] variableMap = new Marker[numberOfVariables]; //getMarkerMap(numberOfVariables); //new Marker[numberOfVariables];
260238
int numberOfDuplicatedVariables = 0;
261239
int numberOfOutOfOrderVariables = 0;
262240
for (int i = 0; i < numberOfVariables; i++) {
@@ -275,11 +253,10 @@ private int collectAndFlattenComplexEExpression(int numberOfParameters, Expressi
275253
presenceBitmap == null ? PresenceBitmap.EXPRESSION : presenceBitmap.get(invocationOrdinal),
276254
invocationOrdinal == (numberOfParameters - 1)
277255
);
278-
Marker marker = getMarker();
256+
Marker marker = getMarker(markerPoolStartIndex + invocationOrdinal);
279257
marker.typeId = null;
280258
marker.startIndex = startIndexInTape;
281259
marker.endIndex = expressionTape.size();
282-
variableMap[variableOrdinal] = marker;
283260
break;
284261
} else if (invocationOrdinal < variableOrdinal) {
285262
// The variable for this ordinal cannot have been read yet.
@@ -291,16 +268,15 @@ private int collectAndFlattenComplexEExpression(int numberOfParameters, Expressi
291268
invocationOrdinal == (numberOfParameters - 1)
292269
);
293270
expressionTape = expressionTapeOrdered;
294-
Marker marker = getMarker();
271+
Marker marker = getMarker(markerPoolStartIndex + invocationOrdinal);
295272
marker.startIndex = scratchStartIndex;
296273
marker.endIndex = expressionTapeScratch.size();
297274
// This is a sentinel to denote that the expression is in the scratch tape.
298275
marker.typeId = IonTypeID.ALWAYS_INVALID_TYPE_ID;
299-
variableMap[invocationOrdinal] = marker;
300276
numberOfOutOfOrderVariables++;
301277
} else {
302278
// This is a variable that has already been encountered.
303-
Marker marker = variableMap[variableOrdinal];
279+
Marker marker = markerPool[markerPoolStartIndex + variableOrdinal];
304280
if (marker == null) {
305281
throw new IllegalStateException("Every variable ordinal must be recorded as it is encountered.");
306282
}
@@ -313,7 +289,7 @@ private int collectAndFlattenComplexEExpression(int numberOfParameters, Expressi
313289
invocationOrdinal++;
314290
}
315291
}
316-
//--markerMapPoolIndex;
292+
markerPoolIndex = markerPoolStartIndex;
317293
return macroBodyTapeIndex;
318294
}
319295

@@ -350,7 +326,7 @@ private ExpressionTape collectAndFlattenUserEExpressionArgs(boolean isTopLevel,
350326
} else if (macro.isSimple()) {
351327
macroBodyTapeIndex = collectAndFlattenSimpleEExpression(numberOfParameters, macroBodyTape, signature, presenceBitmap);
352328
} else {
353-
macroBodyTapeIndex = collectAndFlattenComplexEExpression(numberOfParameters, macroBodyTape, signature, presenceBitmap);
329+
macroBodyTapeIndex = collectAndFlattenComplexEExpression(numberOfParameters, macroBodyTape, signature, presenceBitmap, markerPoolIndex + 1);
354330
}
355331
// Copy everything after the last parameter.
356332
expressionTape.copyFromRange(macroBodyTape, macroBodyTapeIndex, macroBodyTape.size());
@@ -436,6 +412,7 @@ public void transcodeAndBeginEvaluatingMacroInvocation(LazyMacroEvaluator macroE
436412
macroEvaluator.initExpansion(fieldName, collectEExpressionArgs(true));
437413
verbatimExpressionTape.transcodeArgumentsTo(transcoder);
438414
verbatimExpressionTape.clear();
415+
markerPoolIndex = -1;
439416
}
440417

441418
/**

0 commit comments

Comments
 (0)