GWT: How to avoiding calls to dynamicCast and canCastUnsafe in generated JavaScript code?

后端 未结 5 806
醉梦人生
醉梦人生 2021-01-12 12:32

I\'m writing some special purpose data structures in Java, intended for use in the browser, (compiled to JavaScript with GWT).

I\'m trying to match the performance o

5条回答
  •  一整个雨季
    2021-01-12 13:01

    Unfortunately I'm still not exactly clear on the cause, but from my experience, it seems from explicit casts, like:

    ((Comparable) obj).compareTo(other)
    

    The Javascript generated looks like:

    dynamicCast(obj, 1).compareTo(other);
    

    Where 1 is a generated typeId representing the target of the cast. dynamicCast in turn calls canCastUnsafe and if false, it throws a ClassCastException. The value of this has been debated, since this would already be caught in hosted mode.

    It can be sidestepped with JSNI:

    public static native int compare(Object a, Object b) /*-{
        return a.@java.lang.Comparable::compareTo(Ljava/lang/Object;)(b); 
    }-*/;
    

提交回复
热议问题