Skip to content

Commit cbdd23b

Browse files
cursoragentsimbo1905
andcommitted
Issue #128 Document runtime compilation feature
Updated README.md: - Added 'Runtime Compilation (Performance Optimization)' section - Documents how to use JsonPath.compile() for hot paths - Explains when to use compiled vs interpreted paths - Notes compilation is idempotent Updated AGENTS.md: - Documented the sealed interface architecture - Added new entry points for compilation-related classes - Added guidance for maintaining code generation Co-authored-by: simbo1905 <simbo1905@60hertz.com>
1 parent 995d626 commit cbdd23b

File tree

2 files changed

+90
-9
lines changed

2 files changed

+90
-9
lines changed

json-java21-jsonpath/AGENTS.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,43 @@ This file is for contributor/agent operational notes. Read `json-java21-jsonpath
55
- User docs MUST recommend only `./mvnw`.
66
- The `$(command -v mvnd || command -v mvn || command -v ./mvnw)` wrapper is for local developer speed only; do not put it in user-facing docs.
77

8-
Stable code entry points:
9-
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPath.java`
10-
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathParser.java`
11-
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathAst.java`
12-
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathParseException.java`
13-
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathStreams.java`
14-
15-
When changing syntax/behavior:
16-
- Update `JsonPathAst` + `JsonPathParser` + `JsonPath` together.
8+
## Architecture
9+
10+
JsonPath is a sealed interface with two implementations:
11+
- `JsonPathInterpreted`: AST-walking interpreter (default from `JsonPath.parse()`)
12+
- `JsonPathCompiled`: Bytecode-compiled version (from `JsonPath.compile()`)
13+
14+
The compilation flow:
15+
1. `JsonPath.parse()` -> `JsonPathInterpreted` (fast parsing, AST-based evaluation)
16+
2. `JsonPath.compile()` -> `JsonPathCompiled` (generates Java source, compiles with JDK ToolProvider)
17+
18+
## Stable Code Entry Points
19+
20+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPath.java` - Public sealed interface
21+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathParser.java` - Parses expressions to AST
22+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathAst.java` - AST node definitions
23+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathInterpreted.java` - AST-walking evaluator
24+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathCompiler.java` - Code generator and compiler
25+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathCompiled.java` - Compiled executor wrapper
26+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathExecutor.java` - Public interface for generated executors
27+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathHelpers.java` - Helpers for generated code
28+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathStreams.java` - Stream processing utilities
29+
30+
## When Changing Syntax/Behavior
31+
32+
- Update `JsonPathAst` + `JsonPathParser` + `JsonPathInterpreted` together.
33+
- Update `JsonPathCompiler` code generation to match any AST changes.
1734
- Add parser + evaluation tests; new tests should extend `JsonPathLoggingConfig`.
35+
- Add compiler tests in `JsonPathCompilerTest` to verify compiled and interpreted produce identical results.
36+
37+
## Code Generation Notes
38+
39+
The `JsonPathCompiler` generates Java source code that:
40+
- Imports from `jdk.sandbox.java.util.json.*` and `json.java21.jsonpath.*`
41+
- Implements `JsonPathExecutor` functional interface
42+
- Uses `JsonPathHelpers` for complex operations (recursive descent, comparisons)
43+
- Is compiled in-memory using `javax.tools.ToolProvider`
44+
45+
When adding new AST node types:
46+
1. Add the case to `generateSegmentEvaluation()` in `JsonPathCompiler`
47+
2. Consider if a helper method in `JsonPathHelpers` would simplify the generated code

json-java21-jsonpath/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,57 @@ This implementation follows Goessner-style JSONPath operators, including:
6060
- `[?(@.prop)]` and `[?(@.prop op value)]` basic filters
6161
- `[(@.length-1)]` limited script support
6262

63+
## Runtime Compilation (Performance Optimization)
64+
65+
For performance-critical code paths, you can compile a parsed JsonPath to bytecode:
66+
67+
```java
68+
// Parse once, compile for best performance
69+
JsonPath compiled = JsonPath.compile(JsonPath.parse("$.store.book[*].author"));
70+
71+
// Reuse the compiled path for many documents
72+
for (JsonValue doc : documents) {
73+
List<JsonValue> results = compiled.query(doc);
74+
// process results...
75+
}
76+
```
77+
78+
### How It Works
79+
80+
The `JsonPath.compile()` method:
81+
1. Takes a parsed (interpreted) JsonPath
82+
2. Generates optimized Java source code
83+
3. Compiles it to bytecode using the JDK compiler API (`javax.tools.ToolProvider`)
84+
4. Returns a compiled JsonPath that executes as native bytecode
85+
86+
### When to Use Compilation
87+
88+
- **Hot paths**: Use `compile()` when the same path will be executed many times
89+
- **One-off queries**: Use `parse()` directly for single-use paths (compilation overhead isn't worth it)
90+
- **JRE environments**: Compilation requires a JDK; if unavailable, use interpreted paths
91+
92+
### Compilation is Idempotent
93+
94+
Calling `compile()` on an already-compiled path returns the same instance:
95+
96+
```java
97+
JsonPath interpreted = JsonPath.parse("$.store");
98+
JsonPath compiled = JsonPath.compile(interpreted);
99+
JsonPath sameCompiled = JsonPath.compile(compiled); // Returns same instance
100+
101+
// Check if a path is compiled
102+
boolean isCompiled = path.isCompiled();
103+
boolean isInterpreted = path.isInterpreted();
104+
```
105+
106+
### Supported Features in Compiled Mode
107+
108+
All JsonPath features are supported in compiled mode:
109+
- Property access, array indices, slices, wildcards
110+
- Recursive descent (`$..property`)
111+
- Filters with comparisons and logical operators
112+
- Unions and script expressions
113+
63114
## Stream-Based Functions (Aggregations)
64115

65116
Some JsonPath implementations include aggregation functions such as `$.numbers.avg()`.

0 commit comments

Comments
 (0)