Repository navigation
Optimize LSP discriminated union decoding - #63934
Jake Bailey (jakebailey) merged 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Optimizes LSP discriminated-union decoding by streaming fields once the discriminator is found.
Changes:
- Adds reusable streaming struct decoding.
- Updates generated union decoders and generator logic.
- Adds correctness tests and benchmarks.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
structcodec.go |
Implements streaming discriminated-struct decoding. |
lsp.go |
Removes obsolete raw-field scanning. |
lsp_json_test.go |
Tests discriminator ordering and absence. |
lsp_json_benchmark_test.go |
Benchmarks buffered versus streaming decoding. |
lsp_generated.go |
Uses streaming decoding in generated unions. |
_generate/generate.mts |
Generates streaming discriminator dispatch. |
Files not reviewed (1)
- tsc/internal/lsp/lsproto/lsp_generated.go: Generated file
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.
Files not reviewed (1)
- tsc/internal/lsp/lsproto/lsp_generated.go: Generated file
Suppressed comments (2)
tsc/internal/lsp/lsproto/_generate/generate.mts:3462
- This path can stream only when the optional fallback arm is a generated structure. In particular, when
hasUnknownKindscomes from ananyarm, non-object input now fails the object scan instead of decoding asany, while object input reaching the default arm can panic whenunmarshalDiscriminatedArmreflects over the non-struct. Maps are another non-struct object fallback with the same panic. Retain the buffered discriminator dispatch for such arms.
if (disc && disc.unmapped.length <= 1) {
generateStreamingDiscriminatorDispatch(name, disc, "\t");
exhaustive = true;
tsc/internal/lsp/lsproto/structcodec.go:212
- Do not reject non-string values here: a generated union may have one unmapped fallback arm. For example, a valid
TextDocumentEditcan contain an unknown"kind": nullproperty; the previous decoder dispatched its raw value to the defaultTextDocumentEditarm, whose struct decoder ignores that property, but this scan now fails before reaching the fallback. Retain the raw value and let the generated switch either choose its fallback or report an invalid discriminator when no fallback exists.
if value.Kind() != '"' {
return discriminatedStructDecoder{}, fmt.Errorf("invalid %s discriminator %q: got %v", typeName, discriminator, value.Kind())
}
c2a0f70 to
66d7e1c
Compare
66d7e1c to
08525ad
Compare
3e578cb to
091f8d2
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Strict script type-checking loses a required union narrowing inside the new nested callback.
Get a fresh assessment by requesting another Copilot review.
Review details
Files not reviewed (1)
- tsc/internal/lsp/lsproto/lsp_generated.go: Generated file
- Files reviewed: 4/5 changed files
- Comments generated: 1
- Review effort level: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
The retained buffered generator path references the removed jsonObjectRawField helper and can emit uncompilable Go code.
Get a fresh assessment by requesting another Copilot review.
Review details
Files not reviewed (1)
- tsc/internal/lsp/lsproto/lsp_generated.go: Generated file
- Files reviewed: 4/5 changed files
- Comments generated: 1
- Review effort level: Balanced
091f8d2 to
897dd77
Compare
897dd77 to
34fbed0
Compare
While doing some JSON stuff, I realized that since the LSP client put the discriminator first, we can skip buffering anything and just go right into the real decode. If the discriminator is not first, then we are forced to do the slow thing, but the VS Code language client always puts the discriminator first!
This nets faster decoding: