[
https://issues.apache.org/jira/browse/HBASE-15233?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15281486#comment-15281486
]
Jean-Marc Spaggiari commented on HBASE-15233:
---------------------------------------------
So. I search a bit for some places where this can be used and found many. I can
not list them all here. I recommend to open another JIRA for that.
Here is one good example. In MetaTableAccessor.
{code}
public static Put addLocation(final Put p, final ServerName sn, long
openSeqNum, int replicaId){
// using regionserver's local time as the timestamp of Put.
// See: HBASE-11536
long now = EnvironmentEdgeManager.currentTime();
p.addImmutable(HConstants.CATALOG_FAMILY, getServerColumn(replicaId), now,
Bytes.toBytes(sn.getHostAndPort()));
p.addImmutable(HConstants.CATALOG_FAMILY, getStartCodeColumn(replicaId),
now,
Bytes.toBytes(sn.getStartcode()));
p.addImmutable(HConstants.CATALOG_FAMILY, getSeqNumColumn(replicaId), now,
Bytes.toBytes(openSeqNum));
return p;
}
{code}
Can become
{code}
public static Put addLocation(final Put p, final ServerName sn, long
openSeqNum, int replicaId){
byte longBytes = new byte[Bytes.SIZEOF_LONG];
// using regionserver's local time as the timestamp of Put.
// See: HBASE-11536
long now = EnvironmentEdgeManager.currentTime();
p.addImmutable(HConstants.CATALOG_FAMILY, getServerColumn(replicaId), now,
Bytes.toBytes(sn.getHostAndPort());
p.addImmutable(HConstants.CATALOG_FAMILY, getStartCodeColumn(replicaId),
now,
Bytes.toBytes(sn.getStartcode(), longBytes));
p.addImmutable(HConstants.CATALOG_FAMILY, getSeqNumColumn(replicaId), now,
Bytes.toBytes(openSeqNum, longBytes));
return p;
}
{code}
It's not a HUGE saving, it just divides the number of arrays by 2.
Another example in CellCodec
{code}
public void write(Cell cell) throws IOException {
checkFlushed();
// Row
write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
// Column family
write(cell.getFamilyArray(), cell.getFamilyOffset(),
cell.getFamilyLength());
// Qualifier
write(cell.getQualifierArray(), cell.getQualifierOffset(),
cell.getQualifierLength());
// Version
this.out.write(Bytes.toBytes(cell.getTimestamp()));
// Type
this.out.write(cell.getTypeByte());
// Value
write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
// MvccVersion
this.out.write(Bytes.toBytes(cell.getSequenceId()));
}
{code}
can be changed to
{code}
public void write(Cell cell) throws IOException {
byte longBytes = new byte[Bytes.SIZEOF_LONG];
checkFlushed();
// Row
write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
// Column family
write(cell.getFamilyArray(), cell.getFamilyOffset(),
cell.getFamilyLength());
// Qualifier
write(cell.getQualifierArray(), cell.getQualifierOffset(),
cell.getQualifierLength());
// Version
this.out.write(Bytes.toBytes(cell.getTimestamp(), longBytes));
// Type
this.out.write(cell.getTypeByte());
// Value
write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
// MvccVersion
this.out.write(Bytes.toBytes(cell.getSequenceId(), longBytes));
}
{code}
Here again, not a huge benefit, but still divides the number of created arrays
by 2...
So if we add them all, at the end, might still help.
> Bytes.toBytes() methods should allow arrays to be re-used
> ----------------------------------------------------------
>
> Key: HBASE-15233
> URL: https://issues.apache.org/jira/browse/HBASE-15233
> Project: HBase
> Issue Type: Improvement
> Components: API
> Affects Versions: 1.1.3
> Reporter: Jean-Marc Spaggiari
> Assignee: Michael Ernest
> Priority: Minor
> Labels: beginner
>
> Today we have this:
> {code}
> public static byte[] toBytes(long val) {
> byte [] b = new byte[8];
> for (int i = 7; i > 0; i--) {
> b[i] = (byte) val;
> val >>>= 8;
> }
> b[0] = (byte) val;
> return b;
> }
> {code}
> might be nice to also have this:
> {code}
> public static byte[] toBytes(long val, byte[] reuse) {
> for (int i = 7; i > 0; i--) {
> reuse[i] = (byte) val;
> val >>>= 8;
> }
> reuse[0] = (byte) val;
> return reuse;
> }
> {code}
> Same for all the other Bytes.toBytes() methods.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)