[Serve] Eliminate per-replica-per-tick Pydantic rebuild in update_act…#60833
Open
abrarsheikh wants to merge 2 commits intomasterfrom
Open
[Serve] Eliminate per-replica-per-tick Pydantic rebuild in update_act…#60833abrarsheikh wants to merge 2 commits intomasterfrom
abrarsheikh wants to merge 2 commits intomasterfrom
Conversation
…or_details Signed-off-by: abrar <abrar@anyscale.com>
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a significant performance optimization to DeploymentReplica.update_actor_details(). By adding an early-exit guard for no-op updates and switching from .dict() reconstruction to Pydantic's more efficient .copy(update=...) method, it greatly reduces overhead in a frequently called function. The changes are well-reasoned and supported by comprehensive benchmarks. My review includes one suggestion to further improve the robustness of the early-exit check by using a sentinel object to handle invalid keyword arguments more consistently.
Signed-off-by: abrar <abrar@anyscale.com>
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.
DeploymentReplica.update_actor_details()is called on every replica on every controller tick due to the pop-iterate-readd pattern inReplicaStateContainer.add(). The old implementation ran a full.dict()serialization followed byReplicaDetails(**kwargs)validation on every call — even when nothing changed (e.g., a RUNNING replica re-added as RUNNING). With Pydantic v1 compat this is especially expensive (~20 µs/replica).This PR makes two changes:
getattrcomparisons..copy(update=...)instead of.dict()+ reconstruct: When an update is needed, use Pydantic's.copy(update=kwargs)which creates a shallow copy without full serialization or re-validation.Benchmark results (real
ReplicaDetailsPydantic model, 16 → 4096 replicas)Benchmark Script - AI
At 4,096 replicas the steady-state tick cost drops from 85 ms → 2.6 ms (same-state readd, the dominant path). Even real state transitions drop from 83 ms → 10.7 ms.
Test plan
test_deployment_state.py)Related to #60680