Commit 7cc1842c
7cc1842cbc5cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
Merge pull request #120 from xl350r/perf/keys-each-fusion
perf: fuse hash.keys.each to avoid per-iteration array allocation
Details
- Commit
- 7cc1842cbc5c
- Parent
- bf136cbb56c2
- Merge parent
- 8ce5c7981550
lib/sp_runtime.hmodifiedView file
@@ -403,7 +403,7 @@
403403
hoisted codepoint count. We don't need it for correctness, but keeping the404404
ABI lets callers pass it without a wrapper. */405405
static const char*sp_str_sub_range_len(const char*s,mrb_int cl,mrb_int start,mrb_int len){if(start<0)start+=cl;if(start<0)start=0;if(start>=cl||len<=0){return &("\xff" "")[1];}if(start+len>cl)len=cl-start;size_t boff=sp_utf8_byte_offset(s,start);size_t bend=sp_utf8_byte_offset(s+boff,len)+boff;size_t blen=bend-boff;if(len==1&&blen==1){unsigned char c=(unsigned char)s[boff];if(!sp_char_cache_init){for(int i=0;i<256;i++){sp_char_cache[i][0]=(char)0xff;sp_char_cache[i][1]=(char)i;sp_char_cache[i][2]=0;}sp_char_cache_init=1;}return &sp_char_cache[c][1];}char*r=sp_str_alloc_raw(blen+1);memcpy(r,s+boff,blen);r[blen]=0;return r;}406-
static const char*sp_sprintf(const char*fmt,...){char*b=sp_str_alloc_raw(4096);va_list ap;va_start(ap,fmt);vsnprintf(b,4096,fmt,ap);va_end(ap);return b;}406+
static const char*sp_sprintf(const char*fmt,...){char _sp_tmp[4096];va_list ap;va_start(ap,fmt);int _sp_n=vsnprintf(_sp_tmp,sizeof(_sp_tmp),fmt,ap);va_end(ap);if(_sp_n<0)_sp_n=0;if(_sp_n>=(int)sizeof(_sp_tmp))_sp_n=(int)sizeof(_sp_tmp)-1;char*b=sp_str_alloc(_sp_n);memcpy(b,_sp_tmp,_sp_n);return b;}407407
/* Use a temp pointer for realloc so the original buffer is not leaked408408
on allocation failure. Match the perror+exit pattern used elsewhere409409
(see sp_IntArray_replace) instead of returning a partial result. */spinel_codegen.rbmodifiedView file
Loading diff…
test/hash_keys_each.rbaddedView file
Loading diff…