flattening a String?
Uberto Barbini
uberto.gama at gmail.com
Thu Aug 1 08:14:11 UTC 2019
A correction on my post:
Using bytes instead of char (which of course are 16bits) the memory
allocation is exactly as expected.
I've also realized that Valhalla does some kind of data alignment when
creating an array of inline types so if an object is more than 16bytes
it will allocate 32 bytes and if more than 32 bytes it allocates 64
bytes for object.
The main question remains, is there a to represent a flattenable array
of a fixed size?
thank you very much
Uberto
On Wed, 31 Jul 2019 at 23:04, Uberto Barbini <uberto.gama at gmail.com> wrote:
>
> 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