-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add benchmarks for specialization #9752
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0fba923
Fix source directory
liufengyun e788487
Move input files to correct directory
liufengyun 6827b9f
Add specialization benchmarks
liufengyun 6fbdfcd
Update bench-run/src/main/scala/dotty/tools/benchmarks/specialization…
liufengyun 2ce8da3
Update bench-run/src/main/scala/dotty/tools/benchmarks/specialization…
liufengyun 8818fd4
Make sure computation goes to blackhole to prevent optimization
liufengyun cdc14b0
Use explicit state and volatile to avoid optimization
liufengyun 62339a0
Outsmart JVM
liufengyun 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 was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
bench-run/src/main/scala/dotty/tools/benchmarks/specialization/Functions.scala
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,71 @@ | ||
package dotty.tools.benchmarks.specialization | ||
|
||
import org.openjdk.jmh.annotations._ | ||
import scala.util.Random | ||
|
||
|
||
@State(Scope.Benchmark) | ||
class Functions { | ||
extension (x: Int) | ||
inline def times(inline work: Int): Int = { | ||
var res = 0 | ||
var count = 0 | ||
while count < x do | ||
res += work + 1 | ||
count += 1 | ||
res | ||
} | ||
|
||
class ByName { | ||
def foo(x: => Int): Int = x | ||
} | ||
|
||
// outsmart JVM with storage in mutable array | ||
var byName = new ByName | ||
var arrByName = Array(byName, null) | ||
|
||
@Benchmark | ||
def byNameBench(): Int = 10000.times { | ||
// necessary to outsmart JVM | ||
// remove it will result in 200x speed up | ||
arrByName(1) = null | ||
arrByName(0).foo(6) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It turns out we also need this turnaround to defeat JVM optimization. |
||
|
||
|
||
var fn = (x: Int) => x + 1 | ||
var arr = Array(fn, null) | ||
@Benchmark | ||
def lambdaBench(): Int = 10000.times { | ||
arr(1) = null | ||
arr(0)(2) | ||
} | ||
|
||
class Func1[T](fn: T => Int) extends Function1[T, Int] { | ||
def apply(x: T): Int = fn(x) | ||
} | ||
class Fn extends Func1(identity[Int]) | ||
|
||
var fn1: Function1[Int, Int] = new Fn | ||
var arr1 = Array(fn1, null) | ||
|
||
@Benchmark | ||
def extendFun1Bench(): Int = 10000.times { | ||
arr1(1) = null | ||
arr1(0)(12) | ||
} | ||
|
||
|
||
class Func2 extends Function2[Int, Int, Int] { | ||
def apply(i: Int, j: Int) = i + j | ||
} | ||
|
||
var fn2: Function2[Int, Int, Int] = new Func2 | ||
var arr2 = Array(fn2, null) | ||
|
||
@Benchmark | ||
def extendFun2Bench(): Int = 10000.times { | ||
arr2(1) = null | ||
arr2(0)(1300, 37) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 haven't checked what is being done here exactly but note that it's not recommended to use loops when benchmarking on the JVM: http://tutorials.jenkov.com/java-performance/jmh.html#writing-good-benchmarks, instead let JMH run things as many times as needed to get good statistics
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.
Thanks for the reminder, @smarter . It's really subtle to defeat optimization in microbenchmarks.