deep

a Cross Development Platform for Java

User Tools

Site Tools


dev:hstring

HString

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:

  • A HString must be registered before it can be used. A table contains all the registered HStrings.
  • A registered HString exists exactly once. If two HStrings like “Hello World” are created and registered both references point to the same HString-object!
  • Two registered HStrings can be compared by comparing there references.
  • Be careful: two HStrings can only be compared if they were registered before.
  • The central HString-table is created once. If a project is recompiled already registered HStrings remain in the table.

Implementation

Class diagramm for HStrings

Most strings contain characters which fit into 8 bit. If not 16 bits have to be used. This consumes more memory.

Correct Use

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
dev/hstring.txt · Last modified: 2022/12/20 11:26 by ursgraf