forked from charmbracelet/anthropic-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
perf: add DirectEncoder to eliminate nested MarshalJSON allocation chain #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a5a3042
bench: add marshal allocation benchmark for the quadratic memory inqu…
johnstcn 3733d64
test: skip tests that require mock server or VCR cassettes
johnstcn 47ce0eb
perf: add DirectEncoder interface to bypass MarshalJSON allocation chain
johnstcn 5ff81d3
refactor: add exported IsNull() to avoid param.IsNull alloc overhead
johnstcn 2871442
perf: add DirectEncoder to all 22 union types
johnstcn 394c240
test: gate skips on env vars instead of unconditional t.Skip
johnstcn 342eb8c
perf: add EncodeDirect to all 102 MarshalJSON types with coverage test
johnstcn 3a31df9
perf: add EncodeDirect to all 246 types, AST-based coverage test
johnstcn 678f2e9
bench: exercise all ContentBlockParamUnion variants in marshal benchmark
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package anthropic_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestMarshalMessageNewParams_ByteIdentical(t *testing.T) { | ||
| // Build conversations of various sizes. | ||
| for _, numPairs := range []int{1, 10, 50} { | ||
| t.Run(fmt.Sprintf("pairs=%d", numPairs), func(t *testing.T) { | ||
| params := buildConversation(numPairs) | ||
|
|
||
| // Marshal twice — output must be deterministic and identical. | ||
| first, err := json.Marshal(params) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| second, err := json.Marshal(params) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !bytes.Equal(first, second) { | ||
| t.Fatal("Marshal output is not deterministic") | ||
| } | ||
|
|
||
| // Verify output is valid JSON. | ||
| var raw json.RawMessage | ||
| if err := json.Unmarshal(first, &raw); err != nil { | ||
| t.Fatalf("Marshal output is not valid JSON: %v", err) | ||
| } | ||
|
|
||
| // Verify key structure exists. | ||
| var parsed map[string]json.RawMessage | ||
| if err := json.Unmarshal(first, &parsed); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, ok := parsed["messages"]; !ok { | ||
| t.Fatal("missing 'messages' key in output") | ||
| } | ||
| if _, ok := parsed["model"]; !ok { | ||
| t.Fatal("missing 'model' key in output") | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.