You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe it could be an improvement, especially for scala-3, to provide inline definitions of algebra typeclass methods.
To try and illustrate, consider this example. Here I will define a couple of variations on an add function, and also a definition of additive semigroup whose plus method is inline:
objectdefs:importalgebra.ring.*// an in-line function using semigroup typeclassinlinedefadd[V](x: V, y: V)(usingalg: AdditiveSemigroup[V]):V=
alg.plus(x, y)
// a non-inline version of same functiondefadd2[V](x: V, y: V)(usingalg: AdditiveSemigroup[V]):V=
alg.plus(x, y)
// a typeclass with inline 'plus' methodgivenAdditiveSemigroup[Double] withinlinedefplus(x: Double, y: Double):Double=
x + y
valx=1.0
To compare them, here are two equivalent blocks of code, but one uses the standard Spire algebras and the other uses the inline typeclass above:
In conclusion, I believe that defining algebra typeclasses with inline methods will never be worse than the current definitions, but in cases where the calling code is inlined, may be substantially better.
Something like this might need to be part of the .../scala-3/spire/... specialized code. It is possible that it's mostly an advantage for the native types Int, Double, etc, less so for the non-atomic types like BigInt, Rational, etc, in which case only a few would need specialized inline method definitions
I believe it could be an improvement, especially for scala-3, to provide inline definitions of algebra typeclass methods.
To try and illustrate, consider this example. Here I will define a couple of variations on an
add
function, and also a definition of additive semigroup whoseplus
method isinline
:To compare them, here are two equivalent blocks of code, but one uses the standard Spire algebras and the other uses the inline typeclass above:
If you compare the resulting bytecode, I believe the typeclass with inline methods gives a more efficient result, using the inline
add
function:using standard spire algebra:
Using algebra with inline method:
If both arguments are literal constants, the inlined improvements are even more pronounced:
scala-3 compiles it down to a single constant value in the byte-code:
Lastly, if one uses the non-inline
add2
function, both versions of the typeclass yield the same bytecode, which looks like this:The text was updated successfully, but these errors were encountered: