3
3
package com .amazon .ion .impl ;
4
4
5
5
import com .amazon .ion .IonCursor ;
6
+ import com .amazon .ion .IonException ;
6
7
import com .amazon .ion .IonType ;
7
8
import com .amazon .ion .MacroAwareIonWriter ;
8
9
import com .amazon .ion .SymbolToken ;
@@ -58,10 +59,7 @@ abstract class LazyEExpressionArgsReader {
58
59
// Whether to produce a tape that preserves the invocation verbatim.
59
60
private boolean verbatimTranscode = false ;
60
61
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 ];
65
63
private int markerPoolIndex = -1 ;
66
64
67
65
/**
@@ -81,9 +79,9 @@ abstract class LazyEExpressionArgsReader {
81
79
});
82
80
}
83
81
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 ) {
87
85
markerPool = Arrays .copyOf (markerPool , markerPool .length * 2 );
88
86
}
89
87
Marker marker = markerPool [index ];
@@ -94,24 +92,6 @@ private Marker getMarker() {
94
92
return marker ;
95
93
}
96
94
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
-
115
95
/**
116
96
* @return true if the value upon which the reader is positioned represents a macro invocation; otherwise, false.
117
97
*/
@@ -251,12 +231,10 @@ protected void readValueAsExpression(boolean isImplicitRest) {
251
231
}
252
232
}
253
233
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 ) {
255
235
// 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.
256
236
int macroBodyTapeIndex = 0 ;
257
237
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];
260
238
int numberOfDuplicatedVariables = 0 ;
261
239
int numberOfOutOfOrderVariables = 0 ;
262
240
for (int i = 0 ; i < numberOfVariables ; i ++) {
@@ -275,11 +253,10 @@ private int collectAndFlattenComplexEExpression(int numberOfParameters, Expressi
275
253
presenceBitmap == null ? PresenceBitmap .EXPRESSION : presenceBitmap .get (invocationOrdinal ),
276
254
invocationOrdinal == (numberOfParameters - 1 )
277
255
);
278
- Marker marker = getMarker ();
256
+ Marker marker = getMarker (markerPoolStartIndex + invocationOrdinal );
279
257
marker .typeId = null ;
280
258
marker .startIndex = startIndexInTape ;
281
259
marker .endIndex = expressionTape .size ();
282
- variableMap [variableOrdinal ] = marker ;
283
260
break ;
284
261
} else if (invocationOrdinal < variableOrdinal ) {
285
262
// The variable for this ordinal cannot have been read yet.
@@ -291,16 +268,15 @@ private int collectAndFlattenComplexEExpression(int numberOfParameters, Expressi
291
268
invocationOrdinal == (numberOfParameters - 1 )
292
269
);
293
270
expressionTape = expressionTapeOrdered ;
294
- Marker marker = getMarker ();
271
+ Marker marker = getMarker (markerPoolStartIndex + invocationOrdinal );
295
272
marker .startIndex = scratchStartIndex ;
296
273
marker .endIndex = expressionTapeScratch .size ();
297
274
// This is a sentinel to denote that the expression is in the scratch tape.
298
275
marker .typeId = IonTypeID .ALWAYS_INVALID_TYPE_ID ;
299
- variableMap [invocationOrdinal ] = marker ;
300
276
numberOfOutOfOrderVariables ++;
301
277
} else {
302
278
// This is a variable that has already been encountered.
303
- Marker marker = variableMap [ variableOrdinal ];
279
+ Marker marker = markerPool [ markerPoolStartIndex + variableOrdinal ];
304
280
if (marker == null ) {
305
281
throw new IllegalStateException ("Every variable ordinal must be recorded as it is encountered." );
306
282
}
@@ -313,7 +289,7 @@ private int collectAndFlattenComplexEExpression(int numberOfParameters, Expressi
313
289
invocationOrdinal ++;
314
290
}
315
291
}
316
- //--markerMapPoolIndex ;
292
+ markerPoolIndex = markerPoolStartIndex ;
317
293
return macroBodyTapeIndex ;
318
294
}
319
295
@@ -350,7 +326,7 @@ private ExpressionTape collectAndFlattenUserEExpressionArgs(boolean isTopLevel,
350
326
} else if (macro .isSimple ()) {
351
327
macroBodyTapeIndex = collectAndFlattenSimpleEExpression (numberOfParameters , macroBodyTape , signature , presenceBitmap );
352
328
} else {
353
- macroBodyTapeIndex = collectAndFlattenComplexEExpression (numberOfParameters , macroBodyTape , signature , presenceBitmap );
329
+ macroBodyTapeIndex = collectAndFlattenComplexEExpression (numberOfParameters , macroBodyTape , signature , presenceBitmap , markerPoolIndex + 1 );
354
330
}
355
331
// Copy everything after the last parameter.
356
332
expressionTape .copyFromRange (macroBodyTape , macroBodyTapeIndex , macroBodyTape .size ());
@@ -436,6 +412,7 @@ public void transcodeAndBeginEvaluatingMacroInvocation(LazyMacroEvaluator macroE
436
412
macroEvaluator .initExpansion (fieldName , collectEExpressionArgs (true ));
437
413
verbatimExpressionTape .transcodeArgumentsTo (transcoder );
438
414
verbatimExpressionTape .clear ();
415
+ markerPoolIndex = -1 ;
439
416
}
440
417
441
418
/**
0 commit comments