GWT:如何避免在生成的 JavaScript 代码中调用 dynamicCast 和 canCastUnsafe? [英] GWT: How to avoiding calls to dynamicCast and canCastUnsafe in generated JavaScript code?

查看:22
本文介绍了GWT:如何避免在生成的 JavaScript 代码中调用 dynamicCast 和 canCastUnsafe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写一些特殊用途的数据结构,打算在浏览器中使用,(使用 GWT 编译为 JavaScript).

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

我正在尝试匹配一些内置 JDK 类的性能我注意到运行速度相当快,但是当我将我的代码跟踪与一些模拟的 JDK 代码进行比较时,我的有很多调用dynamicCast 和 canCastUnsafe,而 JDK 模拟类没有.而且它也几乎说明了性能差异......

I'm trying to match the performance of some of the built-in JDK classes I'm noticing things run reasonably fast, but when I compare my code trace to some of the emulated JDK code, mine has lots of calls to dynamicCast and canCastUnsafe, while the JDK emulated classes do not. And it just about accounts for the difference in performance too...

任何 GWT 专家都知道如何避免这种情况?这相当于 20% 的开销:-(

Any GWT gurus out there know how to avoid this? It's amounting to a 20% overhead :-(

详情:

这是将 0 到 100,000 之间的随机整数 10,000 次插入两种不同数据结构的配置文件输出(在 Firebug 中捕获):

Here's the profile output (captured in Firebug) for 10,000 insertions of random integers, between 0 and 100,000 into two different data structures:

Google 对 java.util.TreeMap(红黑树)的 TreeMap 实现:

Google's TreeMap implementation for java.util.TreeMap (a red-black tree):

Profile (4058.602ms, 687545 calls)
Function              Calls      Percent   Own Time
$insert_1             129809     41.87%    1699.367ms
$compare_0            120290     16%        649.209ms
$isRed                231166     13.33%     540.838ms
compareTo_0           120290      8.96%     363.531ms
$put_2                 10000      6.02%     244.493ms
wrapArray              10000      3.46%     140.478ms
createFromSeed         10000      2.91%     118.038ms
$TreeMap$Node          10000      2.38%      96.706ms   
initDim                10000      1.92%      77.735ms   
initValues             10000      1.49%      60.319ms   
$rotateSingle           5990      0.73%      29.55ms  
TreeMap$Node           10000      0.47%      18.92ms 

我的代码(一个 AVL 树):

My Code (An AVL tree):

Profile (5397.686ms, 898603 calls)
Function              Calls      Percent   Own Time
$insert               120899     25.06%    1352.827ms
$compare              120899     17.94%      968.17ms
dynamicCast           120899     14.12%     762.307ms <--------
$balanceTree          120418     13.64%     736.096ms
$setHeight            126764      8.93%     482.018ms
compareTo_0           120899      7.76%     418.716ms
canCastUnsafe         120899      6.99%     377.518ms <--------
$put                   10000      2.59%     139.936ms
$AVLTreeMap$Node        9519      1.04%      56.403ms   
$moveLeft               2367      0.36%      19.602ms   
AVLTreeMap$State        9999      0.36%      19.429ms   
$moveRight              2378      0.34%      18.295ms   
AVLTreeMap$Node         9519      0.34%      18.252ms   
$swingRight             1605      0.26%      14.261ms   
$swingLeft              1539      0.26%      13.856ms

其他观察:

  • 我制作的另一个数据结构(SkipList)也有同样的问题.
  • dynamicCast 正在比较函数中应用:

  • Same problem for another data structure I made (SkipList).
  • dynamicCast is being applied in the compare function:

cmp = dynamicCast(right.key, 4).compareTo$(key);

cmp = dynamicCast(right.key, 4).compareTo$(key);

如果类没有实现 Map(即:只是从类中删除实现 Map".不管它是通过接口访问还是直接访问都没有关系.这会导致同一行编译为:

dynamicCast goes away if the class does not implement Map (ie: just removing " implements Map" from the class. Doesn't matter if it's accessed through the interface or directly. This results in the same line compiling to:

cmp = right.key.compareTo$(key);

cmp = right.key.compareTo$(key);

这是来自 SkipList 的 Java 源代码的相关部分:

This is the relevant section of Java source from SkipList:

private int compare(Node a, Object o) {
    if (comparator != null)
        return comparator.compare((K) a.key, (K) o);

    return ((Comparable<K>) a.key).compareTo((K) o);
}

public V get(Object k) {
    K key = (K) k;
    Node<K, V> current = head;

    for (int i = head.height - 1; i >= 0; i--) {
        Node<K, V> right;

        while ((right = current.right[i]) != null) {
            int cmp = compare(right, key);

            ...
        }
    }
}

推荐答案

不幸的是,我仍然不完全清楚原因,但根据我的经验,它似乎来自显式转换,例如:

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

((Comparable) obj).compareTo(other)

生成的 Javascript 如下所示:

The Javascript generated looks like:

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

其中 1 是一个生成的 typeId,表示转换的目标.dynamicCast 依次调用 canCastUnsafe,如果为 false,则抛出 ClassCastException.其价值已经辩论,因为这已经在托管模式下被捕获.

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.

它可以用 JSNI 回避:

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); 
}-*/;

这篇关于GWT:如何避免在生成的 JavaScript 代码中调用 dynamicCast 和 canCastUnsafe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆