Skip to content

Read only the parts of the JSON input that the query can inspect - #87

Merged
davesnx merged 10 commits into
mainfrom
perf/demand-parsing
Nov 10, 2026
Merged

davesnx merged 10 commits into
mainfrom
perf/demand-parsing

Conversation

@davesnx

@davesnx davesnx commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

The query is now analysed before the input is read. The reader builds only the parts of the document that the query can inspect and validates the rest without allocation. Output, error text and exit codes do not change.

This revives the idea of #73 without its second evaluator: the interpreter is unchanged and runs on a pruned tree.

How it works

Demand.of_expression turns the query into a description of what the reader must keep:

type demand = Skip | All | Fields of (string * demand) list | Items of demand
Query Demand
.[] | .name.english Items (Fields ["name", Fields ["english", All]])
map({name, id}) Items (Fields ["name", All; "id", All])
map(.price) | add Items (Fields ["price", All])
length Items Skip
keys, ., any query with fn All
  • A construct the analysis does not know asks for its whole input (All), so the parts of the query before it still narrow the read.
  • A value whose kind does not fit its demand is read in full, and an object with a listed key missing is read again in full. Error messages that show the value stay exact.
  • A skipped value goes through the same grammar as a kept one, so a JSON error has the same text and position.
  • A root .[] | f, map(f), [.[] | f] or . on an array is evaluated one element at a time: read, run f, render into the output buffer, drop. The output is still held until the whole input is valid and the query has succeeded.

New flag

--stream-output prints each result of a root .[] | f when its element is read. The first query error or halt ends the run at once. A JSON error found later follows the results already printed.

Measurements

hyperfine -N --warmup 2, 10 runs, mean ± σ in ms, on a 21 MB array of 97,080 objects, x86_64 Linux. Base is d001076 (main), head is cde94a2, both built with --profile=release. The machine had other load, so read differences below 10% as noise.

Query main this PR jq 1.7
.[] | .name.english 301 ± 7 68 ± 1 449 ± 8
map(.base.Attack) 287 ± 8 71 ± 7 473 ± 14
map(select(.base.Attack > 100) | .name.english) 295 ± 13 80 ± 1 487 ± 28
map({name: .name.english, hp: .base.HP}) 362 ± 18 102 ± 1 591 ± 12
map(.base.Attack) | add 280 ± 4 84 ± 1 478 ± 11
sort_by(.base.Attack) | length 316 ± 8 120 ± 6 738 ± 85
length 246 ± 4 54 ± 1 391 ± 11
.[] 448 ± 25 208 ± 11 588 ± 9
. 468 ± 10 207 ± 13 626 ± 32
keys 256 ± 3 244 ± 8 415 ± 27

Peak memory (/usr/bin/time, one run): .[] | .name.english 160 MB to 8 MB, map(.base.Attack) 152 MB to 8 MB, length 152 MB to 12 MB, .[] 217 MB to 40 MB, . 250 MB to 74 MB.

A query that needs the whole document at once (keys, sort_by(.x)) gets nothing from this change.

Checks

  • A script ran 13,487 combinations of query, document and input method (file, stdin, inline; valid and malformed JSON) through the main binary and this branch: stdout, stderr and exit code are identical in all of them.
  • The test suite does the same comparison in process for each analysed query against a full read (source/test/Test_demand.ml), and checks the reader rules directly (source/jotason/test/Test_demand.ml).

Review notes

  • Start with source/Demand.ml (the rules), then value_d and fields_d in source/jotason/Read.ml, then Core.run_output.
  • Decision to check: a query that does not parse still reads the whole input first, so a JSON error is reported before a query syntax error, as on main.
  • A query that contains fn is read in full, because a definition can shadow a built-in that the analysis knows.
  • The JavaScript build uses the demand analysis but not the per-element path. -n and the REPL are unchanged.
  • Not verified: --stream-output on Windows and with a closed stdout.

Risk: two-way door. A revert restores the full read for every query and removes --stream-output; no data or format changes.

* origin/main:
  Speed up interpreter hot paths and JSON rendering (#72)
  Fit the playground to the viewport and move the share button next to the query (#81)
  Make ? give no output on a type mismatch and stop hiding undefined functions (#83)
  Move .cursor documents to docs and stop tracking .cursor (#84)

# Conflicts:
#	README.md
#	cli/cli.ml
#	source/Core.ml
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

* origin/main:
  Parse plain decimals without copying the literal and compare numbers by value in unique (#74)
  Place the QUERY label inside the query row (#86)
  List each commit's deployment in the preview comment (#85)
  Empty commit

# Conflicts:
#	source/jotason/Read.ml
@davesnx
davesnx marked this pull request as ready for review November 10, 2026 10:47
@davesnx
davesnx merged commit 340f439 into main Nov 10, 2026
5 checks passed
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