Skip to content

Commit 049cf28

Browse files
committed
refactor: optimize scheduler policy lookup with try/except pattern
Replace double dictionary lookup pattern with more efficient try/except approach. This eliminates redundant key existence check and improves performance by reducing dictionary access from two operations to one in the success path. Changes: - Use try/except KeyError instead of 'in' check followed by access - Add 'from None' to suppress exception chaining for cleaner error messages - Maintain identical error handling behavior with ValueError for unknown policies Signed-off-by: zitian.zhao <[email protected]>
1 parent 6875945 commit 049cf28

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

vllm/v1/core/sched/scheduler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ def __init__(
106106
self.requests: dict[str, Request] = {}
107107
# Scheduling policy
108108
policy_name = self.scheduler_config.policy
109-
if policy_name not in self._POLICY_MAPPING:
110-
raise ValueError(f"Unknown scheduling policy: {policy_name}")
111-
self.policy = self._POLICY_MAPPING[policy_name]
109+
try:
110+
self.policy = self._POLICY_MAPPING[policy_name]
111+
except KeyError:
112+
raise ValueError(f"Unknown scheduling policy: {policy_name}") from None
112113
# Priority queues for requests.
113114
self.waiting = create_request_queue(self.policy)
114115
self.running: list[Request] = []

0 commit comments

Comments
 (0)