Commit fb1ad6cf

fb1ad6cf13ffMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

perf(analyze,codegen): cache parse_id_list empty case too

PR #547 by @apocalypse9949. The existing parse_id_list memoizes non-empty cache hits via @parse_id_cache + @parse_id_pool but short-circuits the empty-string case to a freshly-allocated [] on every call. Bootstrap touches @nd_stmts[nid] etc. for every AST node, many of which carry no children; each empty case allocated a fresh array.

The change extends the existing cache to the empty case: a single shared empty array now serves every caller of parse_id_list(""), mirroring the non-empty path.

Cherry-picked rather than merged because the PR's branch was based on an old master and a direct merge would revert the last ~10 commits worth of fixes. Behavior change is identical to the PR; the .freeze from the PR's version is omitted because spinel-Ruby doesn't implement Array#freeze (CRuby gets the defensive guard; spinel-stage-2 silently treats freeze as a no-op).

make bootstrap clean, make test 513 pass, optcarrot checksum 59662 unchanged.

Co-Authored-By: prudhvi [email protected] Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
fb1ad6cf13ff
Parent
7caf9b6db62e

spinel_analyze.rbmodifiedView file

@@ -597,28 +597,27 @@
597597 # parsed, so the same IntArray can be shared across callers. Callers
598598 # must treat the result as read-only.
599599 def parse_id_list(s)
600- if s == ""
601- return []
602- end
603600 if @parse_id_cache.key?(s)
604601 return @parse_id_pool[@parse_id_cache[s]]
605602 end
606603 result = []
607- bs = s.bytes
608- i = 0
609- n = bs.length
610- num = 0
611- while i < n
612- b = bs[i]
613- if b == 44 # ','
614- result.push(num)
615- num = 0
616- else
617- num = num * 10 + (b - 48)
604+ if s != ""
605+ bs = s.bytes
606+ i = 0
607+ n = bs.length
608+ num = 0
609+ while i < n
610+ b = bs[i]
611+ if b == 44 # ','
612+ result.push(num)
613+ num = 0
614+ else
615+ num = num * 10 + (b - 48)
616+ end
617+ i = i + 1
618618 end
619- i = i + 1
619+ result.push(num)
620620 end
621- result.push(num)
622621 @parse_id_cache[s] = @parse_id_pool.length
623622 @parse_id_pool.push(result)
624623 result