Repository navigation
Conversation
…l flag With DD_EXPERIMENTAL_STREAM_FUSION_ENABLED=true, a retransformation-incapable transformer rewrites pipelines such as list.stream().filter(p).map(f) .collect(toList()) in application classes, in place and without moving any code: stream() becomes a source check, each intermediate operation a hook at its original position, and the terminal an invokedynamic linked to a class generated for that call site. Each site gets its own class, so the user's functions are called from code with per-site type profiles. Trusted JDK sources are traversed with their own spliterator, so binding, snapshot, and concurrent-modification behavior are unchanged; any other source runs the original stream calls at their original points. Only methods that call a stream terminal are analyzed, untouched methods are copied byte for byte, and a method is left unchanged if its frames can't be computed without guessing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Site classes and their generated bytes are now registered per class loader in a weakly keyed map, and defined site classes are held through weak references, so fused sites never keep an application class loader alive. Registration stays lock-free; only defining a site class takes a per-loader lock, so it can't deadlock with a loader's own lock during class loading. Sites are registered only after the whole class rewrite has succeeded. Also: trust the HashMap / LinkedHashMap key, value, and entry views as sources, read each class file at most once when computing frames, derive the terminal check from the Terminal enum, and drop unused counters and checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
Contributor
🟢 Java Benchmark SLOs — All performance SLOs passed
PR vs. master results
Commit: Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Does This Do
Adds an experimental, off-by-default load-time optimization: simple
java.util.streampipelines in application classes are fused into per-call-site code. Enable withDD_EXPERIMENTAL_STREAM_FUSION_ENABLED=true.A pipeline such as
list.stream().filter(p).map(f).collect(toList())is rewritten in place, without moving any code:stream()becomes a source check;invokedynamiclinked to a class generated for that call site, which traverses the source with its own spliterator and calls the user's functions directly.Supported today: sources
Collection.stream()(trusted JDK collections) andString.chars(); operationsfilter,map,mapToInt/Long/Double/Obj; terminalscollect(toList()/toSet()/joining()),anyMatch/allMatch/noneMatch,findFirst/findAny,forEach, int/longsum.Layout:
agent-bootstrap:datadog.trace.bootstrap.jit.StreamFusion, the runtime support visible from every class loader.agent-tooling:datadog.trace.agent.tooling.jit.stream, the transformer, the ASM dataflow matcher and rewriter, and the site-class generator.AgentInstaller: registers a retransformation-incapable transformer when the setting is on.Motivation
The JDK's stream library runs every pipeline in an application through one shared set of classes, so their type profiles are megamorphic and the JIT can't specialize or remove the pipeline objects. Per-site code restores per-pipeline specialization: each pipeline performs as if it were the only one in the program.
How it stays correct
The criterion is that every behavior of the rewritten program must already be a possible behavior of the original.
ConcurrentModificationExceptiontiming are unchanged by construction.ArrayList,LinkedList,HashSet,LinkedHashSet,Arrays.asList,List.of/Set.of, andHashMap/LinkedHashMapkey, value and entry views, plusString.chars(). Anything else (subclasses, user collections, wrappers such asCollections.unmodifiable*) runs the original stream calls at their original points.Testing
StreamFusionTransformerTest: about 400 equivalence cases (10+ sources including map views and fallback sources; interference by add, grow, remove, clear, set and remove+add, also through a backing map; null sources, functions and elements; evaluation order of arguments; exceptions part-way through; nested and looped pipelines; non-ASCII strings). Each runs untransformed and transformed through the real transformer, comparing results, exception types and messages, side-effect logs and final source state.Results
mnemonicswith the tracer: 2,152 → 1,723 ms median, +20% (+13% / +18% / +28% across rounds; noisy laptop).Follow-ups (not in this PR)
Stream.toList(),flatMap, array sources.instanceofchecks) on untrusted sources.🤖 Generated with Claude Code