Skip to content

perf(json): cache stringify indentation - #4145

Open
mizchi wants to merge 2 commits into
moonbitlang:mainfrom
mizchi:perf/json-stringify-indent-cache
Open

perf(json): cache stringify indentation#4145
mizchi wants to merge 2 commits into
moonbitlang:mainfrom
mizchi:perf/json-stringify-indent-cache

Conversation

@mizchi

@mizchi mizchi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Cache pretty-print indentation strings by depth instead of rebuilding the same newline-and-spaces string for every array element or object member. Compact output keeps its existing branch and does not populate the cache.

Native benchmark (six nested arrays, 10,000 innermost values, indent=2):

before after change
1.53 ms 0.64 ms 58% faster

Validation: moon test json --target all, moon check --target all, and moon info.

Copilot AI lite review requested due to automatic review settings August 24, 2026 18:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves Json::stringify pretty-print performance by caching indentation strings per nesting depth, avoiding repeated construction of the same "\n" + spaces prefix during iterative stringification.

Changes:

  • Replace per-write indentation string construction with a depth-indexed indent_cache and a write_indent helper.
  • Add a new benchmark test for deep/wide JSON stringification with indent=2 to validate the optimization’s impact.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
json/json.mbt Caches pretty-print indentation strings by depth and reuses them throughout Json::stringify when indent > 0.
json/stringify_indent_bench_test.mbt Adds a benchmark covering deep nesting with many innermost elements to measure indentation caching wins.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

2 participants