Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 183877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:11:36+00:00 2026-05-11T15:11:36+00:00

I’m writing some special purpose data structures in Java, intended for use in the

  • 0

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 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…

Any GWT gurus out there know how to avoid this? It’s amounting to a 20% overhead 🙁

Details:

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’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  

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 

Additional observations:

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

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

  • 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);

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);              ...         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:11:37+00:00Added an answer on May 11, 2026 at 3:11 pm

    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);  }-*/; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out IsWow64Process. May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer UPDATE t_transaction tu JOIN ( SELECT code, MAX(flag) AS flag… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer Assuming you want to use display lists, gluPartialDisk() is probably… May 11, 2026 at 11:52 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.