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

snuyanzin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 9e111593b67 [FLINK-40895][table] Use cache in code splitter
9e111593b67 is described below

commit 9e111593b673e23d2aa21f03259ac5ab11228bbf
Author: Sergey Nuyanzin <[email protected]>
AuthorDate: Sat Oct 3 09:10:47 2026 +0200

    [FLINK-40895][table] Use cache in code splitter
---
 .../flink/table/codesplit/JavaCodeSplitter.java    | 59 ++++++++++++++++++++--
 .../table/codesplit/JavaCodeSplitterTest.java      |  5 +-
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git 
a/flink-table/flink-table-code-splitter/src/main/java/org/apache/flink/table/codesplit/JavaCodeSplitter.java
 
b/flink-table/flink-table-code-splitter/src/main/java/org/apache/flink/table/codesplit/JavaCodeSplitter.java
index 99478014782..c8e2bf69359 100644
--- 
a/flink-table/flink-table-code-splitter/src/main/java/org/apache/flink/table/codesplit/JavaCodeSplitter.java
+++ 
b/flink-table/flink-table-code-splitter/src/main/java/org/apache/flink/table/codesplit/JavaCodeSplitter.java
@@ -18,8 +18,16 @@
 package org.apache.flink.table.codesplit;
 
 import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
 
+import org.apache.flink.shaded.guava33.com.google.common.cache.Cache;
+import org.apache.flink.shaded.guava33.com.google.common.cache.CacheBuilder;
+import 
org.apache.flink.shaded.guava33.com.google.common.util.concurrent.UncheckedExecutionException;
+
+import java.time.Duration;
+import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 
 import static org.apache.flink.util.Preconditions.checkArgument;
 
@@ -30,16 +38,28 @@ import static 
org.apache.flink.util.Preconditions.checkArgument;
 @Internal
 public class JavaCodeSplitter {
 
+    // split() is a pure function of its inputs; cache results so identical 
code isn't re-split.
+    private static final Cache<SplitKey, String> SPLIT_CACHE =
+            CacheBuilder.newBuilder()
+                    .expireAfterAccess(Duration.ofMinutes(5))
+                    .maximumSize(300)
+                    .softValues()
+                    .build();
+
     public static String split(String code, int maxMethodLength, int 
maxClassMemberCount) {
+        SplitKey key = new SplitKey(code, maxMethodLength, 
maxClassMemberCount);
         try {
-            return splitImpl(code, maxMethodLength, maxClassMemberCount);
-        } catch (Throwable t) {
+            return SPLIT_CACHE.get(
+                    key, () -> splitImpl(code, maxMethodLength, 
maxClassMemberCount));
+        } catch (ExecutionException | UncheckedExecutionException e) {
+            Throwable cause = e.getCause() != null ? e.getCause() : e;
             throw new RuntimeException(
-                    "JavaCodeSplitter failed. This is a bug. Please file an 
issue.", t);
+                    "JavaCodeSplitter failed. This is a bug. Please file an 
issue.", cause);
         }
     }
 
-    private static String splitImpl(String code, int maxMethodLength, int 
maxClassMemberCount) {
+    @VisibleForTesting
+    static String splitImpl(String code, int maxMethodLength, int 
maxClassMemberCount) {
         checkArgument(code != null && !code.isEmpty(), "code cannot be empty");
         checkArgument(maxMethodLength > 0, "maxMethodLength must be greater 
than 0");
         checkArgument(maxClassMemberCount > 0, "maxClassMemberCount must be 
greater than 0");
@@ -60,4 +80,35 @@ public class JavaCodeSplitter {
                 .map(text -> new MemberFieldRewriter(text, 
maxClassMemberCount).rewrite())
                 .orElse(code);
     }
+
+    private static final class SplitKey {
+        private final String code;
+        private final int maxMethodLength;
+        private final int maxClassMemberCount;
+
+        SplitKey(String code, int maxMethodLength, int maxClassMemberCount) {
+            this.code = code;
+            this.maxMethodLength = maxMethodLength;
+            this.maxClassMemberCount = maxClassMemberCount;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (!(o instanceof SplitKey)) {
+                return false;
+            }
+            SplitKey that = (SplitKey) o;
+            return maxMethodLength == that.maxMethodLength
+                    && maxClassMemberCount == that.maxClassMemberCount
+                    && Objects.equals(code, that.code);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(code, maxMethodLength, maxClassMemberCount);
+        }
+    }
 }
diff --git 
a/flink-table/flink-table-code-splitter/src/test/java/org/apache/flink/table/codesplit/JavaCodeSplitterTest.java
 
b/flink-table/flink-table-code-splitter/src/test/java/org/apache/flink/table/codesplit/JavaCodeSplitterTest.java
index f02f5589056..6baac8240f4 100644
--- 
a/flink-table/flink-table-code-splitter/src/test/java/org/apache/flink/table/codesplit/JavaCodeSplitterTest.java
+++ 
b/flink-table/flink-table-code-splitter/src/test/java/org/apache/flink/table/codesplit/JavaCodeSplitterTest.java
@@ -52,8 +52,9 @@ class JavaCodeSplitterTest {
                                         .toURI()));
 
         try {
-            String first = JavaCodeSplitter.split(code, 100, 3);
-            String second = JavaCodeSplitter.split(code, 100, 3);
+            // call splitImpl directly to bypass the result cache and actually 
re-run the splitter
+            String first = JavaCodeSplitter.splitImpl(code, 100, 3);
+            String second = JavaCodeSplitter.splitImpl(code, 100, 3);
 
             assertThat(second).isEqualTo(first);
         } finally {

Reply via email to