deep was developed with efficiency and speed in mind. String handling in Java is not very efficient. However, strings are widely used in a compiler. Therefore, we don't use regular Java strings (java.lang.String
) but implement an more efficient version (org.deepjava.strings.HString
). This version should be used throughout the compiler whenever speed is important. String and HString differ in many aspects:
Most strings contain characters which fit into 8 bit. If not 16 bits have to be used. This consumes more memory.
HStrings must be registered before they can be used. This is done with
HString.getRegisteredHString("...")
If a second HString with the same characters is created and registered both references point to the same HString. The comparison of two HStrings is way more efficient than with String.equals()
.
An example:
HString firstString = HString.getRegisteredHString("HelloWorld"); HString secondString = HString.getRegisteredHString("test"); if(firstString == secondString) { // ... }
It's possible to compare to non-registered HStrings as well as comparing a registered with a non-registered HString. For this the class HString contains the method equals.
HString firstString = HString.getRegisteredHString("one"); HString secondString = HString.getHString("one"); firstString == secondString // evalutes to false firstString.equals(secondString) // evalutes to true