various small performance improvenemts
* start with bigger buffers
* bufferLeft to avoid recalculation every time
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java
index d05cfbf..7088bfd 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java
@@ -166,7 +166,7 @@
if (unmodifieableBackingList.isEmpty()) {
return "[]";
}
- final StringWriter writer = new StringWriter();
+ final StringWriter writer = new StringWriter(2048);
try (final JsonGenerator generator = new JsonGeneratorImpl(writer, provider, false)) {
generator.writeStartArray();
unmodifieableBackingList.forEach(generator::write);
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java
index 4e45f08..f593327 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java
@@ -146,7 +146,7 @@
if (unmodifieableBackingMap.isEmpty()) {
return "{}";
}
- final StringWriter writer = new StringWriter();
+ final StringWriter writer = new StringWriter(2048);
try (final JsonGenerator generator = new JsonGeneratorImpl(writer, provider, false)) {
generator.writeStartObject();
unmodifieableBackingMap.forEach(generator::write);
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
index 3e75b5c..8f2edb6 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
@@ -42,6 +42,9 @@
//-1 would cause a re-read of the first character in the buffer (which is at zero index)
private int bufferPos = Integer.MIN_VALUE;
+ // performance optimisation to avoid subtraction on readNextChar
+ private int bufferLeft = 0;
+
//available character in the buffer. It might be <= "buffer.length".
private int availableCharsInBuffer;
@@ -220,7 +223,7 @@
previousEvent != VALUE_NULL && previousEvent != VALUE_NUMBER) {
if (bufferPos < 0) { // check we don't have an empty string to parse
final char c = readNextChar();
- bufferPos--;
+ unreadChar();
return c != EOF;
}
return true;
@@ -289,7 +292,7 @@
//refill is necessary copy the already read value part into the value buffer
protected final char readNextChar() {
- if ((availableCharsInBuffer - bufferPos) <= 1) {
+ if (bufferLeft == 0) {
//fillbuffer
//copy content from old buffer to valuebuffer
@@ -317,6 +320,7 @@
}
bufferPos = 0;
+ bufferLeft = availableCharsInBuffer - bufferPos - 1;
//end fillbuffer
} else {
@@ -327,6 +331,7 @@
//}
bufferPos++;
+ bufferLeft--;
}
return buffer[bufferPos];
@@ -370,13 +375,18 @@
: null;
}
+ private void unreadChar() {
+ bufferPos--;
+ bufferLeft++;
+ }
+
@Override
protected final Event internalNext() {
//main entry, make decision how to handle the current character in the stream
if (!hasNext()) {
final char c = readNextChar();
- bufferPos--;
+ unreadChar();
if (c != EOF) {
throw uexc("No available event");
}
@@ -657,7 +667,7 @@
//current n is one of < '\u001F' -OR- ESCAPE_CHAR -OR- EOL -OR- QUOTE
- bufferPos--; //unread one char
+ unreadChar(); //unread one char
}
} while (true);
@@ -686,8 +696,11 @@
//always the beginning quote of a key or value
//last event must one of the following-> : { [ ,
- if (previousEvent != -1 && (previousEvent != KEY_SEPARATOR_EVENT && previousEvent != START_OBJECT && previousEvent != START_ARRAY
- && previousEvent != COMMA_EVENT)) {
+ if (previousEvent != -1 &&
+ (previousEvent != KEY_SEPARATOR_EVENT &&
+ previousEvent != START_OBJECT &&
+ previousEvent != START_ARRAY &&
+ previousEvent != COMMA_EVENT)) {
throw uexc("Expected : { [ ,");
}
//starting quote already consumed
@@ -792,7 +805,7 @@
if (y == COMMA_CHAR || y == END_ARRAY_CHAR || y == END_OBJECT_CHAR || y == EOL || y == SPACE || y == TAB || y == CR || y == EOF) {
- bufferPos--;//unread one char
+ unreadChar();//unread one char
//['-', DIGIT]
if (isCurrentNumberIntegral && c == MINUS && cumulatedDigitValue >= 48 && cumulatedDigitValue <= 57) {
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java
index 3e7aaf7..7d24e23 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java
@@ -223,19 +223,19 @@
}
public String writeArrayAsString(final Collection<?> instance) {
- final StringWriter writer = new StringWriter();
+ final StringWriter writer = new StringWriter(2048);
writeArray(instance, writer);
return writer.toString();
}
public <T> String writeArrayAsString(final T[] instance) {
- final StringWriter writer = new StringWriter();
+ final StringWriter writer = new StringWriter(2048);
writeArray(instance, writer);
return writer.toString();
}
public String writeObjectAsString(final Object instance) {
- final StringWriter writer = new StringWriter();
+ final StringWriter writer = new StringWriter(2048);
writeObject(instance, writer);
return writer.toString();
}