Tune String's hashCode() + equals()

Marek Kozieł develop4lasu at gmail.com
Thu Mar 4 22:38:52 UTC 2010


2010/3/4 Ulf Zibis <Ulf.Zibis at gmx.de>:

>> 5.
>> Intern string do not need hash codes co comparing cos they have same
>> address, so first loop would return true if they are equal, after this
>> we need only to check if they are not equal:
>>
>>>
>>> if (isIntern()&&  anotherString.isIntern()) return false;
>>>
>
> You are right, but
>    if (h1 != 0 && h2 != 0 && h1 != h2) return false;
> would perform same (if already computed internal hash would be
> back-propagated to the Java object).
>

Could you explain what do you mean ?


If u search for optimization i suggest (if it's not already partially
implemented):
add :
public static String String.intern(String str, int waste)
which would work like String.intern(String str) except that if in
intern table there is already 'other' String that:
str.startWith(other) && other.length()<str.length()+waste then new
intern String would use same char array as other String.

If i'm not wrong this should free around 40% memory consumed by Strings

also could help a lot
String String.intern(String str, String asPartOfString)

I prefer this solution because 10Mb of memory may mean more data
stored in Memory instead of database which can bring 10000% more
benefits than toying with if.

-- 
Pozdrowionka. / Regards.
Lasu aka Marek Kozieł

http://lasu2string.blogspot.com/



More information about the core-libs-dev mailing list