Skip to content

Commit 6875945

Browse files
committed
optimize: improve scheduler policy lookup performance
Replace if-elif chain with dictionary lookup for scheduling policy determination. This change eliminates repeated string comparisons during scheduler initialization and improves code maintainability by centralizing policy mapping logic. Changes: - Add _POLICY_MAPPING class attribute for O(1) policy lookup - Replace conditional chain with single dictionary access - Maintain identical error handling for unknown policies Performance impact: Reduces scheduler initialization overhead, especially beneficial in multi-engine scenarios. Signed-off-by: zitian.zhao <[email protected]>
1 parent 2d18256 commit 6875945

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

vllm/v1/core/sched/scheduler.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939

4040

4141
class Scheduler(SchedulerInterface):
42+
43+
# Policy mapping for efficient policy lookup
44+
_POLICY_MAPPING = {
45+
"priority": SchedulingPolicy.PRIORITY,
46+
"fcfs": SchedulingPolicy.FCFS,
47+
}
4248

4349
def __init__(
4450
self,
@@ -99,13 +105,10 @@ def __init__(
99105
# req_id -> Request
100106
self.requests: dict[str, Request] = {}
101107
# Scheduling policy
102-
if self.scheduler_config.policy == "priority":
103-
self.policy = SchedulingPolicy.PRIORITY
104-
elif self.scheduler_config.policy == "fcfs":
105-
self.policy = SchedulingPolicy.FCFS
106-
else:
107-
raise ValueError(
108-
f"Unknown scheduling policy: {self.scheduler_config.policy}")
108+
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]
109112
# Priority queues for requests.
110113
self.waiting = create_request_queue(self.policy)
111114
self.running: list[Request] = []

0 commit comments

Comments
 (0)