Repository navigation
C API: rb_thread_current and rb_thread_local_aref look no method up - #314
Conversation
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
left a comment
There was a problem hiding this comment.
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.
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:executereset!; stepselect_value, logger offWhere the time goes
The largest part is not the C API. It is the interpreter's linear walk of the instance-variable table.
String#-@,dupandcloneof 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'shiv_made).After activerecord loads, the table is large:
-("c" + "ol")"abc".dup{}.dupsqlite3's C sends
-@for each column name, so this walk is 36% of the execute loop insample. 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.currentandThread#[]around every prepare and step, through its callback frame. Each of those was a send by name back into the interpreter. Counted with lldb atmr_call, per operation:Change
rb_thread_currentandrb_thread_local_arefnow look no method up, as in CRuby.MR_THREAD) answers each from the table thatThread.currentandThread#[]read.fb_tl_owner).#[]as before and gets the same value. It answers 0 when:MERE_RUBY_CAPI_WITHOUT=builtinsends both by name, as before.thread_currentandthread_aref, with contracts that:Thread.current, also inside another thread;Thread#[]=wrote: nil when nothing was written, false when false was written;#[]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:reset!; stepexecuteselect_valueSubtracting two whole runs and dividing by n did not work here. activerecord's load varies by more than the whole loop costs.
Checks:
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.The record is
loop/2026-11-26-capi-ar-sql.md.Left
-@/dupwalk: 0e's 2b'.capi_check --vmonce both PRs are in.🤖 Generated with Claude Code