From 68759451be8145d0a39a35d63346e6e47f70156f Mon Sep 17 00:00:00 2001 From: "zitian.zhao" Date: Sun, 10 Aug 2025 00:16:31 +0800 Subject: [PATCH 1/3] 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 --- vllm/v1/core/sched/scheduler.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/vllm/v1/core/sched/scheduler.py b/vllm/v1/core/sched/scheduler.py index 85fc1a4a016a..b8fa750e6005 100644 --- a/vllm/v1/core/sched/scheduler.py +++ b/vllm/v1/core/sched/scheduler.py @@ -39,6 +39,12 @@ class Scheduler(SchedulerInterface): + + # Policy mapping for efficient policy lookup + _POLICY_MAPPING = { + "priority": SchedulingPolicy.PRIORITY, + "fcfs": SchedulingPolicy.FCFS, + } def __init__( self, @@ -99,13 +105,10 @@ def __init__( # req_id -> Request self.requests: dict[str, Request] = {} # Scheduling policy - if self.scheduler_config.policy == "priority": - self.policy = SchedulingPolicy.PRIORITY - elif self.scheduler_config.policy == "fcfs": - self.policy = SchedulingPolicy.FCFS - else: - raise ValueError( - f"Unknown scheduling policy: {self.scheduler_config.policy}") + policy_name = self.scheduler_config.policy + if policy_name not in self._POLICY_MAPPING: + raise ValueError(f"Unknown scheduling policy: {policy_name}") + self.policy = self._POLICY_MAPPING[policy_name] # Priority queues for requests. self.waiting = create_request_queue(self.policy) self.running: list[Request] = [] From 049cf28f4677654ffe700ab9c8f5ff854ad07911 Mon Sep 17 00:00:00 2001 From: "zitian.zhao" Date: Sun, 10 Aug 2025 00:41:50 +0800 Subject: [PATCH 2/3] 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 --- vllm/v1/core/sched/scheduler.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vllm/v1/core/sched/scheduler.py b/vllm/v1/core/sched/scheduler.py index b8fa750e6005..cc1502bcf77f 100644 --- a/vllm/v1/core/sched/scheduler.py +++ b/vllm/v1/core/sched/scheduler.py @@ -106,9 +106,10 @@ def __init__( self.requests: dict[str, Request] = {} # Scheduling policy policy_name = self.scheduler_config.policy - if policy_name not in self._POLICY_MAPPING: - raise ValueError(f"Unknown scheduling policy: {policy_name}") - self.policy = self._POLICY_MAPPING[policy_name] + try: + self.policy = self._POLICY_MAPPING[policy_name] + except KeyError: + raise ValueError(f"Unknown scheduling policy: {policy_name}") from None # Priority queues for requests. self.waiting = create_request_queue(self.policy) self.running: list[Request] = [] From 8d5cd75038487a616f63a5bcca9b0ca3cf7f8ed9 Mon Sep 17 00:00:00 2001 From: "zitian.zhao" Date: Sun, 10 Aug 2025 01:03:19 +0800 Subject: [PATCH 3/3] pre-commit solved Signed-off-by: zitian.zhao --- vllm/v1/core/sched/scheduler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vllm/v1/core/sched/scheduler.py b/vllm/v1/core/sched/scheduler.py index cc1502bcf77f..22ca50f9a5b7 100644 --- a/vllm/v1/core/sched/scheduler.py +++ b/vllm/v1/core/sched/scheduler.py @@ -39,7 +39,6 @@ class Scheduler(SchedulerInterface): - # Policy mapping for efficient policy lookup _POLICY_MAPPING = { "priority": SchedulingPolicy.PRIORITY, @@ -109,7 +108,8 @@ def __init__( try: self.policy = self._POLICY_MAPPING[policy_name] except KeyError: - raise ValueError(f"Unknown scheduling policy: {policy_name}") from None + raise ValueError( + f"Unknown scheduling policy: {policy_name}") from None # Priority queues for requests. self.waiting = create_request_queue(self.policy) self.running: list[Request] = []