-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Using custom data structure for callstack tracking #2630
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
62 changes: 62 additions & 0 deletions
62
src/main/java/graphql/execution/instrumentation/dataloader/IntMap.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,62 @@ | ||
| package graphql.execution.instrumentation.dataloader; | ||
|
|
||
| import graphql.Internal; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| @Internal | ||
| public class IntMap { | ||
dfa1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // A reasonable default that guarantees no additional allocations for most use cases. | ||
| private static final int DEFAULT_INITIAL_SIZE = 16; | ||
|
|
||
| // this array is mutable in both size and contents. | ||
| private int[] countsByLevel; | ||
|
|
||
| public IntMap(int initialSize) { | ||
| if (initialSize < 0) { | ||
| throw new IllegalArgumentException("negative size " + initialSize); | ||
| } | ||
| countsByLevel = new int[initialSize]; | ||
| } | ||
|
|
||
| public IntMap() { | ||
| this(DEFAULT_INITIAL_SIZE); | ||
| } | ||
|
|
||
| public int get(int level) { | ||
| if (level < 0) { | ||
| throw new IllegalArgumentException("negative level " + level); | ||
| } | ||
| if (level + 1 > countsByLevel.length) { | ||
| throw new IllegalArgumentException("unknown level " + level); | ||
| } | ||
| return countsByLevel[level]; | ||
| } | ||
|
|
||
| public void increment(int level, int by) { | ||
| if (level < 0) { | ||
| throw new IllegalArgumentException("negative level " + level); | ||
| } | ||
| if (level + 1 > countsByLevel.length) { | ||
| int newSize = level == 0 ? 1 : level * 2; | ||
| countsByLevel = Arrays.copyOf(countsByLevel, newSize); | ||
| } | ||
| countsByLevel[level] += by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder result = new StringBuilder(); | ||
| result.append("IntMap["); | ||
| for (int i = 0; i < countsByLevel.length; i++) { | ||
| result.append("level=").append(i).append(",count=").append(countsByLevel[i]).append(" "); | ||
| } | ||
| result.append("]"); | ||
| return result.toString(); | ||
| } | ||
|
|
||
| public void reset() { | ||
| Arrays.fill(countsByLevel, 0); | ||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
src/test/groovy/graphql/execution/instrumentation/dataloader/IntMapTest.groovy
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 graphql.execution.instrumentation.dataloader | ||
|
|
||
| import spock.lang.Specification | ||
|
|
||
| class IntMapTest extends Specification { | ||
|
|
||
| def "increase adds levels"() { | ||
| given: | ||
| IntMap sut = new IntMap(0) // starts empty | ||
|
|
||
| when: | ||
| sut.increment(2, 42) // level 2 has count 42 | ||
|
|
||
| then: | ||
| sut.get(0) == 0 | ||
| sut.get(1) == 0 | ||
| sut.get(2) == 42 | ||
| } | ||
|
|
||
| def "increase count by 10 for every level"() { | ||
| given: | ||
| IntMap sut = new IntMap(0) | ||
|
|
||
| when: | ||
| 5.times {Integer level -> | ||
| sut.increment(level, 10) | ||
| } | ||
|
|
||
| then: | ||
| 5.times { Integer level -> | ||
| sut.get(level) == 10 | ||
| } | ||
| } | ||
|
|
||
| def "increase yields new count"() { | ||
| given: | ||
| IntMap sut = new IntMap(0) | ||
|
|
||
| when: | ||
| sut.increment(1, 0) | ||
|
|
||
| then: | ||
| sut.get(1) == 0 | ||
|
|
||
| when: | ||
| sut.increment(1, 1) | ||
|
|
||
| then: | ||
| sut.get(1) == 1 | ||
|
|
||
| when: | ||
| sut.increment(1, 100) | ||
|
|
||
| then: | ||
| sut.get(1) == 101 | ||
| } | ||
|
|
||
| def "toString() is important for debugging"() { | ||
| given: | ||
| IntMap sut = new IntMap(0) | ||
|
|
||
| when: | ||
| sut.toString() | ||
|
|
||
| then: | ||
| sut.toString() == "IntMap[]" | ||
|
|
||
| when: | ||
| sut.increment(0, 42) | ||
|
|
||
| then: | ||
| sut.toString() == "IntMap[level=0,count=42 ]" | ||
|
|
||
| when: | ||
| sut.increment(1, 1) | ||
|
|
||
| then: | ||
| sut.toString() == "IntMap[level=0,count=42 level=1,count=1 ]" | ||
| } | ||
| } |
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,45 @@ | ||
| package benchmark; | ||
|
|
||
| import graphql.execution.instrumentation.dataloader.IntMap; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @Warmup(iterations = 2) | ||
| @Measurement(iterations = 2, timeUnit = TimeUnit.NANOSECONDS) | ||
| public class IntMapBenchmark { | ||
|
|
||
| @Benchmark | ||
| public void benchmarkLinkedHashMap(Blackhole blackhole) { | ||
| Map<Integer, Integer> result = new LinkedHashMap<>(); | ||
| for (int i = 0; i < 30; i++) { | ||
| int level = i % 10; | ||
| int count = i * 2; | ||
| result.put(level, result.getOrDefault(level, 0) + count); | ||
| blackhole.consume(result.get(level)); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIntMap(Blackhole blackhole) { | ||
| IntMap result = new IntMap(16); | ||
| for (int i = 0; i < 30; i++) { | ||
| int level = i % 10; | ||
| int count = i * 2; | ||
| result.increment(level, count); | ||
| blackhole.consume(result.get(level)); | ||
| } | ||
| } | ||
| } | ||
|
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should make another method that is a "set". The above code is "set level 1 to the value 1" whereas your code is "increment" the value by 1 (but its zero right now)
So I think a
expectedStrategyCallsPerLevel.set(1)is a better method