1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json2: improve the performance of encode ~2x (#17050)

This commit is contained in:
Hitalo Souza 2023-01-21 05:45:38 -03:00 committed by GitHub
parent 630fb2af37
commit 1d51f3109f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -236,8 +236,9 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
$if field.typ is string { $if field.typ is string {
e.encode_string(val.$(field.name).str(), mut wr)! e.encode_string(val.$(field.name).str(), mut wr)!
} $else $if field.typ is time.Time { } $else $if field.typ is time.Time {
parsed_time := val.$(field.name) as time.Time wr.write(json2.quote_bytes)!
e.encode_string(parsed_time.format_rfc3339(), mut wr)! wr.write(val.$(field.name).format_rfc3339().bytes())!
wr.write(json2.quote_bytes)!
} $else $if field.typ in [bool, $Float, $Int] { } $else $if field.typ in [bool, $Float, $Int] {
wr.write(val.$(field.name).str().bytes())! wr.write(val.$(field.name).str().bytes())!
} $else $if field.is_array { } $else $if field.is_array {
@ -432,6 +433,7 @@ fn (mut iter CharLengthIterator) next() ?int {
return len return len
} }
// TODO - Need refactor. Is so slow. The longer the string, the lower the performance.
// encode_string returns the JSON spec-compliant version of the string. // encode_string returns the JSON spec-compliant version of the string.
[manualfree] [manualfree]
fn (e &Encoder) encode_string(s string, mut wr io.Writer) ! { fn (e &Encoder) encode_string(s string, mut wr io.Writer) ! {