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
A simplified opaque type (Wrap) that uses a typeclass to implement operator + logic.
traitAddable[V]:defplus(x: V, y: V):VobjectAddable:givenAddable[Int] withinlinedefplus(x: Int, y: Int):Int= x + y
importwrap.Wrap
extension[V](lhs: Wrap[V])
inlinedef+(rhs: Wrap[V])(usingva: Addable[V]):Wrap[V] =Wrap(va.plus(lhs.value, rhs.value))
objectwrap:opaquetypeWrap[V] =VobjectWrap:defapply[V](v: V):Wrap[V] = v
extension[V](w: Wrap[V])
defvalue:V= w
objecttestwrap:importwrap.*valp=Wrap(3) +Wrap(5)
Output
If I compile this code, and then look at the byte-code for object testwrap, I see the following. It is clear from the byte-code that Wrap is being stored as a raw Int (nice), and the inlined type-class compiles down to raw iadd (also nice). However, the code also invokes quite a lot of boxing and unboxing, and I am trying to work out the trade-off in compute cost from the (un)boxing versus efficiency of storing the raw Int.
I'm not sure what a realistic expectation is, but I was hoping the compiler could elide the boxing/unboxing, since the only semantically necessary operations are istore, iload and iadd on the underlying raw Int values.
I am also unsure how much the various calls to the boxing methods cost. They appear to be significant relative to the core integer operations, however I do not know exactly how much.
The text was updated successfully, but these errors were encountered:
To avoid boxing here you also need specialization don't you?
I think you won't see boxing if opaque type Wrap = Int. Have you tried that? Otherwise, I think these Wrap[V] are ultimately represented at java.lang.Object in bytecode.
Compiler version
3.0.0-RC2
Minimized example
A simplified opaque type (
Wrap
) that uses a typeclass to implement operator+
logic.Output
If I compile this code, and then look at the byte-code for
object testwrap
, I see the following. It is clear from the byte-code thatWrap
is being stored as a rawInt
(nice), and the inlined type-class compiles down to rawiadd
(also nice). However, the code also invokes quite a lot of boxing and unboxing, and I am trying to work out the trade-off in compute cost from the (un)boxing versus efficiency of storing the raw Int.Expectation
I'm not sure what a realistic expectation is, but I was hoping the compiler could elide the boxing/unboxing, since the only semantically necessary operations are
istore
,iload
andiadd
on the underlying rawInt
values.I am also unsure how much the various calls to the boxing methods cost. They appear to be significant relative to the core integer operations, however I do not know exactly how much.
The text was updated successfully, but these errors were encountered: