JEP 370 - text example leads to exception

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Feb 17 15:43:22 UTC 2020


Hi Chris,
thanks for the links - took a look at your first test and, more 
specifically at example03SSNToCreditScore:

MemoryLayout ssnAndCreditStruct = MemoryLayout.ofStruct(
         MemoryLayout.ofSequence(9, 
MemoryLayout.ofValueBits(Character.SIZE, order)).withName("ssn"),
         MemoryLayout.ofValueBits(Integer.SIZE, 
order).withName("creditScore").withBitAlignment(16));

I guess this is what you referred to when you spoke about alignment. 
Here the root problem is that you have a sequence of 9 chars - hence 9 
bytes, which is not a multiple of 4 (the size of an int). So the 
"creditScore" field will start at offset 9 (in bytes) - meaning that the 
int will not be aligned. You have two options here:

1) You do what a C compiler would have done - e.g. you add a padding 
layout between the sequence layout and the credit score layout:

MemoryLayout ssnAndCreditStruct = MemoryLayout.ofStruct(
         MemoryLayout.ofSequence(9, 
MemoryLayout.ofValueBits(Character.SIZE, order)).withName("ssn"), 
*MemoryLayout.ofPaddingBits(16)*,
         MemoryLayout.ofValueBits(Integer.SIZE, 
order).withName("creditScore"));

2) relax the alignment constraints of the credit score field - e.g. from 
being 4-byte aligned to be 1-byte aligned (your example relaxes it to 
2-byte aligned, not 100% if that is correct?)

You do (1) in normal cases, where you want fast, aligned access - (2) 
should be done in cases where you want packed layouts - but it is 
possible that (2) might still not work (not all platform supports 
unaligned access primitives, and not in all possible access modes - e.g. 
atomic).

Maurizio

This should take care of the issue


On 17/02/2020 04:58, Chris T wrote:
> Maurizio, thanks for pointing the bug out - however I don't think I 
> was impacted by it.
>
> I agree with you that a more complex examples might distract the 
> audience from the main presentation points.
>
> As mentioned in one of my previous email, I finalized some examples 
> myself:
> 1. One example for memory layouts creates a structure where we 
> associate the SSN (social security number) to a credit score. The main 
> point here is to make a mix between char arrays and integers.
> Code is here: 
> https://github.com/knowledge-base-and-tutorials/java14-features/blob/master/src/main/java/com/github/kbnt/java14/fma/ForeignMemoryAccessExamples.java 
> <https://urldefense.com/v3/__https://github.com/knowledge-base-and-tutorials/java14-features/blob/master/src/main/java/com/github/kbnt/java14/fma/ForeignMemoryAccessExamples.java__;!!GqivPVa7Brio!Pf9Nt42jivBKFcxTzsgBVXVMil8JZN4IRFDQyoAO0Z-g4vnp-zkHGM8CgPO6aZD458bQ8Ps$> 
> (method called example03SSNToCreditScore)
> Presentation of the case is here: 
> https://www.youtube.com/watch?v=NwXzT8T6mb8&list=PLGDP1Irs2PmWNwAwMPdyOxCqkFqB6gtp9&index=7&t=1287s 
> <https://urldefense.com/v3/__https://www.youtube.com/watch?v=NwXzT8T6mb8&list=PLGDP1Irs2PmWNwAwMPdyOxCqkFqB6gtp9&index=7&t=1287s__;!!GqivPVa7Brio!Pf9Nt42jivBKFcxTzsgBVXVMil8JZN4IRFDQyoAO0Z-g4vnp-zkHGM8CgPO6aZD4Sn1yqXM$>
> 2. A more complex example (in memory off-heap analytics and memory 
> mapped files) is the sleep analytics:
> Code is here: 
> https://github.com/knowledge-base-and-tutorials/java14-features/blob/master/src/main/java/com/github/kbnt/java14/fma/SleepAnalytics.java 
> <https://urldefense.com/v3/__https://github.com/knowledge-base-and-tutorials/java14-features/blob/master/src/main/java/com/github/kbnt/java14/fma/SleepAnalytics.java__;!!GqivPVa7Brio!Pf9Nt42jivBKFcxTzsgBVXVMil8JZN4IRFDQyoAO0Z-g4vnp-zkHGM8CgPO6aZD4obcpcxw$> 
> (the class' javadoc should describe it)
> Presentation of the case is here: 
> https://www.youtube.com/watch?v=NwXzT8T6mb8&list=PLGDP1Irs2PmWNwAwMPdyOxCqkFqB6gtp9&index=7&t=1734s 
> <https://urldefense.com/v3/__https://www.youtube.com/watch?v=NwXzT8T6mb8&list=PLGDP1Irs2PmWNwAwMPdyOxCqkFqB6gtp9&index=7&t=1734s__;!!GqivPVa7Brio!Pf9Nt42jivBKFcxTzsgBVXVMil8JZN4IRFDQyoAO0Z-g4vnp-zkHGM8CgPO6aZD47XZakCQ$>
>
> If you find anything useful in those and want to use but the license 
> (Apache 2.0 for the code and CC-BY-SA for the videos) is in the way, 
> let me know and I can change them to something friendlier (where e.g. 
> no attribution is needed).
>
> Cheers!
>   Chris T
>
>
> On Fri, Feb 14, 2020 at 6:26 PM Maurizio Cimadamore 
> <maurizio.cimadamore at oracle.com 
> <mailto:maurizio.cimadamore at oracle.com>> wrote:
>
>
>     On 13/02/2020 03:39, Chris T wrote:
>>     No problem! Nice talk at FOSDEM, Maurizio ;-)!
>>
>>     One suggestion, for future talks - when it comes to memory
>>     layouts please construct an example that is a little bit more
>>     complex (by end of the upcoming weekend I will publish one that
>>     can be used). I had trouble with bit alignment when working on
>>     mine, but I will come back with the details (no bug or anything
>>     but more clarity would be beneficial in the docs). In your
>>     example (the Point one) the alignment is 32 but that is now
>>     always the case... The reason I mention this as an issue is that
>>     the Java development community is more "high-level". Believe it
>>     or not, bit alignment is not anymore "a thing" with most of us ;-)...
>
>     Hey Chris - on alignment there was an issue that was uncovered on
>     panama-dev - I think this fix should probably be ported to mainline:
>
>     https://bugs.openjdk.java.net/browse/JDK-8238320
>
>     This might make working with layout with padding a bit more
>     tedious than intended, although this is probably not what you ran
>     into.
>
>     Re: talk suggestion - yes, more realistic examples would probably
>     be better - but it's always hard to strike the right balance; if
>     the example is more realistic it can be harder to follow, which
>     might not be good when you are showing new concepts. But I'll keep
>     that in mind for the future - after all this is a new API, and, as
>     it's always the case, the more you think about how to explain
>     these concepts and the more you do it, the more ways you find to
>     get the message across in an optimal way.
>
>     Thanks!
>     Maurizio
>
>
>>
>>     I will also think about some API enhancements I would like to see
>>     as a developer...
>>
>>     Thanks!
>>       Chris T
>>
>>     On Tue, Feb 11, 2020 at 4:07 AM Maurizio Cimadamore
>>     <maurizio.cimadamore at oracle.com
>>     <mailto:maurizio.cimadamore at oracle.com>> wrote:
>>
>>         Thanks Paul!
>>
>>         Maurizio
>>
>>         On 10/02/2020 17:58, Paul Sandoz wrote:
>>         > I modified the JEP with updated code snippets that compile
>>         against the latest API in JDK 14 [*].
>>


More information about the core-libs-dev mailing list