Re: comparing strings with ==

Date view Thread view Subject view Author view

From: Archie Cobbs (archie@whistle.com)
Date: Thu Sep 03 1998 - 16:56:04 EDT


Tim Wilkinson writes:
> Strings can be 'intern'ed in which case you can then come them using ==, but
> this doesn't happen automatically (I'm not sure how it could without help from
> the compiler - how to you magic new String("hello") into it's intern, and does
> the compiler automatically intern quoted strings in source.

You're right... you couldn't really do this because it breaks in the
case of new String("hello").. eg.

    0 new #1 <Class java.lang.String>
    3 dup
    4 ldc #2 <String "hello">
    6 invokespecial #3 <Method java.lang.String(java.lang.String)>

By the time the constructor is called, you've already got a different
object and it's too late.

Hmm, it still may be a good optimization to uniquify the string data,
to conserve memory. For some specialized applications this might be
a noticable win (eg, parsing a program that has the same keywords
repeated over and over).

Something like this:

  public final class String {
    private char[] bytes;

    // enter character data into internal hashtable (if not already there)
    private static native char[] enterData(char[] data);

    public String(String copy) {
      bytes = copy.bytes;
    }

    public String(char[] data) {
      bytes = enterData(data);
    }

    ...
  }

The enterData() method would look for a matching char[] instance in
an internal hashtable, returning it if it already exists, otherwise
entering a new entry with the contents of "data".

To avoid a memory leak, you'd need to allocate new string data with
a new GC type, eg. GC_ALLOC_STRINGDATA. The internal hashtable
would *not* be scanned by the GC; rather, when the GC was GC'ing
a region of type GC_ALLOC_STRINGDATA, it would know by the region's
type to also remove the entry from the hashtable.

So, this would be a tradeoff of adding some native code for a hopeful
performance increase. It might be worthwhile to prototype and see if
any increase is noticable (maybe this weekend I'll play around with it).

-Archie

___________________________________________________________________________
Archie Cobbs * Whistle Communications, Inc. * http://www.whistle.com


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Sat Sep 23 2000 - 19:56:54 EDT