Skip to content

Commit 1d51f31

Browse files
authored
json2: improve the performance of encode ~2x (#17050)
1 parent 630fb2a commit 1d51f31

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

vlib/x/json2/encoder.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
236236
$if field.typ is string {
237237
e.encode_string(val.$(field.name).str(), mut wr)!
238238
} $else $if field.typ is time.Time {
239-
parsed_time := val.$(field.name) as time.Time
240-
e.encode_string(parsed_time.format_rfc3339(), mut wr)!
239+
wr.write(json2.quote_bytes)!
240+
wr.write(val.$(field.name).format_rfc3339().bytes())!
241+
wr.write(json2.quote_bytes)!
241242
} $else $if field.typ in [bool, $Float, $Int] {
242243
wr.write(val.$(field.name).str().bytes())!
243244
} $else $if field.is_array {
@@ -432,6 +433,7 @@ fn (mut iter CharLengthIterator) next() ?int {
432433
return len
433434
}
434435

436+
// TODO - Need refactor. Is so slow. The longer the string, the lower the performance.
435437
// encode_string returns the JSON spec-compliant version of the string.
436438
[manualfree]
437439
fn (e &Encoder) encode_string(s string, mut wr io.Writer) ! {

0 commit comments

Comments
 (0)