Commit b8d6b03
Vineeth Rao Kanaparthi
subscriber: skip RwLock in EnvFilter span callbacks when no dynamic directives
EnvFilter maintains `by_id: RwLock<HashMap<Id, SpanMatcher>>` and
`by_cs: RwLock<HashMap<Identifier, CallsiteMatcher>>` for tracking
per-span field-level match state. These maps are only populated when
`has_dynamics` is true (i.e., when the filter has span/field-level
directives like `my_crate[span_name]=debug`).
However, the span lifecycle callbacks (`on_new_span`, `on_enter`,
`on_exit`, `on_close`, `on_record`) unconditionally acquire the
`by_id` read lock (or write lock for `on_close`) on every call. When
`has_dynamics` is false — which is the common case for pure
target=level configurations like `RUST_LOG=info,hyper=warn` — these
maps are always empty, and the lock acquisition is wasted work.
Under high concurrency this becomes a bottleneck: a GDB backtrace of a
Tokio-based HTTP server under load showed ~25 worker threads contending
on these RwLock acquisitions, with threads in `read_contended` blocked
by `on_close`'s write lock. The contention scales with the number of
EnvFilter instances in the subscriber layer stack (one per layer when
using per-layer filtering).
This commit adds an early `if !self.has_dynamics { return; }` guard to
each of the five span lifecycle methods. This is a pure no-op when
dynamic directives are present (the existing code path runs unchanged),
and completely eliminates the lock traffic for the common static-only
configuration.1 parent 44696e3 commit b8d6b03
1 file changed
+15
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
546 | 546 | | |
547 | 547 | | |
548 | 548 | | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
549 | 552 | | |
550 | 553 | | |
551 | 554 | | |
| |||
559 | 562 | | |
560 | 563 | | |
561 | 564 | | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
562 | 568 | | |
563 | 569 | | |
564 | 570 | | |
| |||
573 | 579 | | |
574 | 580 | | |
575 | 581 | | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
576 | 585 | | |
577 | 586 | | |
578 | 587 | | |
| |||
584 | 593 | | |
585 | 594 | | |
586 | 595 | | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
587 | 599 | | |
588 | 600 | | |
589 | 601 | | |
| |||
600 | 612 | | |
601 | 613 | | |
602 | 614 | | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
603 | 618 | | |
604 | 619 | | |
605 | 620 | | |
| |||
0 commit comments