[foreign-memaccess+abi] RFR: 8291639: Improve the ability to visualize a MemorySegment in human readable forms [v14]

Per Minborg pminborg at openjdk.org
Wed May 31 08:55:24 UTC 2023


On Wed, 31 May 2023 08:31:24 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> This PR proposes to add a "Record Mapper" that allows `MemorySegment` instances to be projected onto `Record` types given `GroupLayout` instances.
>> 
>> Here is a simple example of how to use the mapper:
>> 
>> 
>>     private static final GroupLayout POINT_LAYOUT = MemoryLayout.structLayout(
>>             JAVA_INT.withName("x"),
>>             JAVA_INT.withName("y"));
>> 
>>     MemorySegment segment =  MemorySegment.ofArray(new int[]{3, 4});
>> 
>>     Point point = POINT_LAYOUT.recordMapper(Point.class)
>>           .apply(segment); // Point[x=3, y=4]
>> 
>> 
>> I think the implementation can be improved later, for example by de-duplicating handling of arrays and maybe add recursive "un-pealing" of multidimensional arrays.
>
> Per Minborg has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Use invokeExact and raw method handles for composition

With the composed method handles, performance now looks excellent. Here is a benchmark where we materialize a `record Point(int x, int y)` (benchmarks 'pointMapper') and an array of three `int` components (benchmarks 'arrayMapper'):


Benchmark                         Mode  Cnt  Score   Error  Units
RecordMapper.arrayExplicitMapper  avgt   30  9.361 ± 0.060  ns/op
RecordMapper.arrayMapper          avgt   30  7.697 ± 0.035  ns/op
RecordMapper.pointExplicitMapper  avgt   30  2.235 ± 0.016  ns/op
RecordMapper.pointMapper          avgt   30  2.299 ± 0.007  ns/op


The explicit mappers are hand-crafted mappers like this one:


private static final Function<MemorySegment, Point> POINT_EXPLICIT_MAPPER =
        ms -> new Point(ms.get(JAVA_INT, 0L), ms.get(JAVA_INT, 4));

-------------

PR Comment: https://git.openjdk.org/panama-foreign/pull/833#issuecomment-1569771713


More information about the panama-dev mailing list