Skip to content

Avoid string allocation of types on statement (and freeze arrays) - #743

Open
jhawthorn wants to merge 2 commits into
sparklemotion:mainfrom
jhawthorn:faster_types
Open

jhawthorn wants to merge 2 commits into
sparklemotion:mainfrom
jhawthorn:faster_types

Conversation

@jhawthorn

@jhawthorn jhawthorn commented Sep 21, 2026

Copy link
Copy Markdown
Member

We can avoid intermediate allocations by doing the downcasing and interning in C.

This also changes the strings to be UTF-8.

We only downcase ASCII here because that's what was previously done (as the strings used to be binary.

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "sqlite3"
require "ips"

db = SQLite3::Database.new(":memory:")
db.execute "CREATE TABLE t (#{20.times.map { |i| "c#{i} varchar(255)" }.join(", ")})"
sql = "SELECT * FROM t"

IPS.run do |x|
  x.report("prepare + types") do
    stmt = SQLite3::Statement.new(db, sql)
    stmt.types
    stmt.close
  end
end
$ ips --yjit --sha main --sha HEAD bench/metadata.rb
ruby 4.0.7 (2026-09-15 revision 229531a6cf) +YJIT +PRISM [arm64-darwin25]
     prepare + types:   217.697k i/s (± 0.7%, GC  1.8%)

ruby 4.0.7 (2026-09-15 revision 229531a6cf) +YJIT +PRISM [arm64-darwin25]
     prepare + types:   157.717k i/s (± 0.6%, GC  9.4%)


Summary
  faster_types ran
    1.38 ± 0.01 times faster than 576f36c

We can avoid intermediate allocations by doing the downcasing and
interning in C.

This also changes the strings to be UTF-8.

We only downcase ASCII here because that's what was previously done (as
the strings used to be binary.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant