RFR: 8299576: Reimplement java.io.Bits using VarHandle access [v7]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Jan 9 10:36:53 UTC 2023


On Mon, 9 Jan 2023 09:22:25 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> Currently, `java.io.Bits` is using explicit logic to read/write various primitive types to/from byte arrays. Switching to the use of `VarHandle` access would provide better performance and less code. 
>> 
>> Also, using a standard API for these conversions means future `VarHandle` improvements will benefit `Bits` too. 
>> 
>> Improvements in `Bits` will propagate to `ObjectInputStream`, `ObjectOutputStream` and `RandomAccessFile`.
>> 
>> Initial benchmarks and performance discussions can be found here: https://github.com/openjdk/panama-foreign/pull/762
>
> Per Minborg has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Remove faulty test tag, improve other test tag, fix comments

src/java.base/share/classes/java/io/Bits.java line 77:

> 75:         // Using Double.longBitsToDouble collapses NaN values to a single
> 76:         // "canonical" NaN value
> 77:         return Double.longBitsToDouble((long) LONG.get(b, off));

For the records, I believe the template classes for byte array var handle already perform this conversion - e.g.


@ForceInline
        static $type$ get(VarHandle ob, Object oba, int index) {
            ArrayHandle handle = (ArrayHandle)ob;
            byte[] ba = (byte[]) oba;
#if[floatingPoint]
            $rawType$ rawValue = UNSAFE.get$RawType$Unaligned(
                    ba,
                    ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
                    handle.be);
            return $Type$.$rawType$BitsTo$Type$(rawValue); // <-----------------------------
#else[floatingPoint]
            return UNSAFE.get$Type$Unaligned(
                    ba,
                    ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
                    handle.be);
#end[floatingPoint]
        }
 ````

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

PR: https://git.openjdk.org/jdk/pull/11840


More information about the core-libs-dev mailing list