Skip to content

⚡ Bolt: Optimize JSON serialization ~18x for ASCII#2398

Closed
SatoryKono wants to merge 2 commits intomainfrom
bolt/optimize-json-serialization-10380415512159935346
Closed

⚡ Bolt: Optimize JSON serialization ~18x for ASCII#2398
SatoryKono wants to merge 2 commits intomainfrom
bolt/optimize-json-serialization-10380415512159935346

Conversation

@SatoryKono
Copy link
Copy Markdown
Owner

⚡ Bolt: Optimize JSON serialization for ASCII and fix invalid escaping

💡 What:

  • Replaced slow manual string iteration in bioetl.domain.serialization with optimized str.isascii() check.
  • Replaced manual character escaping with standard library json.dumps fallback for non-ASCII data.
  • Updated OrjsonEncoder.dumps to correctly handle ensure_ascii=True by falling back to json.dumps instead of using unicode_escape (which produces invalid JSON \xXX).

🎯 Why:

  • _has_non_ascii and _escape_non_ascii were implemented using Python loops, which are extremely slow (O(N)).
  • OrjsonEncoder was producing invalid JSON when ensure_ascii=True was requested.
  • str.isascii() is implemented in C and is orders of magnitude faster.
  • json.dumps is implemented in C and handles escaping correctly and efficiently.

📊 Impact:

  • ~18x speedup for ASCII-only data serialization (0.47s -> 0.028s in benchmark).
  • ~8x speedup for non-ASCII data compared to previous manual implementation (0.29s -> 0.036s).
  • Ensures valid JSON output for all serialization paths requesting ASCII-only output.

🔬 Measurement:

  • Benchmarked using timeit on 10k iterations of ASCII and non-ASCII datasets.
  • Verified correctness with unit tests in tests/unit/domain/test_serialization.py and tests/unit/infrastructure/serialization/test_json_encoders.py.

PR created automatically by Jules for task 10380415512159935346 started by @SatoryKono

…scaping

Optimized `bioetl.domain.serialization` by replacing manual Python string iteration with `str.isascii()` check and `json.dumps` fallback, resulting in ~18x speedup for ASCII data and ~8x speedup for non-ASCII data compared to the previous manual implementation.

Fixed a correctness bug in `OrjsonEncoder` where `ensure_ascii=True` produced invalid JSON escapes (`\xXX`) by using `json.dumps` as a correct fallback.

Preserved `dumps_canonical` behavior to maintain hash compatibility.

Co-authored-by: SatoryKono <13055362+SatoryKono@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Optimized `bioetl.domain.serialization` by replacing manual Python string iteration with `str.isascii()` check and `json.dumps` fallback, resulting in ~18x speedup for ASCII data and ~8x speedup for non-ASCII data.

Fixed a correctness bug in `OrjsonEncoder` where `ensure_ascii=True` produced invalid JSON escapes (`\xXX`) by using `json.dumps` as a correct fallback.

Addressed CI failures:
- Fixed ambiguous variable names (`l` -> `label`) and unused variables in `src/tools/differentiate_linkstyle.py`.
- Formatted `tests/unit/domain/test_serialization.py` to satisfy architecture tests.

Co-authored-by: SatoryKono <13055362+SatoryKono@users.noreply.github.com>
@SatoryKono
Copy link
Copy Markdown
Owner Author

Superseded by #2423.

@SatoryKono SatoryKono closed this Mar 2, 2026
@SatoryKono SatoryKono deleted the bolt/optimize-json-serialization-10380415512159935346 branch March 3, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant