HashMap Performance Issues

293 views
Skip to first unread message

shai.be...@gmail.com

unread,
Nov 28, 2006, 11:02:41 AM11/28/06
to Google Web Toolkit
It seems that the HashMap implementation is very slow. about 100 times
slower than native js object.

I re-implemented HashMap using native code (see below) it worked fine
with the compiler (not 100% tested). Yet, I get errors as i run it with
the emulator:

java.lang.RuntimeException: Error creating handle
at com.google.gwt.dev.shell.Handle.createHandle(Handle.java:53)
at
com.google.gwt.dev.shell.ie.HandleIE6.createHandle(HandleIE6.java:22)
at
com.google.gwt.dev.shell.ie.SwtOleGlue.translateDispatchArg(SwtOleGlue.java:401)
at
com.google.gwt.dev.shell.ie.SwtOleGlue.convertVariantToObject(SwtOleGlue.java:120)
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.invokeNativeObject(ModuleSpaceIE6.java:237)
at
com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:103)
at Artwaresoft.TGP.Ajax.AjaxHashMap.get(AjaxHashMap.java:23)
....

I could not figure out why.

Anyway, do you know about any available efficient HashMap class?


public class AjaxHashMap
{

public AjaxHashMap() {
construct();
}
private native void construct() /*-{
this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject = new Object();
}-*/;

public native void put(String key, Object value) /*-{
this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject[key] = value;
}-*/;
public native Object get(String key) /*-{
return this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject[key];
}-*/;

public native boolean containsKey(String key) /*-{
return this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject[key] !==
null;
}-*/;
// .contains[key]

public void clear() { construct(); }

public native void remove(String key) /*-{
return this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject[key] = null;
}-*/;

private JavaScriptObject jsObject;

public native int size() /*-{
return this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject.size();
}-*/;
public Iterator valuesIter() {
return new AjaxHashMapIter(this);
}
public native Object getAt(int m_index) /*-{
return this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject[m_index];
}-*/;
public native void remove(int m_index) /*-{
return this.@Artwaresoft.TGP.Ajax.AjaxHashMap::jsObject[m_index];
}-*/;

}

BTW, The implementation can be improved to look like
http://www.mojavelinux.com/articles/javascript_hashes.html


thanks,

Shai

Emily

unread,
Nov 28, 2006, 12:54:27 PM11/28/06
to Google Web Toolkit
Our benchmarks show that the slow computation of String's hashcode
can be a big bottle neck for HashMap, if you can, tries using something
other than strings as keys. There is work currently being done on
speeding up HashMap globally. If you must use Strings, you can also
try copying the FastStringMap class from com.googl.gwt.user.client.ui
package.

shai.be...@gmail.com

unread,
Nov 28, 2006, 2:55:25 PM11/28/06
to Google Web Toolkit
thanks a lot, Emily.

Now the complied code works faster. Yet, the emulation became very
slow.

Any idea how to make the emulator use the standard HashMap instead of
the FastStringMap? or just be faster...

Shai

Sandy McArthur

unread,
Nov 28, 2006, 3:46:26 PM11/28/06
to Google Web Toolkit

shai.be...@gmail.com wrote:
> Any idea how to make the emulator use the standard HashMap instead of
> the FastStringMap? or just be faster...

Maybe:
Map m = GWT.isScript() ? new FastStringMap() : new HashMap();

Reply all
Reply to author
Forward
0 new messages