Commit 81833b44

81833b44c7ebMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #1463 from rubys/fix-string-heap-gc-trigger

Fix string-heap GC leak (#1450): drive collection off string-heap growth

Details

Commit
81833b44c7eb
Parent
96f3227c76a4
Merge parent
f420324356d8

lib/sp_runtime.hmodifiedView file

@@ -367,6 +367,19 @@
367367 /* ---- String GC ---- */
368368 /* __thread: each Ractor owns a private string heap. */
369369 static __thread sp_str_hdr *sp_str_heap = NULL;
370+/* String-heap collection trigger (matz/spinel#1450). Strings live on a separate
371+ malloc'd heap and are reaped only by sp_str_sweep, which runs from
372+ sp_gc_collect -- which only fires on OBJECT-heap pressure (sp_gc_bytes). A
373+ string-only workload (e.g. an HTTP server building responses, or a tight
374+ `s = s + x` loop) thus never triggers a collection and RSS grows without
375+ bound. Drive collection off the string heap's own live-byte count, with a
376+ SEPARATE threshold so string bytes are NOT refolded into sp_gc_bytes (doing
377+ so over-fired the object heuristic -- the reason they were excluded; see the
378+ comment in sp_str_alloc). */
379+static __thread size_t sp_str_heap_bytes = 0; /* live string-heap bytes */
380+static __thread size_t sp_str_threshold = 256*1024; /* mirrors sp_gc_threshold init */
381+static __thread size_t sp_str_threshold_init = 256*1024; /* recompute floor; lowered to 2048 under SPINEL_GC_STRESS so stress mode survives a collection (mirrors sp_gc_threshold_init) */
382+static __thread int sp_str_stress_checked = 0;
370383 #define SPL(s) (&("\xff" s)[1])
371384 static const char sp_str_empty_data[] = "\xff";
372385 #define sp_str_empty (sp_str_empty_data + 1)
@@ -428,6 +441,21 @@
428441
429442 static char *sp_str_alloc(size_t len) {
430443 size_t total = sizeof(sp_str_hdr) + 1 + len + 1;
444+ /* String-heap pressure drives its own collection (see sp_str_heap_bytes).
445+ Collect BEFORE the new allocation, like sp_gc_alloc, so the string being
446+ built isn't yet live during the sweep. Operands of the calling op (e.g. the
447+ arguments to sp_str_concat) must be reachable across this point -- they are
448+ for rooted locals; the codegen's SP_GC_ROOT discipline is what keeps them
449+ so. Threshold recompute mirrors sp_gc_alloc's. */
450+ if (!sp_str_stress_checked) { sp_str_stress_checked = 1; const char *e = getenv("SPINEL_GC_STRESS"); if (e && *e && *e != '0') { sp_str_threshold = 2048; sp_str_threshold_init = 2048; } }
451+ if (sp_str_heap_bytes > sp_str_threshold) {
452+ size_t before = sp_str_heap_bytes;
453+ sp_gc_collect(); /* runs sp_str_sweep, which decrements sp_str_heap_bytes */
454+ size_t freed = before - sp_str_heap_bytes;
455+ if (freed < before/4) sp_str_threshold = before*2;
456+ else if (sp_str_heap_bytes > 0) { sp_str_threshold = sp_str_heap_bytes*4; if (sp_str_threshold < sp_str_threshold_init) sp_str_threshold = sp_str_threshold_init; }
457+ else sp_str_threshold = sp_str_threshold_init;
458+ }
431459 sp_str_hdr *h = (sp_str_hdr *)malloc(total);
432460 if (!h) sp_oom_die();
433461 h->next = sp_str_heap;
@@ -435,6 +463,7 @@
435463 h->len = (uint32_t)len;
436464 h->hash = 0;
437465 sp_str_heap = h;
466+ sp_str_heap_bytes += total;
438467 /* Don't fold string-heap pressure into sp_gc_bytes : the
439468 threshold heuristic in sp_gc_alloc is keyed on heap survivors, and
440469 the str-heap mark-sweep that runs alongside (sp_str_sweep,
@@ -674,8 +703,7 @@
674703 }
675704 else {
676705 *pp = h->next;
677- /* String allocs no longer fold into sp_gc_bytes (see
678- sp_str_alloc); the matching subtract here drops too. */
706+ sp_str_heap_bytes -= h->size; /* keep the string-heap live-byte count in step */
679707 free(h);
680708 }
681709 }
@@ -689,6 +717,7 @@
689717 sp_str_hdr *h = sp_str_heap;
690718 while (h) { sp_str_hdr *n = h->next; free(h); h = n; }
691719 sp_str_heap = NULL;
720+ sp_str_heap_bytes = 0;
692721 sp_str_lcache_clear();
693722 }
694723
@@ -1824,7 +1853,10 @@
18241853 free(buf); free(fcps); free(tcps);
18251854 return r;
18261855 }
1827-static const char*sp_str_delete(const char*s,const char*chars){if(!s)return sp_str_empty;if(!chars)return s;int negate=0;const char*csp=chars;if(*csp=='^'&&*(csp+1)){negate=1;csp++;}size_t setn;uint32_t*set=sp_utf8_decode_charset(csp,&setn);size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=sp_utf8_set_has(set,setn,cp);if(negate)in_set=!in_set;if(!in_set){memcpy(r+n,p,cn);n+=cn;}p+=cn;}r[n]=0;sp_str_set_len(r,n);free(set);return r;}
1856+/* Build into a malloc temp and read all of `s` BEFORE the result sp_str_alloc:
1857+ that allocation can now trigger a string-heap collection, which would sweep
1858+ an unrooted `s` mid-copy (the read-first pattern sp_str_tr/sp_str_format use). */
1859+static const char*sp_str_delete(const char*s,const char*chars){if(!s)return sp_str_empty;if(!chars)return s;int negate=0;const char*csp=chars;if(*csp=='^'&&*(csp+1)){negate=1;csp++;}size_t setn;uint32_t*set=sp_utf8_decode_charset(csp,&setn);size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);if(!buf)sp_oom_die();size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=sp_utf8_set_has(set,setn,cp);if(negate)in_set=!in_set;if(!in_set){memcpy(buf+n,p,cn);n+=cn;}p+=cn;}buf[n]=0;free(set);char*r=sp_str_alloc(n);memcpy(r,buf,n+1);free(buf);return r;}
18281860 /* Issue #921: shrink the heap-string header length to match the
18291861 squeezed payload — the alloc gives bl+1 bytes, the squeezed
18301862 write fills n<=bl, leaving the header's stored length stale.
@@ -1838,7 +1870,7 @@
18381870 /* Multi-arg delete/squeeze: delete (or squeeze runs of) characters that
18391871 are in the INTERSECTION of all n charset args, mirroring
18401872 sp_str_count_n. Each arg is a charset spec (^negation, a-z ranges). */
1841-static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}
1873+static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);if(!buf)sp_oom_die();size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(buf+m,p,cn);m+=cn;}p+=cn;}buf[m]=0;for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);char*r=sp_str_alloc(m);memcpy(r,buf,m+1);free(buf);return r;}
18421874 static const char*sp_str_squeeze_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return sp_str_squeeze(s);size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;prev=0xFFFFFFFFu;}else if(cp!=prev){memcpy(r+m,p,cn);m+=cn;prev=cp;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}
18431875
18441876 /* Forward decl from sp_crypto.h (libspinel_rt.a). Used by