-
Notifications
You must be signed in to change notification settings - Fork 66
Compiler: auto-size dispatch chunks to stay under JIT HugeMethodLimit #1288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
jmh/src/main/java/com/dylibso/chicory/bench/BenchmarkDispatchChunkSize.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package com.dylibso.chicory.bench; | ||
|
|
||
| import com.dylibso.chicory.compiler.MachineFactoryCompiler; | ||
| import com.dylibso.chicory.runtime.ExportFunction; | ||
| import com.dylibso.chicory.runtime.Instance; | ||
| import com.dylibso.chicory.wabt.Wat2Wasm; | ||
| import com.dylibso.chicory.wasm.Parser; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| // Measures dispatch overhead for large wasm modules (2000 functions). | ||
| // Compare with/without the HugeMethodLimit-aware chunking: | ||
| // java -Dchicory.hugeMethodLimit=1000000 -jar benchmarks.jar BenchmarkDispatchChunkSize (no | ||
| // limit) | ||
| // java -jar benchmarks.jar BenchmarkDispatchChunkSize (default | ||
| // 8KB) | ||
| // | ||
| // Results (JDK 25, Apple M3 Max): | ||
| // Benchmark (targetFunc) Mode Cnt Score Units | ||
| // -- hugeMethodLimit=1000000 (chunks of 1024, exceeds C2 HugeMethodLimit) -- | ||
| // BenchmarkDispatchChunkSize.dispatch 0 avgt 5 0.033 us/op | ||
| // BenchmarkDispatchChunkSize.dispatch 999 avgt 5 0.034 us/op | ||
| // BenchmarkDispatchChunkSize.dispatch 1999 avgt 5 0.035 us/op | ||
| // -- default (chunks of 128, under C2 HugeMethodLimit) -- | ||
| // BenchmarkDispatchChunkSize.dispatch 0 avgt 5 0.004 us/op | ||
| // BenchmarkDispatchChunkSize.dispatch 999 avgt 5 0.004 us/op | ||
| // BenchmarkDispatchChunkSize.dispatch 1999 avgt 5 0.004 us/op | ||
| @State(Scope.Benchmark) | ||
| @Warmup(iterations = 3, time = 2) | ||
| @Measurement(iterations = 5, time = 3) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @Fork(1) | ||
| public class BenchmarkDispatchChunkSize { | ||
|
andreaTP marked this conversation as resolved.
|
||
|
|
||
| private static final int NUM_FUNCTIONS = 2000; | ||
|
|
||
| @Param({"0", "999", "1999"}) | ||
| private int targetFunc; | ||
|
|
||
| private ExportFunction exportFunc; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| StringBuilder wat = new StringBuilder(); | ||
| wat.append("(module\n"); | ||
| for (int i = 0; i < NUM_FUNCTIONS; i++) { | ||
| wat.append(" (func $f").append(i); | ||
| wat.append(" (export \"f").append(i).append("\")"); | ||
| wat.append(" (param i32) (result i32)\n"); | ||
| wat.append(" local.get 0\n"); | ||
| wat.append(" i32.const ").append(i + 1).append("\n"); | ||
| wat.append(" i32.add)\n"); | ||
| } | ||
| wat.append(")\n"); | ||
|
|
||
| byte[] wasm = Wat2Wasm.parse(wat.toString()); | ||
| Instance instance = | ||
| Instance.builder(Parser.parse(wasm)) | ||
| .withMachineFactory(MachineFactoryCompiler::compile) | ||
| .build(); | ||
| exportFunc = instance.export("f" + targetFunc); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void dispatch(Blackhole bh) { | ||
| bh.consume(exportFunc.apply(42)); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.