flattening a String?
Remi Forax
forax at univ-mlv.fr
Mon Aug 5 18:26:17 UTC 2019
----- Mail original -----
> De: "Uberto Barbini" <uberto.gama at gmail.com>
> À: "valhalla-dev" <valhalla-dev at openjdk.java.net>
> Envoyé: Jeudi 1 Août 2019 10:14:11
> Objet: Re: flattening a String?
Hi Uberto,
> 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.
yes,
the alignment is not valhalla specific, all fields in classical objects are also aligned,
it's due to the fact that most of the CPU requires ints to be aligned to 32bits, double to 64bits when reading/writing them in RAM.
For inline types, the current implementation may allocate an inline class in RAM in the interpreter until the code is JITed and array of inline class always lies in RAM (see below),
so inline class fields layout as the same restriction as classical class field layout.
>
> The main question remains, is there a to represent a flattenable array
> of a fixed size?
no,
array are always reference.
There is a proposal to introduce more exotic kind of arrays known as Array 2.0, but we are now focused on delivering inline types first.
>
>
> thank you very much
>
> Uberto
regards,
Rémi
>
> 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