Title: Make gets/each_line line scan linear (drop O(n²) buffer copy) by tannevaled · Pull Request #1 · go-ruby-stringio/stringio · GitHub
Open Graph Title: Make gets/each_line line scan linear (drop O(n²) buffer copy) by tannevaled · Pull Request #1 · go-ruby-stringio/stringio
X Title: Make gets/each_line line scan linear (drop O(n²) buffer copy) by tannevaled · Pull Request #1 · go-ruby-stringio/stringio
Description: Problem gets / each_line / readline / lines were the one path measured slower than MRI on the library benchmark — 5.69× MRI and ~6× MRI+YJIT on the 256-line docs document. The line core scanned for the separator with strings.Index(string(rest), sep). string(rest) copied the entire remaining buffer to a fresh string on every gets call, so walking N lines copied O(N²) bytes overall (plus an allocation per line). The scan itself only reaches the next separator; the whole-buffer copy was the O(n²) cost. Fix Scan directly on the []byte slice from the cursor — no string(rest) copy, no rescan of consumed bytes: bytes.IndexByte(rest, sep[0]) for the common single-byte separator (the $/ default "\n"), the tight memchr-equivalent path; bytes.Index(rest, []byte(sep)) for multi-byte separators; bytes.Index(rest, []byte("\n\n")) for the paragraph-mode (sep == "") split. The whole line walk is now linear. Semantics are unchanged: the returned line still includes the separator, and lineno / pos / limit / paragraph handling are untouched. read / getc were checked and are already linear — left as is. Evidence (Apple M4 Max, ruby 4.0.5 +PRISM, same docs harness) op before ns/op after ns/op vs MRI vs MRI+YJIT gets 102303 ~7.4k 0.41× 0.44× (beats YJIT) Other ops unregressed (write 0.37×, read 0.54×, getc 0.11×, puts 0.30× vs MRI). MRI oracle test (TestOracleAgainstMRI) passes — byte-identical to MRI 4.0.5. Benchmark SHA-256 byte-identical gate passes for every op. Coverage 100.0% (added a multi-byte-separator gets test to cover the new bytes.Index branch). go test -race clean; gofmt clean. Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com
Open Graph Description: Problem gets / each_line / readline / lines were the one path measured slower than MRI on the library benchmark — 5.69× MRI and ~6× MRI+YJIT on the 256-line docs document. The line core scanned for...
X Description: Problem gets / each_line / readline / lines were the one path measured slower than MRI on the library benchmark — 5.69× MRI and ~6× MRI+YJIT on the 256-line docs document. The line core scanned for...
Opengraph URL: https://github.com/go-ruby-stringio/stringio/pull/1
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:c046ef47-bd4e-bd42-f788-7e397bf40b45 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C5F6:14AEA2:161FFC2:1D49AA4:6B04128D |
| html-safe-nonce | ebc06deb96f5f5275e6241fc4bec44e53aa480a836ba0deabdc6e30d4d5f1ee6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNUY2OjE0QUVBMjoxNjFGRkMyOjFENDlBQTQ6NkIwNDEyOEQiLCJ2aXNpdG9yX2lkIjoiMTQ5MzM5NzUzNTQ4NDgwOTg2OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 92754daf0bf0b3c108a935992fbad78154bc2408af2d8bb2ae2759a22c31c66a |
| hovercard-subject-tag | pull_request:3986995919 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/go-ruby-stringio/stringio/pull/1/files |
| twitter:image | https://avatars.githubusercontent.com/u/11405852?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/11405852?s=400&v=4 |
| og:image:alt | Problem gets / each_line / readline / lines were the one path measured slower than MRI on the library benchmark — 5.69× MRI and ~6× MRI+YJIT on the 256-line docs document. The line core scanned for... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/go-ruby-stringio/stringio git https://github.com/go-ruby-stringio/stringio.git |
| octolytics-dimension-user_id | 297888398 |
| octolytics-dimension-user_login | go-ruby-stringio |
| octolytics-dimension-repository_id | 1284123952 |
| octolytics-dimension-repository_nwo | go-ruby-stringio/stringio |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1284123952 |
| octolytics-dimension-repository_network_root_nwo | go-ruby-stringio/stringio |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width