RFR JDK-8234049: Implementation of Memory Access API (Incubator)

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Sat Dec 7 19:02:24 UTC 2019


On 06/12/2019 21:04, Paul Sandoz wrote:
> GroupLayout.java
>>
>    80         OptionalLong sizeof(List<MemoryLayout> elems) {
>    81             long size = 0;
>    82             for (MemoryLayout elem : elems) {
>    83                 if (AbstractLayout.optSize(elem).isPresent()) {
>    84                     size = sizeOp.applyAsLong(size, elem.bitSize());
>    85                 } else {
>    86                     return OptionalLong.empty();
>    87                 }
>    88             }
>    89             return OptionalLong.of(size);
>    90         }
>
> FWIW you can do this:
>
> OptionalLong sizeof(List<MemoryLayout> elems) {
>      return elems.stream().filter(e -> AbstractLayout.optSize(e).isPresent()).mapToLong(MemoryLayout::bitSize)
>              .reduce(sizeOp);


Looked at this more carefully - the code you suggest has a slightly 
different behavior than the one I wrote - note that the original code 
short-circuit when the first layout member with no size is encountered. 
IIUC your code will just drop unsized member layouts, and compute size 
on the rest - which is not what I had in mind. Am I understanding correctly?

Maurizio



More information about the core-libs-dev mailing list