Skip to content

Commit dd6090e

Browse files
committed
speed up string decoding
This speeds up string decoding by using `rb_str_substr` rather than `rb_str_new`. `rb_str_substr` will create shared strings, so it will avoid string copying like `rb_str_new` will do. Before this patch: ``` [aaron@TC bert (master)]$ ruby -I lib:test bench/decode_bench.rb user system total real BERT C Extension Decoder BERT tiny 0.000000 0.000000 0.000000 ( 0.000574) BERT small 0.010000 0.010000 0.020000 ( 0.014938) BERT large 13.990000 11.640000 25.630000 ( 25.892584) BERT complex 0.030000 0.010000 0.040000 ( 0.033596) ``` After this patch: ``` [aaron@TC bert (master)]$ ruby -I lib:test bench/decode_bench.rb user system total real BERT C Extension Decoder BERT tiny 0.000000 0.000000 0.000000 ( 0.000563) BERT small 0.010000 0.000000 0.010000 ( 0.008701) BERT large 6.180000 0.040000 6.220000 ( 6.299307) BERT complex 0.060000 0.000000 0.060000 ( 0.070287) ```
1 parent 7ce2909 commit dd6090e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

ext/bert/c/decode.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ static VALUE rb_cTuple;
3434

3535
struct bert_buf {
3636
const uint8_t *data;
37+
const uint8_t *start;
3738
const uint8_t *end;
39+
VALUE rb_buf;
3840
};
3941

4042
static VALUE bert_read_invalid(struct bert_buf *buf);
@@ -300,7 +302,7 @@ static VALUE bert_read_bin(struct bert_buf *buf)
300302
length = bert_buf_read32(buf);
301303

302304
bert_buf_ensure(buf, length);
303-
rb_bin = rb_str_new((char *)buf->data, length);
305+
rb_bin = rb_str_substr(buf->rb_buf, buf->data - buf->start, length);
304306
buf->data += length;
305307

306308
return rb_bin;
@@ -512,6 +514,8 @@ static VALUE rb_bert_decode(VALUE klass, VALUE rb_string)
512514

513515
Check_Type(rb_string, T_STRING);
514516
buf.data = (uint8_t *)RSTRING_PTR(rb_string);
517+
buf.start = buf.data;
518+
buf.rb_buf = rb_string;
515519
buf.end = buf.data + RSTRING_LEN(rb_string);
516520

517521
bert_buf_ensure(&buf, 1);

0 commit comments

Comments
 (0)