Commit 8433e129
8433e129a114Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Read each_line into a reusable stack buffer
f.each_line { |line| ... } emitted sp_File_gets, which allocates and
copies a fresh GC string for every line. In a line-oriented loop this
allocation dominates: the word-count benchmark spent most of its time here.
Legacy reads into a stack buffer with fgets and lets the body copy what it
needs.
Add sp_File_gets_buf (fgets into a caller-provided buffer, no allocation) and emit a per-loop stack buffer for each_line. The line pointer is valid only within the iteration; this matches legacy, which assumes the line does not escape the loop body.
bm_io_wordcount: 1.34x -> 0.84x vs legacy (now faster), output matches. make test 845, make bench 57, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 8433e129a114
- Parent
- 3f8b35a1c1d5
lib/sp_runtime.hmodifiedView file
memcpy(r, buf, n); return r;}/* Read a line into a caller-provided buffer without allocating. The returned pointer is `buf` (or NULL at EOF); valid only until the next call. Used by each_line loops where the line does not escape the loop body. */static inline const char *sp_File_gets_buf(sp_File *f, char *buf, int size) { if (!f || !f->fp) return NULL; if (!fgets(buf, size, f->fp)) return NULL; return buf;}static inline const char *sp_File_read(sp_File *f) { if (!f || !f->fp) return sp_str_empty; long pos = ftell(f->fp);src/codegen_call.cmodifiedView file
Loading diff…