Repository navigation
perf: small tail-latency cleanups (log fflush, date lock, epoll timer rounding) - #382
Merged
Merged
Conversation
When the opt-in background writer is not started, every log line in the sync fallback forced a stdio-lock + write syscall with fflush. Keep the one-line-per-fwrite granularity but let stdio batch lines (block-buffered on pipes/files, line-buffered on a TTY); the documented interleaving contract is about write granularity, not delivery latency. Document the batching in the logger docblock and the middleware guide (en + ko) and name cwist_log_async_start() as the immediate-delivery path. Access-log middleware behavior is unchanged (it never flushed per line).
The cached IMF-fixdate string took a global mutex at every 1-second boundary, so each second rollover contended every worker on one lock once. Cache the (second, string) pair per thread like the logger's stamp pattern: on a boundary miss each worker recomputes its own copy once; concurrent one-time recomputes yield the same value. g_date_lock has no other user.
The epoll branch always rounded the timer wait UP, letting a timer land up to ~1 ms late. Round to nearest with a 1 ms minimum (absorbing the sub-ms remainder instead of every wait) — an early epoll_wait return cannot dispatch a timer early because reactor_run_timers() re-checks every deadline against the clock, and the only armed timers (webrtc conn/SCTP ticks) re-check their own expiry or are harmless when ticked slightly early. An already-due timer now waits at most 1 ms.
Member
Author
|
although the comment looks slop it's valid. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three independent micro-fixes from the tail-latency audit, one commit each (revertible individually):
perf(log)— the sync log fallback (background writer not started) no longerfflushes per line: one stdio-lock + write syscall per logged request drops off the request path. Write granularity stays one line perfwrite; stdio batches delivery (block-buffered on pipes/files, TTY stays line-buffered).cwist_log_async_start()is the documented immediate-delivery path. Access-log middleware unchanged (it never flushed per line).perf(http)— the cachedDateheader drops its global mutex: per-thread(second, string)cache, so a second rollover costs each worker one local recompute instead of a cross-worker lock acquisition.g_date_lockhad no other user.perf(reactor)— the epoll branch rounds the timer wait to the nearest ms (1 ms floor) instead of always up, cutting worst-case timer lateness from ~1 ms to ~0.6 ms. Shipped only after verifying early dispatch is impossible:reactor_run_timers()re-checks every deadline against the clock, and the only armed timers (WebRTC conn/SCTP ticks) re-check their own expiry or are harmless when ticked early.Test plan
test_log,test_log_async,test_logger_config,test_access_log,test_http_stringify(Date wire format),test_http,test_http_fairness,test_reactor_wake,test_reactor_timer— default andCWIST_REACTOR_BACKEND=epollforced, all passDateheader live-verified over curl