Skip to content

runtime: make the two calls around each drop cheaper - #4598

Open
hiraditya wants to merge 1 commit into
mainfrom
runtime-drop-fast-path
Open

hiraditya wants to merge 1 commit into
mainfrom
runtime-drop-fast-path

Conversation

@hiraditya

@hiraditya hiraditya commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #4583.

What changes

vxc calls vx_cancel_off before each call of a Drop impl's drop, and vx_cancel_on after it. They give the drop its deadline (F6a in docs/discussions/waiting_tasks_channels_and_task_errors.md). With a release runtime, these two calls took more than half of the time of the solver written in Vx. It drops BigInt and Rational values in its inner loops. Each drop of a Vec, String, Box or Arc makes the same two calls.

Three changes in stdlib/rust_core/src/ffi/tasks.rs and wait.rs make them cheaper. A drop gets the same deadline as before.

  • One thread-local value. The current mark and the scopes with a deadline are one value, MARKS. Each call reads the state of the thread once, not up to five times.
  • The outermost drop. Take a thread with no tasks block and no other scope. There, the outermost drop keeps its scope in OUTER_DROP, a small thread-local value with no destructor. A drop inside it that shares its scope only adds 1 to a count. Each other call goes through marks(), which moves that scope into MARKS first.
  • The clock. That drop keeps the raw reading of the coarse clock. Only a drop that needs its deadline turns the reading into nanoseconds. On macOS, the runtime reads the ratio of clock ticks to nanoseconds once. clock_gettime_nsec_np asked for it at each call.

Measurements

The solver written in Vx, on 340 queries:

A program built with vxc -O2 from run_corpus.vx runs them all, one after the other. I linked it 5 times, each time with a different runtime built with cargo build --release. The answers are the same in each run.

Shared by all runs
Machine Apple M4, 32 GB, macOS 27.0.1, idle, caffeinate -dims
Solver std::algo::smt of main at 414bb29, compiled with vxc -O2
Tools rustc 1.95.0, clang 22.1.4
Runs 5 rounds. Each round runs each runtime once, in a rotated order, with 2 s between runs.

Total time of each group, in ms: the median of 5 runs, with the fastest and the slowest run.

Runtime 4 if-chains 45 linear 291 nonlinear All 340
main 834 (820, 840) 20.8 (19.6, 21.3) 3003 (2993, 3026) 3855 (3846, 3885)
This PR 459 (456, 477) 11.7 (11.7, 13.4) 1688 (1673, 1697) 2156 (2143, 2187)
vx_cancel_off and vx_cancel_on empty 361 (357, 369) 9.6 (9.4, 11.2) 1366 (1334, 1430) 1733 (1705, 1810)

The last row is a floor: it keeps the calls, but they do nothing. Each of the 340 queries is faster with this PR. Per query, main takes this long, in times the time with this PR:

Group Geometric mean Smallest Largest
4 if-chains 1.80 1.77 1.84
45 linear 1.53 1.31 1.83
291 nonlinear 1.74 1.58 1.85

The 4 slowest nonlinear queries are from llama2.vx. Each took about 204 ms with main, and takes about 113 ms with this PR.

Where the gain comes from

Each row adds one change to the row above it. The first two rows with a change keep the clock code of main.

Runtime All 340, ms Gain, ms
main 3855
One thread-local value 3430 425
and the outermost drop 2580 850
and the raw reading of the clock (this PR) 2156 424

Limits

Commands

vxc -O2 run_corpus.vx --action emit-obj -o run_corpus.o
clang run_corpus.o -o run_<runtime> -Wl,-rpath,<runtime>/target/release <llvm>/libmlir_c_runner_utils.dylib <llvm>/libmlir_runner_utils.dylib <runtime>/target/release/libvx_std_core.dylib
VX_SMT_LIST=queries.txt ./run_<runtime>

Tests

  • tasks.rs gets three unit tests of OUTER_DROP. Drops in it leave MARKS empty. A read of the deadline moves the scope into MARKS. A drop in a tasks block puts its mark aside in MARKS.
  • a_wait_in_a_drop_can_take_up_to_its_time_limit.vx is new. A drop sleeps for 0.3 s, and sleep must give true: the whole time passed. It drops a value in the main code, in another drop, and in a task. The tests before it checked only that a drop gives up at its deadline.

I broke the code in 11 ways, one at a time, and each made a test fail:

Break Test that failed
No drop uses OUTER_DROP the three new unit tests
marks() does not move the scope of OUTER_DROP 6 unit tests of the scopes
The moved scope has one inner drop too many 5 unit tests of the scopes
A drop uses OUTER_DROP also with a mark current the test binary stops at the assertion "a tasks block in a drop did not end"
OUTER_DROP is not used again after the first call of marks() 2 of the new unit tests
The deadline of OUTER_DROP has no lag of the coarse clock the_deadline_of_a_drop_is_its_limit_from_now_or_a_little_later
A drop with a shorter limit shares the scope of OUTER_DROP a_drop_in_a_drop_with_a_shorter_limit_gets_an_earlier_deadline
vx_cancel_on does not count down OUTER_DROP drops_in_the_outermost_drop_share_its_scope_with_no_scope_in_marks
The precise clock gives ticks, not nanoseconds the_coarse_clock_is_never_ahead_and_never_behind_by_more_than_its_lag and the deadline test
The deadline of OUTER_DROP has no limit the new .vx test, in the main code and in a drop in a drop, in each of its 3 RUN lines
A scope in MARKS has no limit the new .vx test, in the task, in each of its 3 RUN lines

With no limit on the deadline of OUTER_DROP, the 6 older tests of drops that wait in tests/backend/pass/ still passed. They check only that a drop gives up.

vxc calls vx_cancel_off before each call of a Drop impl's drop, and
vx_cancel_on after it. With a release runtime, these two calls took more
than half of the time of the solver written in Vx. It drops BigInt and
Rational values in its inner loops.

Three changes make them cheaper. A drop gets the same deadline as
before.

- The current mark and the scopes with a deadline are one thread-local
  value. So each call reads the state of the thread once, not up to
  five times.
- The outermost drop on a thread with no tasks block and no other scope
  keeps its scope in a small thread-local value with no destructor. A
  drop inside it that shares its scope only adds 1 to a count. Each
  other call moves the scope into the full state first.
- That drop keeps the raw reading of the coarse clock. Only a drop that
  needs its deadline turns the reading into nanoseconds. On macOS, the
  runtime reads the ratio of clock ticks to nanoseconds once.
  clock_gettime_nsec_np asked for it at each call.

The new test a_wait_in_a_drop_can_take_up_to_its_time_limit.vx checks
that a drop can wait until its deadline. The tests before it checked
only that a drop gives up at its deadline.

Part of #4583

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