Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java): fix bytebuffer no such method error #1580

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.fury.benchmark.state.ObjectType;
import org.apache.fury.benchmark.state.ProtoBuffersState;
import org.apache.fury.benchmark.state.ProtostuffState;
import org.apache.fury.memory.Platform;
import org.apache.fury.memory.ByteBufferUtil;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand Down Expand Up @@ -125,7 +125,7 @@ public Object protobuffers_deserialize(ProtoBuffersState.ProtoBuffersUserTypeSta

@Benchmark
public Object flatbuffers_deserialize(FlatBuffersState.FlatBuffersUserTypeState state) {
Platform.clearBuffer(state.deserializedData);
ByteBufferUtil.clearBuffer(state.deserializedData);
if (state.objectType == ObjectType.SAMPLE) {
return FlatBuffersState.deserializeSample(state.deserializedData);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.fury.benchmark.state.ObjectType;
import org.apache.fury.benchmark.state.ProtoBuffersState;
import org.apache.fury.benchmark.state.ProtostuffState;
import org.apache.fury.memory.Platform;
import org.apache.fury.memory.ByteBufferUtil;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand Down Expand Up @@ -133,7 +133,7 @@ public byte[] protobuffers_serialize(ProtoBuffersState.ProtoBuffersUserTypeState

@Benchmark
public Object flatbuffers_serialize(FlatBuffersState.FlatBuffersUserTypeState state) {
Platform.clearBuffer(state.directBuffer);
ByteBufferUtil.clearBuffer(state.directBuffer);
if (state.objectType == ObjectType.SAMPLE) {
return FlatBuffersState.serializeSample((Sample) state.object, state.directBuffer);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import org.apache.fury.benchmark.data.SerializableByteBuffer;
import org.apache.fury.benchmark.state.BufferType;
import org.apache.fury.config.Language;
import org.apache.fury.memory.ByteBufferUtil;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.MemoryUtils;
import org.apache.fury.memory.Platform;
import org.apache.fury.serializer.BufferObject;
import org.apache.fury.test.bean.ArraysData;
import org.apache.fury.util.Preconditions;
Expand Down Expand Up @@ -314,7 +314,7 @@ public Object jsonb_serialize(JsonBState state, Blackhole bh) {
public static Object jsonbSerialize(JsonBState state, Blackhole bh) {
byte[] bytes = JSONB.toBytes(state.data, state.jsonbWriteFeatures);
if (state.bufferType == BufferType.directBuffer) {
Platform.clearBuffer(state.directBuffer);
ByteBufferUtil.clearBuffer(state.directBuffer);
state.directBuffer.put(bytes);
}
if (bh != null) {
Expand All @@ -331,7 +331,7 @@ public Object jsonb_deserialize(JsonBState state, Blackhole bh) {

public static Object jsonbDeserialize(JsonBState state, Blackhole bh) {
if (state.bufferType == BufferType.directBuffer) {
Platform.rewind(state.directBuffer);
ByteBufferUtil.rewind(state.directBuffer);
byte[] bytes = new byte[state.buffer.length];
state.directBuffer.get(bytes);
Object newObj = JSONB.parseObject(bytes, Object.class, state.jsonbReaderFeatures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.apache.fury.benchmark.state.generated.FBSMedia;
import org.apache.fury.benchmark.state.generated.FBSMediaContent;
import org.apache.fury.benchmark.state.generated.FBSSample;
import org.apache.fury.memory.Platform;
import org.apache.fury.memory.ByteBufferUtil;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
Expand Down Expand Up @@ -439,7 +439,7 @@ public void setup() {
} else {
deserializedData = ByteBuffer.wrap(data);
}
Platform.clearBuffer(deserializedData);
ByteBufferUtil.clearBuffer(deserializedData);
Object newObj = deserializeFunc.apply(deserializedData);
Preconditions.checkArgument(object.equals(newObj));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.fury.benchmark.data.Image;
import org.apache.fury.benchmark.data.Media;
import org.apache.fury.benchmark.data.MediaContent;
import org.apache.fury.memory.Platform;
import org.apache.fury.memory.ByteBufferUtil;
import org.apache.fury.util.Preconditions;
import org.nustaq.serialization.FSTConfiguration;
import org.openjdk.jmh.annotations.CompilerControl;
Expand Down Expand Up @@ -93,7 +93,7 @@ public static byte[] serialize(
Blackhole blackhole) {
byte[] bytes = fst.asSharedByteArray(value, out);
if (bufferType == BufferType.directBuffer) {
Platform.clearBuffer(directBuffer);
ByteBufferUtil.clearBuffer(directBuffer);
directBuffer.put(bytes, 0, out[0]);
}
if (blackhole != null) {
Expand All @@ -116,7 +116,7 @@ public static Object deserialize(
ByteBuffer directBuffer,
Blackhole blackhole) {
if (bufferType == BufferType.directBuffer) {
Platform.rewind(directBuffer);
ByteBufferUtil.rewind(directBuffer);
byte[] bytes = new byte[out[0]];
directBuffer.get(bytes);
Object newObj = fst.asObject(bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.apache.fury.benchmark.data.CustomJDKSerialization;
import org.apache.fury.logging.Logger;
import org.apache.fury.logging.LoggerFactory;
import org.apache.fury.memory.Platform;
import org.apache.fury.memory.ByteBufferUtil;
import org.apache.fury.util.Preconditions;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void setup() {
"======> Jsonb | {} | {} | {} | {} |", objectType, references, bufferType, buffer.length);
if (bufferType == BufferType.directBuffer) {
directBuffer.put(buffer);
Platform.clearBuffer(directBuffer);
ByteBufferUtil.clearBuffer(directBuffer);
}
Preconditions.checkArgument(object.equals(deserialize(null, this)));
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public static JSONReader.Feature[] getJsonbReaderConfig(boolean refTracking) {
public static byte[] serialize(Blackhole blackhole, JsonbBenchmarkState state, Object value) {
byte[] bytes = JSONB.toBytes(value, state.jsonbWriteFeatures);
if (state.bufferType == BufferType.directBuffer) {
Platform.clearBuffer(state.directBuffer);
ByteBufferUtil.clearBuffer(state.directBuffer);
state.directBuffer.put(bytes);
}
if (blackhole != null) {
Expand All @@ -139,7 +139,7 @@ public static byte[] serialize(Blackhole blackhole, JsonbBenchmarkState state, O

public static Object deserialize(Blackhole blackhole, JsonbBenchmarkState state) {
if (state.bufferType == BufferType.directBuffer) {
Platform.rewind(state.directBuffer);
ByteBufferUtil.rewind(state.directBuffer);
byte[] bytes = new byte[state.buffer.length];
state.directBuffer.get(bytes);
Object newObj = JSONB.parseObject(bytes, Object.class, state.jsonbReaderFeatures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.channels.ReadableByteChannel;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.fury.exception.DeserializationException;
import org.apache.fury.memory.ByteBufferUtil;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.Platform;
import org.apache.fury.util.Preconditions;
Expand Down Expand Up @@ -60,7 +61,7 @@ public int fillBuffer(int minFillSize) {
byteBuf.position(0);
newByteBuf.put(byteBuf);
byteBuf = byteBuffer = newByteBuf;
memoryBuf.initDirectBuffer(Platform.getAddress(byteBuf), position, byteBuf);
memoryBuf.initDirectBuffer(ByteBufferUtil.getAddress(byteBuf), position, byteBuf);
}
byteBuf.limit(newLimit);
int readCount = channel.read(byteBuf);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.memory;

import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import org.apache.fury.util.Preconditions;

public class ByteBufferUtil {
private static final long BUFFER_ADDRESS_FIELD_OFFSET;
private static final long BUFFER_CAPACITY_FIELD_OFFSET;

static {
try {
Field addressField = Buffer.class.getDeclaredField("address");
BUFFER_ADDRESS_FIELD_OFFSET = Platform.objectFieldOffset(addressField);
Preconditions.checkArgument(BUFFER_ADDRESS_FIELD_OFFSET != 0);
Field capacityField = Buffer.class.getDeclaredField("capacity");
BUFFER_CAPACITY_FIELD_OFFSET = Platform.objectFieldOffset(capacityField);
Preconditions.checkArgument(BUFFER_CAPACITY_FIELD_OFFSET != 0);
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
}
}

public static long getAddress(ByteBuffer buffer) {
Preconditions.checkNotNull(buffer, "buffer is null");
Preconditions.checkArgument(buffer.isDirect(), "Can't get address of a non-direct ByteBuffer.");
long offHeapAddress;
try {
offHeapAddress = Platform.getLong(buffer, BUFFER_ADDRESS_FIELD_OFFSET);
} catch (Throwable t) {
throw new Error("Could not access direct byte buffer address field.", t);
}
return offHeapAddress;
}

private static final ByteBuffer localBuffer = ByteBuffer.allocateDirect(0);

/** Create a direct buffer from native memory represented by address [address, address + size). */
public static ByteBuffer createDirectByteBufferFromNativeAddress(long address, int size) {
try {
// ByteBuffer.allocateDirect(0) is about 30x slower than `localBuffer.duplicate()`.
ByteBuffer buffer = localBuffer.duplicate();
Platform.putLong(buffer, BUFFER_ADDRESS_FIELD_OFFSET, address);
Platform.putInt(buffer, BUFFER_CAPACITY_FIELD_OFFSET, size);
buffer.clear();
return buffer;
} catch (Throwable t) {
throw new Error("Failed to wrap unsafe off-heap memory with ByteBuffer", t);
}
}

/** Wrap a buffer [address, address + size) into provided <code>buffer</code>. */
public static void wrapDirectByteBufferFromNativeAddress(
ByteBuffer buffer, long address, int size) {
Preconditions.checkArgument(
buffer.isDirect(), "Can't wrap native memory into a non-direct ByteBuffer.");
Platform.putLong(buffer, BUFFER_ADDRESS_FIELD_OFFSET, address);
Platform.putInt(buffer, BUFFER_CAPACITY_FIELD_OFFSET, size);
buffer.clear();
}

public static ByteBuffer wrapDirectBuffer(long address, int size) {
return createDirectByteBufferFromNativeAddress(address, size);
}

/** Wrap a buffer [address, address + size) into provided <code>buffer</code>. */
public static void wrapDirectBuffer(ByteBuffer buffer, long address, int size) {
Platform.putLong(buffer, BUFFER_ADDRESS_FIELD_OFFSET, address);
Platform.putInt(buffer, BUFFER_CAPACITY_FIELD_OFFSET, size);
buffer.clear();
}

public static void clearBuffer(Buffer buffer) {
buffer.clear();
}

public static void flipBuffer(Buffer buffer) {
buffer.flip();
}

public static void rewind(Buffer buffer) {
buffer.rewind();
}

public static void position(Buffer buffer, int pos) {
buffer.position(pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public void get(int offset, ByteBuffer target, int numBytes) {
}
final int targetPos = target.position();
if (target.isDirect()) {
final long targetAddr = Platform.getAddress(target) + targetPos;
final long targetAddr = ByteBufferUtil.getAddress(target) + targetPos;
final long sourceAddr = address + offset;
if (sourceAddr <= addressLimit - numBytes) {
Platform.copyMemory(heapMemory, sourceAddr, null, targetAddr, numBytes);
Expand All @@ -340,7 +340,7 @@ public void get(int offset, ByteBuffer target, int numBytes) {
assert target.hasArray();
get(offset, target.array(), targetPos + target.arrayOffset(), numBytes);
}
target.position(targetPos + numBytes);
ByteBufferUtil.position(target, targetPos + numBytes);
}

public void put(int offset, ByteBuffer source, int numBytes) {
Expand All @@ -350,7 +350,7 @@ public void put(int offset, ByteBuffer source, int numBytes) {
}
final int sourcePos = source.position();
if (source.isDirect()) {
final long sourceAddr = Platform.getAddress(source) + sourcePos;
final long sourceAddr = ByteBufferUtil.getAddress(source) + sourcePos;
final long targetAddr = address + offset;
if (targetAddr <= addressLimit - numBytes) {
Platform.copyMemory(null, sourceAddr, heapMemory, targetAddr, numBytes);
Expand All @@ -361,7 +361,7 @@ public void put(int offset, ByteBuffer source, int numBytes) {
assert source.hasArray();
put(offset, source.array(), sourcePos + source.arrayOffset(), numBytes);
}
source.position(sourcePos + numBytes);
ByteBufferUtil.position(source, sourcePos + numBytes);
}

public void put(int index, byte[] src) {
Expand Down Expand Up @@ -2465,12 +2465,12 @@ public ByteBuffer sliceAsByteBuffer(int offset, int length) {
ByteBuffer offHeapBuffer = this.offHeapBuffer;
if (offHeapBuffer != null) {
ByteBuffer duplicate = offHeapBuffer.duplicate();
int start = (int) (address - Platform.getAddress(duplicate));
duplicate.position(start + offset);
int start = (int) (address - ByteBufferUtil.getAddress(duplicate));
ByteBufferUtil.position(duplicate, start + offset);
duplicate.limit(start + offset + length);
return duplicate.slice();
} else {
return Platform.createDirectByteBufferFromNativeAddress(address + offset, length);
return ByteBufferUtil.createDirectByteBufferFromNativeAddress(address + offset, length);
}
}
}
Expand Down Expand Up @@ -2553,7 +2553,7 @@ public static MemoryBuffer fromByteArray(byte[] buffer) {
public static MemoryBuffer fromByteBuffer(ByteBuffer buffer) {
if (buffer.isDirect()) {
return new MemoryBuffer(
Platform.getAddress(buffer) + buffer.position(), buffer.remaining(), buffer);
ByteBufferUtil.getAddress(buffer) + buffer.position(), buffer.remaining(), buffer);
} else {
int offset = buffer.arrayOffset() + buffer.position();
return new MemoryBuffer(buffer.array(), offset, buffer.remaining());
Expand All @@ -2562,7 +2562,7 @@ public static MemoryBuffer fromByteBuffer(ByteBuffer buffer) {

public static MemoryBuffer fromDirectByteBuffer(
ByteBuffer buffer, int size, FuryStreamReader streamReader) {
long offHeapAddress = Platform.getAddress(buffer) + buffer.position();
long offHeapAddress = ByteBufferUtil.getAddress(buffer) + buffer.position();
return new MemoryBuffer(offHeapAddress, size, buffer, streamReader);
}

Expand Down
Loading
Loading