Skip to content

Commit 5e0e96c

Browse files
committed
perf: Skip Cache allocation for the common case
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent de2340c commit 5e0e96c

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/harfile/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,17 @@ def add_entry(
147147
dumps = _encode
148148
if isinstance(startedDateTime, datetime):
149149
startedDateTime = startedDateTime.isoformat()
150-
cache = cache or Cache()
150+
# The common case is no cache info; avoid allocating a Cache and
151+
# encoding an empty dict (``dumps(Cache().asdict())`` is just ``"{}"``).
152+
cache_json = "{}" if cache is None else dumps(cache.asdict())
151153
chunk = (
152154
f"{separator} {{"
153155
f'\n "startedDateTime": "{startedDateTime}",'
154156
f'\n "time": {time},'
155157
f'\n "request": {dumps(request.asdict())},'
156158
f'\n "response": {dumps(response.asdict())},'
157159
f'\n "timings": {dumps(timings.asdict())},'
158-
f'\n "cache": {dumps(cache.asdict())}'
160+
f'\n "cache": {cache_json}'
159161
)
160162
if serverIPAddress:
161163
chunk += f',\n "serverIPAddress": {dumps(serverIPAddress)}'

0 commit comments

Comments
 (0)