[ 
https://issues.apache.org/jira/browse/HBASE-19684?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16309055#comment-16309055
 ] 

Mike Drob commented on HBASE-19684:
-----------------------------------

FWIW, looks like StringBuilder may be the most performant approach.

{noformat}
Benchmark                        Mode  Cnt        Score        Error  Units
MyBenchmark.testConcatMethod    thrpt  200  8531013.864 ± 125796.993  ops/s
MyBenchmark.testConcatOperator  thrpt  200  7168184.764 ± 209635.694  ops/s
MyBenchmark.testStringBuilder   thrpt  200  9801662.430 ± 185811.763  ops/s
MyBenchmark.testStringFormat    thrpt  200  1021658.557 ±  13366.938  ops/s
{noformat}

{code}
public class MyBenchmark {

    @State(Scope.Thread)
    public static class Data {
        Random r = new Random();
        String a;
        Long b;

        @Setup(Level.Trial)
        public void setup() {
            a = Long.toHexString(r.nextLong());
            b = r.nextLong();
        }
    }

    @Benchmark
    public String testStringFormat(Data data) {
        return (String.format("%s_%d", data.a, data.b));
    }

    @Benchmark
    public String testConcatMethod(Data data) {
        return (data.a.concat("_").concat(Long.toString(data.b)));
    }

    @Benchmark
    public String testStringBuilder(Data data) {
        return (new StringBuilder(data.a.length() + 
21).append(data.a).append('_').append(data.b).toString());
    }

    @Benchmark
    public String testConcatOperator(Data data) {
        return data.a + '_' + data.b;
    }
}
{code}



> BlockCacheKey toString Performance
> ----------------------------------
>
>                 Key: HBASE-19684
>                 URL: https://issues.apache.org/jira/browse/HBASE-19684
>             Project: HBase
>          Issue Type: Improvement
>          Components: hbase
>    Affects Versions: 3.0.0
>            Reporter: BELUGA BEHR
>            Assignee: BELUGA BEHR
>            Priority: Trivial
>         Attachments: HBASE-19684.1.patch, HBASE-19684.2.patch
>
>
> {code:titile=BlockCacheKey.java}
>   @Override
>   public String toString() {
>     return String.format("%s_%d", hfileName, offset);
>   }
> {code}
> I found through bench-marking that the following code is 10x faster.
> {code:titi\le=BlockCacheKey.java}
>   @Override
>   public String toString() {
>     return hfileName.concat("_").concat(Long.toString(offset));
>   }
> {code}
> Normally it wouldn't matter for a _toString()_ method, but this is comes into 
> play because {{MemcachedBlockCache}} uses it.
> {code:title=MemcachedBlockCache.java}
>   @Override
>   public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) {
>     if (buf instanceof HFileBlock) {
>       client.add(cacheKey.toString(), MAX_SIZE, (HFileBlock) buf, tc);
>     } else {
>       if (LOG.isDebugEnabled()) {
>         LOG.debug("MemcachedBlockCache can not cache Cacheable's of type "
>             + buf.getClass().toString());
>       }
>     }
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to