Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@
## 2024-07-30 - Avoid chained string replace when checking character sets
**Learning:** Using chained `.replace(a, "").replace(b, "")` to check if a string consists entirely of specific characters requires intermediate string allocations for every call. In benchmarks, using `.strip("ab")` is ~30% faster and avoids multiple allocations in the hot path.
**Action:** When checking if a string is solely composed of specific characters, use `.strip(chars)` instead of chained `.replace()` calls to improve performance.

## 2024-07-31 - Bypass string allocation when reading JSON files
**Learning:** Passing `path.read_bytes()` directly to `json.loads()` is faster than `path.read_text()` because it bypasses the intermediate string allocation and decoding overhead.
**Action:** When reading JSON files, use `path.read_bytes()` for performance optimization.
3 changes: 2 additions & 1 deletion src/newsdom_api/mineru_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ def _execute_mineru(cmd: list[str]) -> subprocess.CompletedProcess[str]:
def _read_mineru_json(path: Path, *, artifact: str) -> Any:
"""Read a MinerU JSON artifact with safe, differentiated failure messages."""
try:
return json.loads(path.read_text(encoding="utf-8"))
# ⚡ Bolt: Read bytes directly to bypass intermediate string allocation and decoding overhead
return json.loads(path.read_bytes())
except json.JSONDecodeError as exc:
raise MineruIncompleteOutputError(f"{artifact} JSON was malformed") from exc
except (OSError, UnicodeDecodeError) as exc:
Expand Down
16 changes: 8 additions & 8 deletions tests/test_mineru_runner_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ def test_parse_mineru_output_distinguishes_read_failures(
json.dumps([{"layout_dets": []}]), encoding="utf-8"
)

original_read_text = Path.read_text
original_read_bytes = Path.read_bytes

def fake_read_text(self, *args, **kwargs):
def fake_read_bytes(self, *args, **kwargs):
if self.name == file_name:
raise read_error
return original_read_text(self, *args, **kwargs)
return original_read_bytes(self, *args, **kwargs)

monkeypatch.setattr(Path, "read_text", fake_read_text)
monkeypatch.setattr(Path, "read_bytes", fake_read_bytes)

with pytest.raises(MineruIncompleteOutputError, match=expected_detail) as exc_info:
mineru_runner._parse_mineru_output(tmp_path, Path("sample.pdf"))
Expand Down Expand Up @@ -613,14 +613,14 @@ class Result:

return Result()

original_read_text = Path.read_text
original_read_bytes = Path.read_bytes

def fake_read_text(self, *args, **kwargs):
def fake_read_bytes(self, *args, **kwargs):
if self.name == file_name:
raise read_error
return original_read_text(self, *args, **kwargs)
return original_read_bytes(self, *args, **kwargs)

monkeypatch.setattr(Path, "read_text", fake_read_text)
monkeypatch.setattr(Path, "read_bytes", fake_read_bytes)
monkeypatch.setattr(mineru_runner.subprocess, "run", fake_run)

with pytest.raises(Exception) as exc_info:
Expand Down
Loading