flattening a String?

Uberto Barbini uberto.gama at gmail.com
Wed Jul 31 22:04:09 UTC 2019


Hi,
I've converted my examples from LW1 to LW2 and I've played a bit.
Thanks for all the good work!

One thing that bothered me is that all examples were just using
numbers, so I've done some experiments to save a String of a fixed
length (a reasonable compromise for high performance) in an inline
type.
This would allow to use them in more use cases including FIX records etc.

So far the best I managed is just having a bunch of char fields and
mapping them to and from a String.
The code is not nice but it works and it should be fast (I've not
measured anything yet).

Of course, there are many limitations (Unicode and strings longer than
16 chars :)) but the main issue now is that looking at the memory
footprint, it seems that it takes 4 bytes for each char, is that
correct?

I can imagine using int instead to store 4 chars in a more compact way
but I wonder if it is possible to do better than this, either now or
in the future (flattened arrays of fixed size)?


Thank you very much

Uberto




inline public class User {

    public final int yearOfBirth;

    private final SixteenCharString name;

    public User(String name, int yearOfBirth) {
        this.yearOfBirth = yearOfBirth;
        this.name = new SixteenCharString(name);
    }

    public String getName(){
        return name.getValue();
    }

}





inline public class SixteenCharString {
    private final char c0;
    private final char c1;
    private final char c2;
    private final char c3;
    private final char c4;
    private final char c5;
    private final char c6;
    private final char c7;
    private final char c8;
    private final char c9;
    private final char cA;
    private final char cB;
    private final char cC;
    private final char cD;
    private final char cE;
    private final char cF;

    public SixteenCharString(String value){
        char[] padded = (value + "                ").toCharArray();
        int index =0;
        c0 = padded[index++];
        c1 = padded[index++];
        c2 = padded[index++];
        c3 = padded[index++];
        c4 = padded[index++];
        c5 = padded[index++];
        c6 = padded[index++];
        c7 = padded[index++];
        c8 = padded[index++];
        c9 = padded[index++];
        cA = padded[index++];
        cB = padded[index++];
        cC = padded[index++];
        cD = padded[index++];
        cE = padded[index++];
        cF = padded[index++];
    }

    public String getValue(){
        char[] padded = new char[16];
        int index =0;
        padded[index++] = c0;
        padded[index++] = c1;
        padded[index++] = c2;
        padded[index++] = c3;
        padded[index++] = c4;
        padded[index++] = c5;
        padded[index++] = c6;
        padded[index++] = c7;
        padded[index++] = c8;
        padded[index++] = c9;
        padded[index++] = cA;
        padded[index++] = cB;
        padded[index++] = cC;
        padded[index++] = cD;
        padded[index++] = cE;
        padded[index++] = cF;
        return new String(padded).trim();
    }
}


More information about the valhalla-dev mailing list