Skip to content

Commit

Permalink
chimera: avoid extra byte array creation during inode2bytes conversion
Browse files Browse the repository at this point in the history
Motivation:
we create a an array with a fixed size and later on creating a copy of
a part of it with significant data.

Modification:
pre-calculate byte array size and avoid copy.

Result:
no visible changes.

Acked-by: Gerd Behrmann
Target: master
Require-book: no
Require-notes: no
  • Loading branch information
kofemann committed Jun 20, 2016
1 parent e4f1b42 commit 7650505
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions modules/chimera/src/main/java/org/dcache/chimera/FsInode.java
Expand Up @@ -19,7 +19,6 @@
import com.google.common.primitives.Longs;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -142,12 +141,22 @@ public FsInodeType type() {
}

/**
* A helper method to generate the base path of identifier.
* A helper method to generate the base part of identifier.
* @param opaque inode specific data
* @return
* @return byte array identifying the inode.
*/
protected final byte[] byteBase(byte[] opaque) {
ByteBuffer b = ByteBuffer.allocate(128);
/**
* allocate array with a correct number of bytes:
* 1 - fs is
* 1 - inode type
* 1 - number of bytes in ino, for compatibility
* 8 - ino,
* 1 - size of type specific data
* opaque.len - opaque data
*/
byte[] bytes = new byte[1 + 1 + 1 + Long.BYTES + 1 + opaque.length];
ByteBuffer b = ByteBuffer.wrap(bytes);
b.put((byte) _fs.getFsId())
.put((byte) _type.getType())
.put((byte)Long.BYTES) // set the file id size to be compatible with old format
Expand All @@ -156,7 +165,7 @@ protected final byte[] byteBase(byte[] opaque) {
b.put((byte) opaque.length);
b.put(opaque);

return Arrays.copyOf(b.array(), b.position());
return bytes;
}

/**
Expand Down

0 comments on commit 7650505

Please sign in to comment.