Skip to content

Commit f5155bb

Browse files
trim21Rocky Allen
authored and
Rocky Allen
committed
perf: avoid regex re-compile (encode#2700)
* perf * format * avoid * fix
1 parent 0d61105 commit f5155bb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

starlette/responses.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ def __init__(self, max_size: int) -> None:
272272
self.max_size = max_size
273273

274274

275+
_RANGE_PATTERN = re.compile(r"(\d*)-(\d*)")
276+
277+
275278
class FileResponse(Response):
276279
chunk_size = 64 * 1024
277280

@@ -453,7 +456,7 @@ def _parse_range_header(http_range: str, file_size: int) -> list[tuple[int, int]
453456
int(_[0]) if _[0] else file_size - int(_[1]),
454457
int(_[1]) + 1 if _[0] and _[1] and int(_[1]) < file_size else file_size,
455458
)
456-
for _ in re.findall(r"(\d*)-(\d*)", range_)
459+
for _ in _RANGE_PATTERN.findall(range_)
457460
if _ != ("", "")
458461
]
459462

starlette/schemas.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class EndpointInfo(typing.NamedTuple):
2929
func: typing.Callable[..., typing.Any]
3030

3131

32+
_remove_converter_pattern = re.compile(r":\w+}")
33+
34+
3235
class BaseSchemaGenerator:
3336
def get_schema(self, routes: list[BaseRoute]) -> dict[str, typing.Any]:
3437
raise NotImplementedError() # pragma: no cover
@@ -89,7 +92,7 @@ def _remove_converter(self, path: str) -> str:
8992
Route("/users/{id:int}", endpoint=get_user, methods=["GET"])
9093
Should be represented as `/users/{id}` in the OpenAPI schema.
9194
"""
92-
return re.sub(r":\w+}", "}", path)
95+
return _remove_converter_pattern.sub("}", path)
9396

9497
def parse_docstring(self, func_or_method: typing.Callable[..., typing.Any]) -> dict[str, typing.Any]:
9598
"""

0 commit comments

Comments
 (0)