fix: perf: use startswith(x, i) instead of string slicing to avoid O(N^2) (#52580)
Signed-off-by: Ricardo-M-L <ricardoporsche001@icloud.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Ricardo-M-L committed
Sep 26, 2026 at 07:22 UTC
b040321b333c1e9d1b9074d1a8c70e361544667e
1 file changed
+6
-6
vllm/parser/gemma4.py
+6
-6
@@ -120,7 +120,7 @@ def _parse_gemma4_args(args_str: str, *, partial: bool = False) -> dict:
120
result[key] = ""
121
break
122
123
- if args_str[i : i + _DELIM_LEN] == STRING_DELIM:
123
+ if args_str.startswith(STRING_DELIM, i):
124
i += _DELIM_LEN
125
val_start = i
126
end_pos = args_str.find(STRING_DELIM, i)
@@ -139,7 +139,7 @@ def _parse_gemma4_args(args_str: str, *, partial: bool = False) -> dict:
139
obj_start = i + 1
140
i += 1
141
while i < n and depth > 0:
142
- if args_str[i : i + _DELIM_LEN] == STRING_DELIM:
142
+ if args_str.startswith(STRING_DELIM, i):
143
# Skip over string contents to avoid counting { inside strings
144
i += _DELIM_LEN
145
next_delim = args_str.find(STRING_DELIM, i)
@@ -162,7 +162,7 @@ def _parse_gemma4_args(args_str: str, *, partial: bool = False) -> dict:
162
arr_start = i + 1
163
i += 1
164
while i < n and depth > 0:
165
- if args_str[i : i + _DELIM_LEN] == STRING_DELIM:
165
+ if args_str.startswith(STRING_DELIM, i):
166
i += _DELIM_LEN
167
next_delim = args_str.find(STRING_DELIM, i)
168
i = n if next_delim == -1 else next_delim + _DELIM_LEN
@@ -213,7 +213,7 @@ def _parse_gemma4_array(arr_str: str, *, partial: bool = False) -> list:
213
if i >= n:
214
break
215
216
- if arr_str[i : i + _DELIM_LEN] == STRING_DELIM:
216
+ if arr_str.startswith(STRING_DELIM, i):
217
i += _DELIM_LEN
218
end_pos = arr_str.find(STRING_DELIM, i)
219
if end_pos == -1:
@@ -227,7 +227,7 @@ def _parse_gemma4_array(arr_str: str, *, partial: bool = False) -> list:
227
obj_start = i + 1
228
i += 1
229
while i < n and depth > 0:
230
- if arr_str[i : i + _DELIM_LEN] == STRING_DELIM:
230
+ if arr_str.startswith(STRING_DELIM, i):
231
i += _DELIM_LEN
232
nd = arr_str.find(STRING_DELIM, i)
233
i = nd + _DELIM_LEN if nd != -1 else n
@@ -247,7 +247,7 @@ def _parse_gemma4_array(arr_str: str, *, partial: bool = False) -> list:
247
sub_start = i + 1
248
i += 1
249
while i < n and depth > 0:
250
- if arr_str[i : i + _DELIM_LEN] == STRING_DELIM:
250
+ if arr_str.startswith(STRING_DELIM, i):
251
i += _DELIM_LEN
252
nd = arr_str.find(STRING_DELIM, i)
253
i = nd + _DELIM_LEN if nd != -1 else n