Skip to content

Commit a4a99c4

Browse files
committed
build: enhance JVM options for improved performance in development and CI environments
1 parent 31839be commit a4a99c4

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

flake.nix

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,66 @@
3838
let
3939
jdk = pkgs.openjdk23;
4040
sbt = pkgs.sbt.override { jre = jdk; };
41+
visualvm = pkgs.visualvm.override { jdk = jdk; };
42+
43+
# Common JVM options for both app and sbt JVM
44+
commonJvmOpts = [
45+
# Memory settings - large heap for Scala compilation and IR processing
46+
# "-Xmx12G" # Maximum heap size
47+
"-Xms4G" # Initial heap size (reduces early GC pressure)
48+
"-Xss64m" # Stack size for deep recursive calls in compiler
49+
50+
# Enable experimental features for Java 23
51+
"-XX:+UnlockExperimentalVMOptions" # Allow use of experimental VM options
52+
53+
# Garbage Collection - ZGC for ultra-low latency
54+
"-XX:+UseZGC" # Use Z Garbage Collector (concurrent, low-latency)
55+
56+
# Memory optimizations
57+
"-XX:+UseStringDeduplication" # Deduplicate identical strings to save memory
58+
"-XX:+OptimizeStringConcat" # Optimize string concatenation operations
59+
60+
# Code cache settings for better JIT performance
61+
"-XX:ReservedCodeCacheSize=512m" # Reserve more space for compiled native code
62+
"-XX:InitialCodeCacheSize=64m" # Start with larger initial code cache
63+
64+
# Compilation settings
65+
"-XX:+TieredCompilation" # Use tiered compilation (C1 + C2 compilers)
66+
67+
# Memory efficiency
68+
"-XX:+UseCompressedOops" # Use 32-bit pointers on 64-bit JVM (saves memory)
69+
"-XX:+UseCompressedClassPointers" # Compress class metadata pointers
70+
71+
# Java 23 preview features
72+
"--enable-preview" # Enable preview language features
73+
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
74+
# Linux-specific optimizations (not available on macOS)
75+
"-XX:+UseTransparentHugePages" # Use OS huge pages for better memory performance
76+
];
77+
78+
# App-specific JVM options (runtime performance focused)
79+
appJvmOpts = commonJvmOpts ++ [
80+
# JIT compiler optimizations for better runtime performance
81+
"-XX:MaxInlineLevel=15" # Allow deeper method inlining (Scala benefits from this)
82+
"-XX:MaxInlineSize=270" # Allow larger methods to be inlined
83+
"-XX:CompileThreshold=1000" # Compile methods to native code after 1000 invocations
84+
];
85+
86+
# SBT-specific JVM options (optimized for faster startup)
87+
sbtJvmOpts = commonJvmOpts ++ [
88+
# Startup optimization - prioritize fast startup over peak performance
89+
"-XX:TieredStopAtLevel=1" # Stop at C1 compiler (faster startup, less optimization)
90+
"-XX:CompileThreshold=1500" # Higher threshold for native compilation (faster startup)
91+
92+
# SBT-specific optimizations
93+
"-Dsbt.boot.lock=false" # Disable boot lock file (faster concurrent sbt instances)
94+
"-Dsbt.cached.resolution.cache.limit=50" # Limit dependency resolution cache size
95+
];
4196
in
4297
pkgs.mkShell {
4398
JAVA_HOME = "${jdk}";
44-
JAVA_OPTS = "-Xmx4g -Xss512m -XX:+UseG1GC";
99+
JAVA_OPTS = builtins.concatStringsSep " " appJvmOpts;
100+
SBT_OPTS = builtins.concatStringsSep " " sbtJvmOpts;
45101
CARGO_NET_GIT_FETCH_WITH_CLI = "true";
46102
CARGO_REGISTRIES_CRATES_IO_PROTOCOL = "sparse";
47103
# Fixes issues with Node.js 20+ and OpenSSL 3 during webpack build
@@ -90,10 +146,45 @@
90146
let
91147
jdk = pkgs.openjdk11;
92148
sbt = pkgs.sbt.override { jre = jdk; };
149+
150+
# Common JVM options for CI environment (Java 11 - more conservative settings)
151+
ciCommonJvmOpts = [
152+
# Conservative memory settings for CI environments
153+
"-Xmx12G" # Maximum heap size (smaller than dev for CI resource limits)
154+
"-Xms2G" # Initial heap size (conservative for CI)
155+
"-Xss64m" # Stack size for deep recursive calls in compiler
156+
157+
# Garbage Collection - G1GC for Java 11 stability
158+
"-XX:+UseG1GC" # Use G1 Garbage Collector (stable, good for large heaps)
159+
160+
# Memory optimizations (Java 11 compatible)
161+
"-XX:+UseStringDeduplication" # Deduplicate identical strings to save memory
162+
163+
# Code cache settings
164+
# "-XX:ReservedCodeCacheSize=512m" # Reserve space for compiled native code
165+
# "-XX:InitialCodeCacheSize=64m" # Start with larger initial code cache
166+
167+
# Compilation settings
168+
# "-XX:+TieredCompilation" # Use tiered compilation (C1 + C2 compilers)
169+
170+
# Memory efficiency
171+
"-XX:+UseCompressedOops" # Use 32-bit pointers on 64-bit JVM (saves memory)
172+
];
173+
174+
# CI SBT-specific options (prioritize build speed and reliability)
175+
ciSbtJvmOpts = ciCommonJvmOpts ++ [
176+
# Fast startup for CI builds
177+
"-XX:TieredStopAtLevel=1" # Stop at C1 compiler (faster CI startup)
178+
"-XX:CompileThreshold=1500" # Higher threshold for native compilation
179+
180+
# CI-specific optimizations
181+
"-Dsbt.boot.lock=false" # Disable boot lock (faster in containerized CI)
182+
];
93183
in
94184
pkgs.mkShell {
95185
JAVA_HOME = "${jdk}";
96-
JAVA_OPTS = "-Xmx4g -Xss512m -XX:+UseG1GC";
186+
JAVA_OPTS = builtins.concatStringsSep " " ciCommonJvmOpts;
187+
SBT_OPTS = builtins.concatStringsSep " " ciSbtJvmOpts;
97188
CARGO_NET_GIT_FETCH_WITH_CLI = "true";
98189
CARGO_REGISTRIES_CRATES_IO_PROTOCOL = "sparse";
99190
# Fixes issues with Node.js 20+ and OpenSSL 3 during webpack build
@@ -115,7 +206,6 @@
115206
ln -s ${plutus}/plutus-conformance plutus-conformance
116207
export LIBRARY_PATH="${tiny_keccak_wrapper}/lib:${pkgs.secp256k1}/lib:${pkgs.libsodium}/lib:$LIBRARY_PATH"
117208
export LD_LIBRARY_PATH="${tiny_keccak_wrapper}/lib:${pkgs.secp256k1}/lib:${pkgs.libsodium}/lib:$LD_LIBRARY_PATH"
118-
export SBT_OPTS="-Xss64m $SBT_OPTS"
119209
'';
120210
};
121211
};

0 commit comments

Comments
 (0)