Skip to content

Commit cc5a8ca

Browse files
authored
reduce number of loops over request headers from 2 to 1 (#54326)
`__getitem__` on headers is a O(n) operation. Optimize the code to cut out the extra loop. --------- Signed-off-by: abrar <[email protected]>
1 parent 98cf6f3 commit cc5a8ca

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

python/ray/serve/_private/http_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,11 @@ def __init__(self, app: ASGIApp):
657657

658658
async def __call__(self, scope: Scope, receive: Receive, send: Send):
659659
headers = MutableHeaders(scope=scope)
660-
if SERVE_HTTP_REQUEST_ID_HEADER not in headers:
660+
request_id = headers.get(SERVE_HTTP_REQUEST_ID_HEADER)
661+
662+
if request_id is None:
661663
request_id = generate_request_id()
662664
headers.append(SERVE_HTTP_REQUEST_ID_HEADER, request_id)
663-
elif SERVE_HTTP_REQUEST_ID_HEADER in headers:
664-
request_id = headers[SERVE_HTTP_REQUEST_ID_HEADER]
665665

666666
async def send_with_request_id(message: Message):
667667
if message["type"] == "http.response.start":

0 commit comments

Comments
 (0)