Repository navigation
refactor: remove encoding/json from the library - #11
Closed
deadprogram wants to merge 1 commit into
Closed
deadprogram wants to merge 1 commit into
deadprogram wants to merge 1 commit into
Conversation
When this package is compiled to WebAssembly with TinyGo, encoding/json adds about 1.6MB to the binary. Measured with `tinygo build -target=wasi -opt=z` on a "Compile plus Render plus tojson" program it was 2715KB before this change and 1435KB after, a drop of 47 percent. The new file `json.go` has the following functions: - appendJSONString escapes a string the way encoding/json does, with an option for the HTML metacharacters. - appendJSONFloat reproduces the encoding/json float format, which the indented form of tojson needs. - parseJSON is a recursive descent parser that builds a Value tree directly. The depth limit of 1000 matches CPython and protects the small TinyGo stack. encodeValueJSON now writes the indented form itself, so tojson no longer calls json.MarshalIndent. The layout is unchanged, which means sorted keys and escaped HTML metacharacters in that mode. Helpers encodeJSONString and valueToGo are no longer needed. The from_json filter uses parseJSON, with no intermediate any tree. Two behaviors are changed to match Python. An integer literal that fits in an int64 stays an integer, so a value such as 9007199254740993 round-trips exactly. Also Object keys keep their source order instead of being sorted. FromGoValue matches json.Number by method set instead of by name, so a caller can still pass a value from a UseNumber decode. New file json_internal_test.go compares the encoder and the parser against encoding/json. Test files can keep the original import because the test binary is not shipped. All 272 template and request pairs in testdata render byte for byte the same as before this change. Signed-off-by: deadprogram <ron@hybridgroup.com>
Author
|
I might need to export some of the new functions... stand by... |
Author
|
Hmm, now that I think about the size of the models being downloaded, perhaps the size difference is not worth it... 🤔 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When this package is compiled to WebAssembly with TinyGo, encoding/json adds about 1.6MB to the binary.
This PR replaces
encoding/jsonwith code specific to the JSON needs of this package without the size increase or external dependencies.Measuring a "Compile plus Render plus tojson" program with
tinygo build -target=wasi -opt=zit was 2715KB before this change and 1435KB after, a drop of 47 percent.The new file
json.gohas the following functions:encodeValueJSON now writes the indented form itself, so tojson no longer calls json.MarshalIndent. The layout is unchanged, which means sorted keys and escaped HTML metacharacters in that mode. Helpers encodeJSONString and valueToGo are no longer needed.
The from_json filter uses parseJSON, with no intermediate any tree. Two behaviors are changed to match Python. An integer literal that fits in an int64 stays an integer, so a value such as 9007199254740993 round-trips exactly. Also Object keys keep their source order instead of being sorted.
FromGoValue matches json.Number by method set instead of by name, so a caller can still pass a value from a UseNumber decode.
New file json_internal_test.go compares the encoder and the parser against encoding/json. Test files can keep the original import because the test binary is not shipped.
All 272 template and request pairs in testdata render byte for byte the same as before this change.