Skip to content

Commit

Permalink
common: small optimization for ptr encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Xinze Chi <xinze@xsky.com>
  • Loading branch information
XinzeChi committed Nov 26, 2015
1 parent 091a998 commit 2c8a175
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/include/encoding.h
Expand Up @@ -222,7 +222,7 @@ inline void encode(const buffer::ptr& bp, bufferlist& bl)
__u32 len = bp.length();
encode(len, bl);
if (len)
bl.append(bp);
bl.append(bp, 0, bp.length());
}
inline void decode(buffer::ptr& bp, bufferlist::iterator& p)
{
Expand Down

1 comment on commit 2c8a175

@branch-predictor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this supposed to help with performance? Prevoius code just shared the bufferptr and new code copies (except data), which is both slower and adds work to already overloaded memory allocator.

unsigned long long gettickcount(void) {
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
    return 0;
  }
  return (ts.tv_nsec) + ts.tv_sec * 1000000000;
}

void test_default() {

 bufferlist bl;
 char data[49152];
 bufferptr bp(&data[0], 128);

 unsigned long long ti = gettickcount();
 for (int i=0; i<1000000; i++) {
   bl.clear();
   for (int j=0; j<100; j++) {
     bl.append(bp);
   }
 }

 printf("old ti: %.2f\n", (gettickcount()-ti)/1000000000.0);

}

void test_new() {

 bufferlist bl;
 char data[49152];
 bufferptr bp(&data[0], 128);

 unsigned long long ti = gettickcount();
 for (int i=0; i<1000000; i++) {
   bl.clear();
   for (int j=0; j<100; j++) {
     bl.append(bp, 0, bp.length());
   }
 }

 printf("new ti: %.2f\n", (gettickcount()-ti)/1000000000.0);

}

The above code shows the following on Xeon E5-2420 v2 @ 2.20GHz:

old ti: 3.91
new ti: 5.92

In other words, your "optimization" slows down bufferlist encode by at least around 2us per call.

Please sign in to comment.