Repository navigation
perf(sdk): make EventLog.append flat against conversation length - #4856
alanhuangyoo wants to merge 1 commit into
Conversation
append() listed the whole events directory on every call, inside the lock, to
notice writes from another process. LocalFileStore.list() stats every entry to
decide whether it is a directory, so appending event N cost N stat calls on the
run loop's critical path, and the cost grew with the conversation.
A marker file records the number of events on disk and append only counts when
the marker cannot prove the count is unchanged. The count is in the filename
rather than the contents because FileStore.read is served from a cache that the
class documents as assuming exclusive access, so it cannot see another writer,
while FileStore.exists goes to the filesystem. Only an exact match with our own
length is treated as proof; a missing marker, a marker for another count, or a
store that cannot answer all fall through to the counting path unchanged, so an
interrupted append costs the next writer a full count rather than letting it
trust a stale one.
3200 appends, LocalFileStore, mean ms per append by bucket:
events before after
400 1.066 0.396
1600 5.701 0.425
3200 12.827 0.385
growth 12.0x 1.0x
total 21.78s 1.30s
Two things found on the way and fixed here because this change exercises them:
LocalFileStore.delete removed the file and then raised KeyError evicting a cache
entry that was never there, which the handler logged as "Error clearing local
file store" even though the delete had succeeded. Any file written by another
process hit this; the marker hits it on every append.
_scan_and_build_index warned "Unrecognized event file name" for the lock file on
every open. Bookkeeping dotfiles now skip that check instead.
|
🚦 CI is currently failing on this PR's latest commit. Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request This is an automated check - no AI was used to generate this comment. |
|
Thank you for this, and I'm sorry to be the bearer of annoying news: this landed a few hours ago in #4697 |
|
Superseded by #4697, which landed while I was working on this. Closing. We converged on the same design — the marker naming the count in its filename because The ordering is wrong in mine. #4697 deletes the old marker before writing the new one, and says why:
Mine writes the new marker first and deletes the old one after, so an interruption between the two leaves both on disk. A writer whose I also went looking for something to still contribute and did not find it. #4697 covers the same two incidental fixes I had — For whatever it is worth to #4671, my measurement agrees with the one in the new benchmark script — 3200 appends over Thanks @VascoSch92. |
HUMAN:
Ran a long conversation against a local file store and watched the per-append time stop climbing; 3200 appends went from 22s to 1.3s.
AGENT:
Why
Closes #4676 (step 1 of #4671).
EventLog.appendlisted the whole events directory on every call, inside the lock, to notice writes from another process:LocalFileStore.list()is worse than a listing. It stats every entry to decide whether to append a trailing/:So appending event N costs N
statcalls on the run loop's critical path, while holding the lock. At 1600 entries the listing is 0.416 ms and the stats take it to 3.031 ms — 86% of the cost is for a decoration the counter does not read.Summary
appendcounts the directory only when a marker file cannot prove the count is unchanged.FileStore.readis served from a cache the class documents as assuming exclusive access, so it cannot see another writer;FileStore.existsgoes to the filesystem. Measured below.LocalFileStore.deleteno longer logs a failure when evicting a cache entry that was never there, and_scan_and_build_indexno longer reports bookkeeping dotfiles as malformed event files.Why this cannot lose a concurrent write
Only an exact match with our own
_lengthis treated as proof. A missing marker, a marker naming a different count, or a store that cannot answer all fall through to_count_events_on_disk()+_sync_from_disk()unchanged, so the authoritative path is exactly what it is today and the new fast path only ever skips work when nothing has changed.The marker is written after the event, inside the lock. An append interrupted anywhere in the critical section therefore leaves no marker at all, which costs the next writer a full count rather than letting it trust a stale one. Over-trusting is not reachable.
How to Test
Full suite:
ruff checkandruff format --checkare clean on the four changed files.The measurement the issue asks for
EventLogoverLocalFileStore, realMessageEvents with generated ids, mean ms per append by bucket:Flat, which is the acceptance criterion. 17x on the total, 33x on the last append.
End-to-end across two real processes
Unit tests share a
FileStoreobject, so they cannot exercise the thing the marker exists for. Parent appends 1, a spawned child process appends 3, parent appends again:The parent's probe correctly fails, it re-counts, and the child's events are picked up.
And the asymmetry the design rests on, on the same store:
A contents-based marker would have been silently wrong across processes.
tests/sdk/io/test_filestore_cache.py::test_exists_sees_an_external_write_but_read_does_notpins that property so the reason for the filename encoding does not get refactored away.Tests
Six cases in
test_event_store.pyand three intest_filestore_cache.py. Four of them fail on the parent commit and pass here:The other two — a second instance still seeing the first's events, and a missing marker falling back to counting — pass either way on purpose: they guard the multi-writer behaviour this change must not cost.
test_marker_names_do_not_prefix_one_anotheris there becauseInMemoryFileStore.deletematches by prefix, so.eventlog-count-1would have deleted.eventlog-count-10. The names carry a.markersuffix so no one is a prefix of another at any count.The two incidental fixes
Both were found by this change and both reproduce on
main:and every
EventLogopen logsUnrecognized event file name: .eventlog.lock.Issue Number
Closes #4676
Type