Skip to content

C API: rb_thread_current and rb_thread_local_aref look no method up - #314

Merged
284km merged 1 commit into
mainfrom
capi-ar-sql
Nov 26, 2026
Merged

284km merged 1 commit into
mainfrom
capi-ar-sql

Conversation

@284km

@284km 284km commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Summary

This measures where activerecord's SQL time goes on main. 0e found a statement at about 100x ruby's cost even after #284. The PR takes the part that belongs to the C API and records the rest.

On main, per select 1:

operation mere-ruby ruby 4.0.10
raw execute 0.589 ms 0.0027 ms
prepared reset!; step 0.0136 ms 0.0002 ms
select_value, logger off 3.30 ms 0.008 ms

Where the time goes

The largest part is not the C API. It is the interpreter's linear walk of the instance-variable table.

  • String#-@, dup and clone of a String, Array or Hash walk the whole table. This starts once any String, Array or Hash has ever had an instance variable (m_state.mere's hiv_made).

  • After activerecord loads, the table is large:

    mere-ruby ruby
    -("c" + "ol") 0.317 ms 0.0007 ms
    "abc".dup 0.398 ms 0.0001 ms
    {}.dup 0.476 ms 0.0002 ms
  • sqlite3's C sends -@ for each column name, so this walk is 36% of the execute loop in sample. In the select_value loop it is 22%.

  • This PR does not touch it. That storage is being rebuilt in note 559's 2b, and 0e takes the walk as "2b'". 0e will measure these loops before and after.

The C API's part. sqlite3 asks for Thread.current and Thread#[] around every prepare and step, through its callback frame. Each of those was a send by name back into the interpreter. Counted with lldb at mr_call, per operation:

  • execute and select_value: 3 of each.
  • step: 1 of each.

Change

rb_thread_current and rb_thread_local_aref now look no method up, as in CRuby.

  • Request 37 (MR_THREAD) answers each from the table that Thread.current and Thread#[] read.
    • Inside a Fiber, the thread-local read is the Fiber's own (fb_tl_owner).
  • Fallback. If the request answers 0, C sends #[] as before and gets the same value. It answers 0 when:
    • the receiver is not a Thread, or
    • the value is false.
  • MERE_RUBY_CAPI_WITHOUT=builtin sends both by name, as before.
  • capi/check gets thread_current and thread_aref, with contracts that:
    • C's current thread is Thread.current, also inside another thread;
    • a thread-local reads what Thread#[]= wrote: nil when nothing was written, false when false was written;
    • inside a Fiber it reads the Fiber's own value, and the thread's value again after the Fiber;
    • it reads another thread's value from that thread;
    • a Thread subclass's #[] is not what C reads.

Checked

main 6a46f64, mere 9dec2ff6 for both builds, ruby 4.0.10.

Instructions. Only the loop is counted: instructions retired are read inside the process before and after the loop, with a small C extension built for each side that calls proc_pid_rusage. GC is disabled during the loop. Base and new builds alternate, median of 3:

per operation main this PR ruby
prepared reset!; step 161,875 115,412 (−28.7%) 2,880
raw execute 6,792,954 6,644,317 (−2.2%) 33,522
select_value 23,081,784 22,947,805 (−0.6%) 82,168

Subtracting two whole runs and dividing by n did not work here. activerecord's load varies by more than the whole loop costs.

Checks:

  • capi_check:
    • api 412 (7 new), with both sides agreeing;
    • the same under collection stress;
    • each negative control fails.
  • run_corpus.sh --both: the gates, then 962/962 with the hash index on and off.
  • MERE_RUBY_VM=1 run_corpus.sh --both: 962/962.
  • capi_spec_check: 2420 of 2602, the recorded count.
  • capi_mkmf_check: passes.
  • capi_gem_check: seventeen gems, sqlite3 among them, give the same output on both sides.
  • headers_check: passes.

The record is loop/2026-11-26-capi-ar-sql.md.

Left

🤖 Generated with Claude Code

sqlite3 asks both around every prepare and step (its callback frame), and
each was a send by name back into the interpreter. Request 37 (MR_THREAD)
answers them from the tables Thread.current and Thread#[] read, as CRuby
looks no method up; 0 (a value of false, or not a Thread) sends #[] as
before. A prepared statement's step: 161,875 -> 115,412 instructions.

The record also has where activerecord's SQL time goes: most of it is the
String#-@ / dup walk of the instance-variable table (hiv_made), which is
0e's 2b'.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1133dbf. No Critical / Major / Minor findings. rb_thread_current and rb_thread_local_aref are now answered by up-call 37 (MR_THREAD) without a method lookup, as in CRuby (neither looks up a method there). rb_thread_current reads the current Thread from the same table Thread.current uses. rb_thread_local_aref reads the fiber-local through fb_tl_owner, as Thread#[] does. A value it cannot answer (not a Thread, a non-Symbol key) or a false value returns 0, and C falls back to sending #[], so the answers are the same. The 7 new Thread contracts in api.rb cover this. The loop note gives the breakdown other sessions will use: the -@ / dup scan is 36% of execute and 22% of select_value, and it goes to 0e's 2b'. Instructions (in-process loop, GC off, median of 3 alternating): step −28.7%, execute −2.2%, select_value −0.6%. The description has capi_check (api 412, stress, negative controls), corpus --both 962/962 and MERE_RUBY_VM=1 962/962, spec, mkmf, the 17 gems including sqlite3, and headers. The VM run of these contracts follows #313. git merge-tree against main: clean. No mere-pr-review-watch inline comments. Merging.

@284km
284km merged commit d93de08 into main Nov 26, 2026
4 of 19 checks passed
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