I may have encountered a limitation, not sure.
In Scala Function1 at least is specialized.
So given this sample:
class Sample[@miniboxed +T](val value: T) {
def map[U](f: T => U) =
new Sample[U](f(value))
}
The bytecode generated for that map function uses Function.apply(Object) instead of Function1.apply$mcJJ$sp(Long), which would be the specialized apply version for Long on Function1.
Is there something that can be done about it?
I don’t care much about interoperability with other @specialized classes, but using specialized functions is important for my use-case.
Here’s the full listing of that method as extracted with javap on the generated Sample_J:
public <U extends java/lang/Object> Sample<U> map(scala.Function1<Tsp, U>);
Code:
0: new #16 // class Sample_L
3: dup
4: aload_1
5: aload_0
6: aload_0
7: getfield #18 // Field "CancelSample_J|T_TypeTag":B
10: invokevirtual #22 // Method value_J:(B)J
13: aload_0
14: getfield #18 // Field "CancelSample_J|T_TypeTag":B
17: invokestatic #28 // Method miniboxing/runtime/MiniboxConversions.minibox2box:(JB)Ljava/lang/Object;
20: invokeinterface #34, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
25: invokespecial #38 // Method Sample_L."<init>":(Ljava/lang/Object;)V
28: areturn
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-miniboxing/CAN9KCosLBcOwWdpZgTE8RL4pwAaHvABBvOod9atjGs7vwmVxmw%40mail.gmail.com.--
You received this message because you are subscribed to the Google Groups "Scala Miniboxing Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-miniboxi...@googlegroups.com.
To post to this group, send email to scala-mi...@googlegroups.com.
Visit this group at http://groups.google.com/group/scala-miniboxing.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-miniboxing/CAN9KCouxw4wox8OutNW35Ocr3sBWwkoDRnuJLLFXYsBtJFvrFA%40mail.gmail.com.
Alexandru, I sketched a proposed solution here: https://github.com/miniboxing/miniboxing-plugin/issues/108
I'm not sure how it would affect performance -- it may be the case that the overhead of the "specialization bridge method" outweighs the benefits of avoiding boxing -- we need to check this using benchmarks. I will be implementing the two proposed solutions over the next days, so we can let benchmarks decide what is the best solution.
Thanks Vlad,I'm also flirting with the idea of having my own Function interface and do implicit conversions to it by means of macros. This would also work OK for purposes of Java 8 interoperability, since Scala's function types are not `@FunctionalInterface` precisely because of specialization. Haven't tried it out yet, but I don't see a reason for why this shouldn't work.
Of course, it would be nice if miniboxing would just work with FunctionX, so it all depends on the results of those benchmarks you're planning.
BTW, speaking of benchmarking and boxing, I'm sure you know really well what you're doing - just want to point out that for me the biggest benefit of avoiding boxing is to relieve pressure from the GC, as to avoid stop-the-world sweeps as much as possible - which becomes incredibly important for my use-case - optimizing my library to be able to move millions of messages per second over asynchronous boundaries.
Boxing is one reason for why the GC can end up acting like a GIL that chokes the process and you end up acquainted with Amdahl's law :-)