This is an automated email from the ASF dual-hosted git repository.

uros-b pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 418db10b6c55 [SPARK-57611][SQL] Reuse JsonFactory instances in the 
variant module
418db10b6c55 is described below

commit 418db10b6c558a79dcf2f5479d30165b28923590
Author: YangJie <[email protected]>
AuthorDate: Thu Jun 25 00:17:43 2026 +0200

    [SPARK-57611][SQL] Reuse JsonFactory instances in the variant module
    
    ### What changes were proposed in this pull request?
    
    This PR reuses a single shared `JsonFactory` in the variant module instead 
of allocating a new one on every call. `Variant.escapeJson` and 
`VariantBuilder.parseJson` each created a `new JsonFactory()` per invocation; 
both now reference a `private static final` instance.
    
    ### Why are the changes needed?
    
    `JsonFactory` is thread-safe and designed to be reused. `escapeJson` runs 
once per string value and object key while serializing a variant, and 
`parseJson` runs once per input JSON value (e.g. per row in `parse_json`), so 
creating a fresh factory on each call adds avoidable allocation (including its 
symbol tables) on these hot paths. Sharing one instance removes that overhead.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Existing variant tests cover both the parsing and serialization paths. 
There is no behavior change: both factories use the default configuration and 
are never mutated, so a shared instance produces identical results.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No.
    
    Closes #56691 from LuciferYang/SPARK-variant-escapejson-factory.
    
    Authored-by: YangJie <[email protected]>
    Signed-off-by: Uros Bojanic <[email protected]>
    (cherry picked from commit ff354ecbf80347b2cda487b417981b6c35d7b0f6)
    Signed-off-by: Uros Bojanic <[email protected]>
---
 .../src/main/java/org/apache/spark/types/variant/Variant.java     | 3 +--
 .../main/java/org/apache/spark/types/variant/VariantBuilder.java  | 3 +--
 .../src/main/java/org/apache/spark/types/variant/VariantUtil.java | 8 ++++++++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git 
a/common/variant/src/main/java/org/apache/spark/types/variant/Variant.java 
b/common/variant/src/main/java/org/apache/spark/types/variant/Variant.java
index acb585cb8803..8996caeb364c 100644
--- a/common/variant/src/main/java/org/apache/spark/types/variant/Variant.java
+++ b/common/variant/src/main/java/org/apache/spark/types/variant/Variant.java
@@ -17,7 +17,6 @@
 
 package org.apache.spark.types.variant;
 
-import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonGenerator;
 
 import java.io.CharArrayWriter;
@@ -246,7 +245,7 @@ public final class Variant {
   // (4 characters).
   static String escapeJson(String str) {
     try (CharArrayWriter writer = new CharArrayWriter();
-         JsonGenerator gen = new JsonFactory().createGenerator(writer)) {
+         JsonGenerator gen = JSON_FACTORY.createGenerator(writer)) {
       gen.writeString(str);
       gen.flush();
       return writer.toString();
diff --git 
a/common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java
 
b/common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java
index dc0a114706c7..79f869ba6be7 100644
--- 
a/common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java
+++ 
b/common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java
@@ -30,7 +30,6 @@ import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.*;
 
-import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonToken;
@@ -70,7 +69,7 @@ public class VariantBuilder {
    */
   public static Variant parseJson(String json, boolean allowDuplicateKeys,
       boolean validateUnicodeInJsonParsing) throws IOException {
-    try (JsonParser parser = new JsonFactory().createParser(json)) {
+    try (JsonParser parser = JSON_FACTORY.createParser(json)) {
       parser.nextToken();
       return parseJson(parser, allowDuplicateKeys, 
validateUnicodeInJsonParsing);
     }
diff --git 
a/common/variant/src/main/java/org/apache/spark/types/variant/VariantUtil.java 
b/common/variant/src/main/java/org/apache/spark/types/variant/VariantUtil.java
index ac93246991c0..1a3912775a2f 100644
--- 
a/common/variant/src/main/java/org/apache/spark/types/variant/VariantUtil.java
+++ 
b/common/variant/src/main/java/org/apache/spark/types/variant/VariantUtil.java
@@ -29,6 +29,8 @@ import java.nio.ByteOrder;
 import java.util.Arrays;
 import java.util.UUID;
 
+import com.fasterxml.jackson.core.JsonFactory;
+
 /**
  * This class defines constants related to the variant format and provides 
functions for
  * manipulating variant binaries.
@@ -50,6 +52,12 @@ import java.util.UUID;
  * - UTF-8 string data.
  */
 public class VariantUtil {
+  // Jackson's JsonFactory is thread-safe and intended to be reused, so the 
variant module shares a
+  // single instance. Variant.escapeJson (one call per serialized string or 
key) and
+  // VariantBuilder.parseJson (one call per parsed JSON value) both use it, 
which avoids a per-call
+  // factory allocation (including its symbol tables) and keeps one source of 
configuration.
+  static final JsonFactory JSON_FACTORY = new JsonFactory();
+
   public static final int BASIC_TYPE_BITS = 2;
   public static final int BASIC_TYPE_MASK = 0x3;
   public static final int TYPE_INFO_MASK = 0x3F;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to