Skip to content

perf: coalesce empty DRAM ticks and fix drive() drain accounting - #1

Merged
chenshih1 merged 1 commit into
masterfrom
cursor/des-hotpath-coalesce-cf4c
Aug 27, 2026
Merged

chenshih1 merged 1 commit into
masterfrom
cursor/des-hotpath-coalesce-cf4c

Conversation

@chenshih1

@chenshih1 chenshih1 commented Aug 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

The DES Dram path was spending most of its host time on empty per-cycle Python events. This PR ticks those empty in-flight DRAM cycles in C++ when no other simulator event falls in the window, and fixes MemorySystem.drive() so a second call on the same instance actually drains.

Changes

  • Dram tick coalescing: new C++ run_until_progress ticks until a completion, idle, or max_cycles. Dram._tick uses it when the next pending simulator event is more than one DRAM cycle away, then _advance_to jumps sim.now to the cycle that made progress. Completions remain zero-delay events at that cycle. Other events are never skipped (now + k*period must be strictly before next_time).
  • drive() accounting: completed_count_ is reset at the start of each drive/drive_range. A leftover count from a previous call made issued - completed_count_ negative, skipped the drain loop, and returned with requests still pending.
  • Smaller hot-path allocations: reuse the last READ callback wrapper in _engine / Dram when the user callback object is unchanged; store a (time, priority, seq) key on _Event so heap comparisons do not allocate tuples; Simulator.run() no longer double-peeks.

Simulation semantics (timing, RequestInfo, backpressure, event order vs other components) stay identical. Same-run A/B with coalescing forced off (_max_tick_burst = 1) produced bitwise-identical RequestInfo tuples and the same DRAM cycle counts. sim.processed / event_counts for DRAM-only runs drop because empty cycles are no longer simulator events.

Investigation (what was already tight)

Already in good shape from 0.5.0: C++ run/drive loops, completion batching, send_batch callback reuse, idle DRAM scheduling nothing, idle-refresh exponential backoff, Pipe retry batching, O(1) cancel, LTO, -march=native, PGO script. Ramulator Memory::tick stats and command scheduling cannot be skipped without touching the submodule. Releasing the GIL around ticks is unsafe while callbacks copy py::object, and would not help a single-threaded DES.

Benchmarks (this machine, DDR4-2400R, median of 5)

Path Before (per-cycle Python ticks) After (coalesce)
DES, queue depth 32, per-request callbacks 60.4K req/s, 6.64 events/req 97.1K req/s, 2.0 events/req (1.61×)
DES, serial issue+drain (unloaded) 28.9K req/s, 22.8 events/req 174K req/s, 2.0 events/req (6.0×)
DES kernel, empty events 399K events/s 486K events/s
Engine Python issue loop 142K req/s 148K req/s (noise / callback reuse)
Engine drive() 208K req/s 209K req/s

benchmarks/bench.py DRAM latency/bandwidth numbers match the expected cycle-accurate tables (DDR4-2400 sequential avg 191.4 cyc, qd=32 BW 13.47 GB/s). examples/accel_sim.py runs.

Testing

  • ruff check pyramulator/ tests/ benchmarks/ examples/ passes
  • ruff format --check pyramulator/ tests/ benchmarks/ examples/ passes
  • mypy pyramulator/ passes
  • pytest passes (191 tests, 97% coverage)
  • If the build changed: pip install -e ".[dev]" from a clean checkout works

New tests cover coalescing vs a host event in the middle of a DRAM latency, sim.now vs depart_cycle, distinct callbacks, tick_until_progress, _advance_to guards, and two drive() calls on one instance.

Changelog

Yes — [Unreleased] in CHANGELOG.md.

Open in Web Open in Cursor 

Collapse in-flight DRAM ticks with no other pending simulator events
into one C++ run_until_progress call, then jump sim time to the cycle
that made progress. Completions stay zero-delay events at that cycle.

Also reset MemorySystem.drive() completion credits per call so a second
drive on the same instance actually drains, and reuse request callback
wrappers plus a stored heap key on the DES kernel.

Co-authored-by: chenshih <chenshih1@users.noreply.github.com>
@chenshih1
chenshih1 marked this pull request as ready for review August 27, 2026 11:19
@chenshih1
chenshih1 merged commit 8c3fb50 into master Aug 27, 2026
7 checks passed

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5f724aa. Configure here.

Comment thread pyramulator/dram.py
"""
n = self._mem.tick_until_progress(self._max_tick_burst())
if n > 1:
self.sim._advance_to(self.sim.now + (n - 1) * self.clk.period_ps)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Coalescing jumps past run until

High Severity

_max_tick_burst only clips coalesced DRAM ticks to next_time, so _tick can _advance_to a completion cycle far beyond Simulator.run(until=...). That horizon is not a heap event. An in-flight request can push sim.now past until, drop dram.pending to 0, and leave the completion callback unfired until a later run. Incremental run(until=) windows and the documented idle_refresh + run(until=...) path no longer stop at the requested time.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5f724aa. Configure here.

@chenshih1 chenshih1 mentioned this pull request Aug 27, 2026
6 tasks done
chenshih1 added a commit that referenced this pull request Aug 27, 2026
Cut 0.5.3 from master after PRs #1–#4:
- DRAM empty-cycle coalescing and drive() drain accounting
- idle-refresh backoff reset, drive(batch=0) hang, cancel accounting
- coalescing clipped to Simulator.run(until=)
- Pipe+FIFO+Dram composition example, occupancy, and issue-pipe refill

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: chenshih <chenshih1@users.noreply.github.com>
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.

2 participants