You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by st...@apache.org on 2026/07/23 19:07:34 UTC
(johnzon) branch master updated (f98c1b48 -> 88332275)
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
from f98c1b48 update h2 and cxf to newer bugfix releases
new 41b2e2af JOHNZON-435 improve writePrimitives
new 1052109d JOHNZON-435 remove old unused code
new 88332275 JOHNZON-435 reuse toUnicode with caching
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/apache/johnzon/core/JsonGeneratorImpl.java | 108 +--------------------
.../main/java/org/apache/johnzon/core/Strings.java | 2 +-
.../johnzon/mapper/MappingGeneratorImpl.java | 31 +++---
3 files changed, 18 insertions(+), 123 deletions(-)
(johnzon) 02/03: JOHNZON-435 remove old unused code
Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 1052109d14b0f016921595e5bdc8f715619acd01
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Jul 23 20:32:30 2026 +0200
JOHNZON-435 remove old unused code
I benchmarked the old implementation and even wrote a new justWrite(char[]).
It performs worse than the (highly optimised native) Long.toString
---
.../org/apache/johnzon/core/JsonGeneratorImpl.java | 101 ---------------------
1 file changed, 101 deletions(-)
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
index 9feb34c8..25af0184 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
@@ -708,105 +708,4 @@ class JsonGeneratorImpl implements JsonGenerator, JsonChars, Serializable {
private void writeInt0(final int i) {
justWrite(String.valueOf(i));
}
-
- //optimized number optimizations
- /*
- private void writeLong0(final long i) {
- if (i == Long.MIN_VALUE) {
- justWrite("-9223372036854775808");
- return;
- }
- final int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
- final char[] buf = new char[size];
- getChars(i, size, buf);
- justWrite(buf);
- }
-
- private void writeInt0(final int i) {
- if (i == Integer.MIN_VALUE) {
- justWrite("-2147483648");
- return;
- }
- final int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
- final char[] buf = new char[size];
- getChars(i, size, buf);
- justWrite(buf);
- }
-
- private final static char[] DIGIT_TENS = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1',
- '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4',
- '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6',
- '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9',
- '9', '9', '9', '9', '9', '9', '9', };
-
- private final static char[] DIGIT_ONES = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2',
- '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2',
- '3', '4', '5', '6', '7', '8', '9', };
-
- private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
- 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
-
- // Requires positive x
- private static int stringSize(final long x) {
- long p = 10;
- for (int i = 1; i < 19; i++) {
- if (x < p) {
- return i;
- }
- p = 10 * p;
- }
- return 19;
- }
-
- private static void getChars(long i, final int index, final char[] buf) {
- long q;
- int r;
- int charPos = index;
- char sign = 0;
-
- if (i < 0) {
- sign = '-';
- i = -i;
- }
-
- // Get 2 digits/iteration using longs until quotient fits into an int
- while (i > Integer.MAX_VALUE) {
- q = i / 100;
- // really: r = i - (q * 100);
- r = (int) (i - ((q << 6) + (q << 5) + (q << 2)));
- i = q;
- buf[--charPos] = DIGIT_ONES[r];
- buf[--charPos] = DIGIT_TENS[r];
- }
-
- // Get 2 digits/iteration using ints
- int q2;
- int i2 = (int) i;
- while (i2 >= 65536) {
- q2 = i2 / 100;
- // really: r = i2 - (q * 100);
- r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
- i2 = q2;
- buf[--charPos] = DIGIT_ONES[r];
- buf[--charPos] = DIGIT_TENS[r];
- }
-
- // Fall thru to fast mode for smaller numbers
- // assert(i2 <= 65536, i2);
- for (;;) {
- q2 = (i2 * 52429) >>> (16 + 3);
- r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
- buf[--charPos] = DIGITS[r];
- i2 = q2;
- if (i2 == 0) {
- break;
- }
- }
- if (sign != 0) {
- buf[--charPos] = sign;
- }
- }
- */
}
Fwd: (johnzon) 03/03: JOHNZON-435 reuse toUnicode with caching
Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi Mark
did you "jmh" it? recall the cache the counter productive when originally
setup on several code paths. If it is now it can be worth explaining the
gain ratio in a comment even if depending on the machine to avoid it to be
removed (ping pong code ;))
Romain Manni-Bucau
@rmannibucau <https://x.com/rmannibucau> | .NET Blog
<https://dotnetbirdie.github.io/> | Blog <https://rmannibucau.github.io/> | Old
Blog <http://rmannibucau.wordpress.com> | Github
<https://github.com/rmannibucau> | LinkedIn
<https://www.linkedin.com/in/rmannibucau> | Book
<https://www.packtpub.com/en-us/product/java-ee-8-high-performance-9781788473064>
Javaccino founder (Java/.NET service - contact via linkedin)
---------- Forwarded message ---------
De : <st...@apache.org>
Date: jeu. 23 juil. 2026 à 21:08
Subject: (johnzon) 03/03: JOHNZON-435 reuse toUnicode with caching
To: commits@johnzon.apache.org <co...@johnzon.apache.org>
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 8833227516f01925398b87030708b411922c1616
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Jul 23 21:06:24 2026 +0200
JOHNZON-435 reuse toUnicode with caching
---
.../src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java | 7
++-----
johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java | 2 +-
2 files changed, 3 insertions(+), 6 deletions(-)
diff --git
a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
index 25af0184..34305258 100644
---
a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
+++
b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
@@ -34,6 +34,8 @@ import java.math.BigInteger;
import java.util.Iterator;
import java.util.Map;
+import static org.apache.johnzon.core.Strings.toUnicode;
+
class JsonGeneratorImpl implements JsonGenerator, JsonChars, Serializable {
private final transient Writer writer;
private final BufferStrategy.BufferProvider<char[]> bufferProvider;
@@ -547,11 +549,6 @@ class JsonGeneratorImpl implements JsonGenerator,
JsonChars, Serializable {
private static final String UNICODE_PREFIX = "\\u";
private static final String UNICODE_PREFIX_HELPER = "000";
- private static String toUnicode(final char c) {
- final String hex = UNICODE_PREFIX_HELPER + Integer.toHexString(c);
- final String s = UNICODE_PREFIX + hex.substring(hex.length() - 4);
- return s;
- }
private void justWrite(final String value) {
final int valueLength = value.length();
diff --git
a/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
b/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
index 975ba348..44009e43 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
@@ -102,7 +102,7 @@ class Strings implements JsonChars {
}
}
- private static String toUnicode(final char c) {
+ public static String toUnicode(final char c) {
final String found = UNICODE_CACHE.get(c);
if (found != null) {
return found;
(johnzon) 03/03: JOHNZON-435 reuse toUnicode with caching
Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 8833227516f01925398b87030708b411922c1616
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Jul 23 21:06:24 2026 +0200
JOHNZON-435 reuse toUnicode with caching
---
.../src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java | 7 ++-----
johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java | 2 +-
2 files changed, 3 insertions(+), 6 deletions(-)
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
index 25af0184..34305258 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
@@ -34,6 +34,8 @@ import java.math.BigInteger;
import java.util.Iterator;
import java.util.Map;
+import static org.apache.johnzon.core.Strings.toUnicode;
+
class JsonGeneratorImpl implements JsonGenerator, JsonChars, Serializable {
private final transient Writer writer;
private final BufferStrategy.BufferProvider<char[]> bufferProvider;
@@ -547,11 +549,6 @@ class JsonGeneratorImpl implements JsonGenerator, JsonChars, Serializable {
private static final String UNICODE_PREFIX = "\\u";
private static final String UNICODE_PREFIX_HELPER = "000";
- private static String toUnicode(final char c) {
- final String hex = UNICODE_PREFIX_HELPER + Integer.toHexString(c);
- final String s = UNICODE_PREFIX + hex.substring(hex.length() - 4);
- return s;
- }
private void justWrite(final String value) {
final int valueLength = value.length();
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java b/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
index 975ba348..44009e43 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/Strings.java
@@ -102,7 +102,7 @@ class Strings implements JsonChars {
}
}
- private static String toUnicode(final char c) {
+ public static String toUnicode(final char c) {
final String found = UNICODE_CACHE.get(c);
if (found != null) {
return found;
(johnzon) 01/03: JOHNZON-435 improve writePrimitives
Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 41b2e2af4ea06749983068fa73e55998b3dff796
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Jul 23 19:51:57 2026 +0200
JOHNZON-435 improve writePrimitives
We don't need to check for real primitive types as we get Object.class as parameter.
So any primitive would have been wrapped in it's wrapper type already anyway.
---
.../johnzon/mapper/MappingGeneratorImpl.java | 31 +++++++++++-----------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
index 6289c4f9..7394bd14 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
@@ -279,17 +279,17 @@ public class MappingGeneratorImpl implements MappingGenerator {
return handled;
}
- private boolean writePrimitives(final String key, final Class<?> type, final Object value,
- final JsonGenerator generator) {
+
+ private boolean writePrimitives(final String key, final Class<?> type, final Object value, final JsonGenerator generator) {
boolean handled = false;
if (type == String.class) {
generator.write(key, value.toString());
handled = true;
} else if (JsonValue.class.isAssignableFrom(type)) {
- generator.write(key, JsonValue.class.cast(value));
+ generator.write(key, (JsonValue)value);
handled = true;
- } else if (type == long.class || type == Long.class) {
- final long longValue = Long.class.cast(value).longValue();
+ } else if (type == Long.class) {
+ final long longValue = ((Long) value).longValue();
if (isInJsRange(longValue)) {
generator.write(key, longValue);
} else {
@@ -297,21 +297,21 @@ public class MappingGeneratorImpl implements MappingGenerator {
}
handled = true;
} else if (isInt(type)) {
- generator.write(key, Number.class.cast(value).intValue());
+ generator.write(key, ((Number) value).intValue());
handled = true;
} else if (isFloat(type)) {
- if (type == Float.class || type == float.class) {
- if (!Float.isNaN(Float.class.cast(value))) {
+ if (type == Float.class) {
+ if (!Float.isNaN((Float) value)) {
generator.write(key, new BigDecimal(value.toString()));
}
} else {
- final double doubleValue = Number.class.cast(value).doubleValue();
+ final double doubleValue = ((Number) value).doubleValue();
if (!Double.isNaN(doubleValue)) {
generator.write(key, doubleValue);
}
}
handled = true;
- } else if (type == boolean.class || type == Boolean.class) {
+ } else if (type == Boolean.class) {
generator.write(key, Boolean.class.cast(value));
handled = true;
} else if (type == BigDecimal.class) {
@@ -320,7 +320,7 @@ public class MappingGeneratorImpl implements MappingGenerator {
} else if (type == BigInteger.class) {
generator.write(key, BigInteger.class.cast(value));
handled = true;
- } else if (type == char.class || type == Character.class) {
+ } else if (type == Character.class) {
generator.write(key, Character.class.cast(value).toString());
handled = true;
}
@@ -329,14 +329,13 @@ public class MappingGeneratorImpl implements MappingGenerator {
private static boolean isInt(final Class<?> type) {
- return type == int.class || type == Integer.class
- || type == byte.class || type == Byte.class
- || type == short.class || type == Short.class;
+ // no need to check for primitives, as those are already converted to wrapper types
+ return type == Integer.class || type == Byte.class || type == Short.class;
}
private static boolean isFloat(final Class<?> type) {
- return type == double.class || type == Double.class
- || type == float.class || type == Float.class;
+ // no need to check for primitives, as those are already converted to wrapper types
+ return type == Double.class || type == Float.class;
}