AsArgument adds an allocation
wsargent opened this issue · 1 comments
wsargent commented
Statement interpolation is faster than using a statement apply:
class StatementBenchmark {
@Benchmark
def statementFromInterpolation(blackhole: Blackhole): Unit = {
val arg1 = "one"
val arg2 = "two"
val arg3 = "three"
blackhole.consume(st"Hello world $arg1, $arg2, $arg3")
}
@Benchmark
def statementFromApply(blackhole: Blackhole): Unit = {
val arg1 = "one"
val arg2 = "two"
val arg3 = "three"
blackhole.consume(
Statement(message = "Hello world {}, {}, {}", arguments = Arguments(arg1, arg2, arg3))
)
}
@Benchmark
def statementFromArray(blackhole: Blackhole): Unit = {
val arg1 = "one"
val arg2 = "two"
val arg3 = "three"
blackhole.consume(
Statement(
message = "Hello world {}, {}, {}",
arguments = Arguments.fromArray(Array(Argument(arg1), Argument(arg2), Argument(arg3)))
)
)
}
}
yields
[info] Benchmark Mode Cnt Score Error Units
[info] StatementBenchmark.statementFromApply avgt 5 73.042 ± 2.727 ns/op
[info] StatementBenchmark.statementFromArray avgt 5 28.677 ± 0.653 ns/op
[info] StatementBenchmark.statementFromInterpolation avgt 5 29.063 ± 4.497 ns/op
The apply method is probably slower because it uses AsArgument
, which the interpolation and array does not use -- it's macro based instead.
So it may be possible to save on an allocation by using a macro for Arguments.apply
here.