-
Notifications
You must be signed in to change notification settings - Fork 21
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
Missing generated AnyRef specialization for array access #12764
Comments
I'm not sure where that would go...? In the Scaladoc for |
Nobody is working on Scala 2 specialization; there are many open bugs. Currently |
Scaladoc is unclear. https://github.com/scala/scala/blob/v2.13.13/src/library/scala/specialized.scala#L31 Oh, there is a test that suggests a workaround:
where
it could say "beware generalist use of specialized!" |
Reproduction steps
Consider the following jmh benchmark (on scala 2.13.8 and hotspot openjdk-19, but I believe this applies to all versions except that graal is possibly better than C2 at loop hoisting the checks):
This gives with LinuxPerfNorm profiler enabled
(the confidence intervals reported by jmh are overly wide in many cases, because it assumes the quite conservative student t distribution. In other words, one needs to very carefully design test parameters with high iteration and fork counts in order to get good confidence intervals, and I did not do that here)
Problem
The problem is that specialization conflates
AnyRef
withAny
. In many cases, there is not a relevant distinction in terms of performance whether we statically know that a typeT
isT <: AnyRef
.For accessing an
Array[T]
this makes a big difference, though: Accessing the array isaload
ifT <: AnyRef
, and is a much slowerscala.runtime.ScalaRunTime.array_apply(xs: AnyRef, idx: Int): Any
.Ideally, specialization would stop conflating Any and AnyRef.
If that is difficult for technical reasons, then we need to very prominently document this and warn people away from naive use of
@specialized
. Instead we need to point people to using custom specialization, as the standard library already does in some places likescala.collection.immutable.ArraySeq$ofRef
.Furthermore, we need to take our own bitter medicine and manually specialize performance critical parts. It is hard to imagine more critical code than iteration over arrays, so we definitely need a
class scala.collection.ArrayOps$ArrayIteratorOfRef
. But we need to look at all uses of@specialized
and consider manually specializing the AnyRef case.The text was updated successfully, but these errors were encountered: