From: Leo U. <leo...@ya...> - 2007-01-02 19:46:47
|
Hmm, we may have a performance win by swapping out the System.identityHashCode method call in PyObject.object_hashcode with just a call to super.hashCode(). >From running this test code: public class TestHashCode{ public static void main(String ... args){ Object o = new Object(); System.out.println(o.hashCode()); System.out.println(System.identityHashCode(o)); int x = 10000000; long time1 = System.currentTimeMillis(); for(int i = 0; i < x; i ++) o.hashCode(); long time2 = System.currentTimeMillis(); System.out.println(time2 - time1); time1 = System.currentTimeMillis(); for(int i = 0; i < x; i++) System.identityHashCode(o); time2 = System.currentTimeMillis(); System.out.println(time2 - time1); test1 t1 = new test1(); System.out.println(t1.hashCode2()); System.out.println(System.identityHashCode(t1)); test2 t2 = new test2(); System.out.println(t2.hashCode2()); System.out.println(System.identityHashCode(t2)); } static class test1{ int hashCode2(){ return super.hashCode(); } public int hashCode(){ return 1; } } static class test2 extends test1{ public int hashCode(){ return 2; } } } it seems that invoking super.hashCode is quite a bit quicker than the call to System. Given that PyObject subclasses Object, I believe we are free to use this. It may make sense to get rid of the overriden hashCode method in PyObject as well, since doing so will have the same effect. object_hashCode is final, and its sole purpose appears to be to return the "identity" hashCode. Given that hashCode already does return this value, I don't see any reason to overide it to return what it will be returning anyway. I guess the negative of doing this is that the contract for Object does not guarantee that it will return the identityHashCode, though this is what HotSpot does. It may have the same effect though, since the contract does state that as much as possible it will return a unique integer. leouser __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |