Skip to content

Pre-size flattened parser sequences #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,9 @@ public static JsonParserSequence createFlattened(boolean checkForExistingToken,
return new JsonParserSequence(checkForExistingToken,
new JsonParser[] { first, second });
}
ArrayList<JsonParser> p = new ArrayList<>();
if (first instanceof JsonParserSequence) {
((JsonParserSequence) first).addFlattenedActiveParsers(p);
} else {
p.add(first);
}
if (second instanceof JsonParserSequence) {
((JsonParserSequence) second).addFlattenedActiveParsers(p);
} else {
p.add(second);
}
ArrayList<JsonParser> p = new ArrayList<>(8);
flattenParsers(first, p);
flattenParsers(second, p);
return new JsonParserSequence(checkForExistingToken,
p.toArray(new JsonParser[p.size()]));
}
Expand All @@ -116,13 +108,26 @@ public static JsonParserSequence createFlattened(JsonParser first, JsonParser se
@SuppressWarnings("resource")
protected void addFlattenedActiveParsers(List<JsonParser> listToAddIn)
{
for (int i = _nextParserIndex-1, len = _parsers.length; i < len; ++i) {
JsonParser p = _parsers[i];
if (p instanceof JsonParserSequence) {
((JsonParserSequence) p).addFlattenedActiveParsers(listToAddIn);
} else {
listToAddIn.add(p);
}
int i = _nextParserIndex-1;
int len = _parsers.length;
expandToAdd(listToAddIn, len - i);
for (; i < len; ++i) {
flattenParsers(_parsers[i], listToAddIn);
}
}

private static void flattenParsers(JsonParser parser, List<JsonParser> p) {
if (parser instanceof JsonParserSequence) {
((JsonParserSequence) parser).addFlattenedActiveParsers(p);
} else {
p.add(parser);
}
}

private static void expandToAdd(List<?> list, int additionalCapacity) {
if (additionalCapacity > 0 && list instanceof ArrayList<?>) {
int newCapacity = list.size() + additionalCapacity;
((ArrayList<?>) list).ensureCapacity(newCapacity);
}
}

Expand Down
Loading