Skip to content

Commit 674a9f4

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

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased] - TBD
44

5+
### Performance
6+
7+
- Faster HAR writing.
8+
59
## [0.4.0] - 2025-09-24
610

711
### Added

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cov = [
3939
"coverage[toml]>=7",
4040
]
4141
bench = [
42-
"pytest-codspeed==2.2.1",
42+
"pytest-codspeed==5.0.3; python_version >= '3.9'",
4343
]
4444
dev = ["harfile[tests,cov,bench]"]
4545

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)