Skip to content

Optimize hot paths: cache lists, fix integer overflow, reduce reflection#758

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/identify-slow-code-issues
Draft

Optimize hot paths: cache lists, fix integer overflow, reduce reflection#758
Copilot wants to merge 4 commits intomasterfrom
copilot/identify-slow-code-issues

Conversation

Copy link
Contributor

Copilot AI commented Jan 8, 2026

Performance bottlenecks in datasource selection, load balancing, and configuration merging cause unnecessary allocations and potential crashes under high load.

Changes

GroupDataSource - Eliminate allocation in hot path

  • Cache immutable list of datasource keys, rebuild only on add/remove
  • Prevents ArrayList creation on every determineDsKey() call
  • Uses Collections.unmodifiableList() with volatile for thread safety

LoadBalanceDynamicDataSourceStrategy - Fix overflow crash

  • Replace Math.abs(index.getAndAdd(1) % size) with (index.getAndIncrement() & Integer.MAX_VALUE) % size
  • Math.abs(Integer.MIN_VALUE) returns negative, causing ArrayIndexOutOfBoundsException after 2B+ requests

ConfigMergeCreator - Cache reflection results

  • Add cached BeanInfo field with double-checked locking
  • Eliminates repeated Introspector.getBeanInfo() calls during configuration merges
  • Add null validation in constructor

DynamicRoutingDataSource - Optimize string parsing

  • Replace ds.split("_")[0] with ds.substring(0, ds.indexOf("_"))
  • Avoids regex compilation and array allocation for simple parsing
// Before: creates new ArrayList on every call
public String determineDsKey() {
    return strategy.determineKey(new ArrayList<>(dataSourceMap.keySet()));
}

// After: uses cached immutable list
private volatile List<String> cachedDsKeys = Collections.emptyList();
public String determineDsKey() {
    return strategy.determineKey(cachedDsKeys);
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • maven.aliyun.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED -XX:MaxMetaspaceSize=512m -XX:&#43;HeapDumpOnOutOfMemoryError -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant (dns block)
    • Triggering command: /usr/lib/jvm/temurin-8-jdk-amd64/bin/java /usr/lib/jvm/temurin-8-jdk-amd64/bin/java -XX:MaxMetaspaceSize=512m -XX:&#43;HeapDumpOnOutOfMemoryError -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.13-bin/5xuhj0ry160q40clulazy9h7d/gradle-8.13/lib/gradle-daemon-main-8.13.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.13-bin/5xuhj0ry160q40clulazy9h7d/gradle-8.13/lib/agents/gradle-instrumentation-agent-8.13.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.13 ng/src/main/java/com/baomidou/dynamic/datasource/DynamicRoutingDataSource.java urce-creator/src/main/java/com/btest rgo/bin/grep .split xyChecker.java /snap/bin/git r (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Identify and suggest improvements to slow or inefficient code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 8, 2026 07:15
Co-authored-by: huayanYu <16700837+huayanYu@users.noreply.github.com>
Co-authored-by: huayanYu <16700837+huayanYu@users.noreply.github.com>
Co-authored-by: huayanYu <16700837+huayanYu@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize hot paths: cache lists, fix integer overflow, reduce reflection Jan 8, 2026
Copilot AI requested a review from huayanYu January 8, 2026 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants