8343188: Investigate ways to simplify MemorySegment::ofBuffer

Reviewed-by: mcimadamore
This commit is contained in:
Per Minborg 2024-11-04 11:06:25 +00:00
parent 7f131a9e1e
commit f69b6016d6
9 changed files with 307 additions and 66 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -37,11 +37,9 @@ import jdk.internal.misc.VM.BufferPool;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.ForceInline;
import java.io.FileDescriptor;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Reference;
import java.util.List;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -780,6 +778,23 @@ public abstract sealed class Buffer
return Preconditions.checkIndex(i, limit - nb + 1, IOOBE_FORMATTER);
}
/**
* {@return the scale shifts for this Buffer}
* <p>
* The scale shifts are:
* ByteBuffer: 0
* ShortBuffer, CharBuffer: 1
* IntBuffer, FloatBuffer: 2
* LongBuffer, DoubleBuffer: 3
*/
abstract int scaleShifts();
abstract AbstractMemorySegmentImpl heapSegment(Object base,
long offset,
long length,
boolean readOnly,
MemorySessionImpl bufferScope);
final int markValue() { // package-private
return mark;
}
@ -832,6 +847,7 @@ public abstract sealed class Buffer
return new HeapByteBuffer(hb, -1, 0, capacity, capacity, offset, segment);
}
@ForceInline
@Override
public Object getBufferBase(Buffer buffer) {
return buffer.base();
@ -906,6 +922,23 @@ public abstract sealed class Buffer
public int pageSize() {
return Bits.pageSize();
}
@ForceInline
@Override
public int scaleShifts(Buffer buffer) {
return buffer.scaleShifts();
}
@ForceInline
@Override
public AbstractMemorySegmentImpl heapSegment(Buffer buffer,
Object base,
long offset,
long length,
boolean readOnly,
MemorySessionImpl bufferScope) {
return buffer.heapSegment(base, offset, length, readOnly, bufferScope);
}
});
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,10 @@
package java.nio;
import java.lang.foreign.MemorySegment;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.foreign.SegmentFactories;
import jdk.internal.vm.annotation.ForceInline;
import java.util.Objects;
import jdk.internal.misc.Unsafe;
@ -246,6 +250,21 @@ class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private
#end[char]
@ForceInline
@Override
int scaleShifts() {
return Integer.numberOfTrailingZeros($Fulltype$.BYTES);
}
@ForceInline
@Override
AbstractMemorySegmentImpl heapSegment(Object base,
long offset,
long length,
boolean readOnly,
MemorySessionImpl bufferScope) {
return SegmentFactories.arrayOfByteSegment(base, offset, length, readOnly, bufferScope);
}
public ByteOrder order() {
#if[boB]

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,7 +31,10 @@ import java.io.FileDescriptor;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Reference;
import java.util.Objects;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.foreign.SegmentFactories;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.misc.ScopedMemoryAccess.ScopedAccessError;
import jdk.internal.misc.VM;
import jdk.internal.ref.Cleaner;
@ -528,6 +531,24 @@ class Direct$Type$Buffer$RW$$BO$
}
#end[char]
#if[byte]
@ForceInline
@Override
int scaleShifts() {
return 0;
}
@ForceInline
@Override
AbstractMemorySegmentImpl heapSegment(Object base,
long offset,
long length,
boolean readOnly,
MemorySessionImpl bufferScope) {
// Direct buffers are not backed by an array.
throw new UnsupportedOperationException();
}
#end[byte]
#if[byte]
// #BIN

View file

@ -28,6 +28,10 @@
package java.nio;
import java.lang.foreign.MemorySegment;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.foreign.SegmentFactories;
import jdk.internal.vm.annotation.ForceInline;
import java.util.Objects;
/**
@ -735,6 +739,23 @@ class Heap$Type$Buffer$RW$
#end[char]
#if[byte]
@ForceInline
@Override
int scaleShifts() {
return 0;
}
@ForceInline
@Override
AbstractMemorySegmentImpl heapSegment(Object base,
long offset,
long length,
boolean readOnly,
MemorySessionImpl bufferScope) {
return SegmentFactories.arrayOf$Type$Segment(base, offset, length, readOnly, bufferScope);
}
#end[byte]
#if[!byte]

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -37,6 +37,10 @@ import java.util.stream.StreamSupport;
import java.util.stream.$Streamtype$Stream;
#end[streamableType]
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.foreign.SegmentFactories;
import jdk.internal.vm.annotation.ForceInline;
import java.lang.foreign.MemorySegment;
import java.util.Objects;
import jdk.internal.util.ArraysSupport;
@ -2321,6 +2325,22 @@ public abstract sealed class $Type$Buffer
#end[byte]
@ForceInline
@Override
int scaleShifts() {
return Integer.numberOfTrailingZeros($Fulltype$.BYTES);
}
@ForceInline
@Override
AbstractMemorySegmentImpl heapSegment(Object base,
long offset,
long length,
boolean readOnly,
MemorySessionImpl bufferScope) {
return SegmentFactories.arrayOf$Type$Segment(base, offset, length, readOnly, bufferScope);
}
#if[streamableType]
#if[char]