You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2023/04/07 21:18:58 UTC
[jena] branch main updated: GH-1803: Keep the old code for byte buffer copy
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/main by this push:
new 4dada47b03 GH-1803: Keep the old code for byte buffer copy
new 9f3f5ae8f3 Merge pull request #1833 from afs/buff-old
4dada47b03 is described below
commit 4dada47b03356ab5f40788490a8e96fef43b9a9b
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Fri Apr 7 15:25:29 2023 +0100
GH-1803: Keep the old code for byte buffer copy
Add comments
Rename ByteBufferLib.duplicate
---
.../org/apache/jena/atlas/lib/ByteBufferLib.java | 117 +++++++++++++++------
.../apache/jena/tdb/store/nodetable/TestNodec.java | 2 +-
2 files changed, 85 insertions(+), 34 deletions(-)
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/lib/ByteBufferLib.java b/jena-base/src/main/java/org/apache/jena/atlas/lib/ByteBufferLib.java
index 42714491a0..2b57ae4aed 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/lib/ByteBufferLib.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/lib/ByteBufferLib.java
@@ -29,10 +29,12 @@ public class ByteBufferLib {
private ByteBufferLib() {}
+ /** Fill the byte buffer from position to limit with a byte value */
public static void fill(ByteBuffer bb, byte v) {
fill(bb, bb.position(), bb.limit(), v) ;
}
+ /** Fill the section of the byte buffer from start (inc) to finish (exc) to limit with a byte value */
public static void fill(ByteBuffer bb, int start, int finish, byte v) {
for ( int i = start ; i < finish ; i++ )
bb.put(i, v) ;
@@ -75,6 +77,7 @@ public class ByteBufferLib {
out.println() ;
}
+ /** Test whether two byte buffers have the same byte content */
public static boolean sameValue(ByteBuffer bb1, ByteBuffer bb2) {
if ( bb1.capacity() != bb2.capacity() )
return false ;
@@ -85,11 +88,8 @@ public class ByteBufferLib {
return true ;
}
- /**
- * Copy of a ByteBuffer - the contents are copied (unlike
- * ByteBuffer.duplicate)
- */
- final public static ByteBuffer duplicate(ByteBuffer bb) {
+ /** Copy of a ByteBuffer - the contents are copied (unlike {@link ByteBuffer#duplicate}) */
+ final public static ByteBuffer copyOf(ByteBuffer bb) {
ByteBuffer bb2 = ByteBuffer.allocate(bb.limit() - bb.position()) ;
int x = bb.position() ;
bb2.put(bb) ;
@@ -112,7 +112,11 @@ public class ByteBufferLib {
b[j++] = bb.get(i++) ;
}
- // For non-array versions : beware of overlaps.
+ // --------
+
+ /**
+ * Copy to section bytes from src to dst within the same {@link ByteBuffer}.
+ */
final public static void bbcopy(ByteBuffer bb, int src, int dst, int length, int slotLen) {
if ( src == dst )
return ;
@@ -125,7 +129,24 @@ public class ByteBufferLib {
bbcopyBulk(bb, src, dst, length, slotLen) ;
}
+ final private static void acopyArray(ByteBuffer bb, int src, int dst, int length, int slotLen) {
+ byte[] b = bb.array() ;
+
+ int offset = bb.arrayOffset() ;
+
+ int bSrc = src * slotLen ;
+ int bDst = dst * slotLen ;
+ int bLen = length * slotLen ;
+
+ arraycopy(b, offset + bSrc, b, offset + bDst, bLen) ;
+ }
+
+ /**
+ * Bulk copy, using an intermediate byte[].
+ */
private final static void bbcopyBulk(ByteBuffer bb, int src, int dst, int length, int slotLen) {
+ // 2023-03 - this was shown to be faster than the old way that did
+ // not create an object but can't use bulk operations.
int bDst = dst * slotLen ;
int bSrc = src * slotLen ;
int bLen = length * slotLen ;
@@ -139,10 +160,54 @@ public class ByteBufferLib {
bb.position(bDst);
bb.put(srcBytes, 0, bLen);
bb.position(pos);
+ }
+
+ // --------
+ // For the record ...
+ /**
+ * Bulk copy, without using an intermediate byte[].
+ */
+ private final static void bbcopyOLD(ByteBuffer bb, int src, int dst, int length, int slotLen) {
+ if ( src == dst )
+ return ;
+
+ if ( allowArray && bb.hasArray() ) {
+ acopyArray(bb, src, dst, length, slotLen) ;
+ return ;
+ }
+
+ if ( src < dst )
+ bbcopy1(bb, src, dst, length, slotLen) ;
+ else
+ bbcopy2(bb, src, dst, length, slotLen) ;
+ }
+
+ private final static void bbcopy1(ByteBuffer bb, int src, int dst, int length, int slotLen) {
+ int bDst = dst * slotLen ;
+ int bSrc = src * slotLen ;
+ int bLen = length * slotLen ;
+ // src < dst so top dst is not in the overlap : work backwards
+ for ( int i = bLen - 1 ; i >= 0 ; i-- )
+ bb.put(bDst + i, bb.get(bSrc + i)) ;
+ }
+
+ private final static void bbcopy2(ByteBuffer bb, int src, int dst, int length, int slotLen) {
+ int bDst = dst * slotLen ;
+ int bSrc = src * slotLen ;
+ int bLen = length * slotLen ;
+ // src > dst so dst[0] is not in the overlap
+ for ( int i = 0 ; i < bLen ; i++ )
+ bb.put(bDst + i, bb.get(bSrc + i)) ;
}
+ // --------
+
+ /** Copy a section of bytes from one {@link ByteBuffer} to a different {@link ByteBuffer} */
public final static void bbcopy(ByteBuffer bb1, int src, ByteBuffer bb2, int dst, int length, int slotLen) {
+ if ( bb1 == bb2 )
+ throw new IllegalArgumentException("ByteBuffers are the same object");
+
// Assume bb1 and bb2 are different and do not overlap.
if ( allowArray && bb1.hasArray() && bb2.hasArray() ) {
acopyArray(bb1, src, bb2, dst, length, slotLen) ;
@@ -168,33 +233,6 @@ public class ByteBufferLib {
bb2.position(pos2);
}
- final public static void bbfill(ByteBuffer bb, int fromIdx, int toIdx, byte fillValue, int slotLen) {
- if ( allowArray && bb.hasArray() ) {
- afillArray(bb, fromIdx, toIdx, fillValue, slotLen) ;
- return ;
- }
-
- int bStart = fromIdx * slotLen ;
- int bFinish = toIdx * slotLen ;
-
- for ( int i = bStart ; i < bFinish ; i++ )
- bb.put(i, fillValue) ;
- }
-
- // To ArrayOps?
-
- final private static void acopyArray(ByteBuffer bb, int src, int dst, int length, int slotLen) {
- byte[] b = bb.array() ;
-
- int offset = bb.arrayOffset() ;
-
- int bSrc = src * slotLen ;
- int bDst = dst * slotLen ;
- int bLen = length * slotLen ;
-
- arraycopy(b, offset + bSrc, b, offset + bDst, bLen) ;
- }
-
final private static void acopyArray(ByteBuffer bb1, int src, ByteBuffer bb2, int dst, int length, int slotLen) {
byte[] b1 = bb1.array() ;
byte[] b2 = bb2.array() ;
@@ -208,6 +246,19 @@ public class ByteBufferLib {
arraycopy(b1, offset1 + bSrc, b2, offset2 + bDst, bLen) ;
}
+ final public static void bbfill(ByteBuffer bb, int fromIdx, int toIdx, byte fillValue, int slotLen) {
+ if ( allowArray && bb.hasArray() ) {
+ afillArray(bb, fromIdx, toIdx, fillValue, slotLen) ;
+ return ;
+ }
+
+ int bStart = fromIdx * slotLen ;
+ int bFinish = toIdx * slotLen ;
+
+ for ( int i = bStart ; i < bFinish ; i++ )
+ bb.put(i, fillValue) ;
+ }
+
final private static void afillArray(ByteBuffer bb, int fromIdx, int toIdx, byte fillValue, int slotLen) {
int offset = bb.arrayOffset() ;
int bStart = fromIdx * slotLen ;
diff --git a/jena-tdb1/src/test/java/org/apache/jena/tdb/store/nodetable/TestNodec.java b/jena-tdb1/src/test/java/org/apache/jena/tdb/store/nodetable/TestNodec.java
index 1641cf7ff6..3c89563c1e 100644
--- a/jena-tdb1/src/test/java/org/apache/jena/tdb/store/nodetable/TestNodec.java
+++ b/jena-tdb1/src/test/java/org/apache/jena/tdb/store/nodetable/TestNodec.java
@@ -120,7 +120,7 @@ public class TestNodec
assertEquals(bbLen, x);
assertEquals(0, bb.position());
- ByteBuffer bb2 = ByteBufferLib.duplicate(bb);
+ ByteBuffer bb2 = ByteBufferLib.copyOf(bb);
Node n2 = nodec.decode(bb2, null);
assertEquals(n, n2);
}