Skip to content
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

json2 perf: encode faster than cJSON #20052

Merged
merged 49 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
d32ffe3
perf: encode
enghitalo Dec 1, 2023
05b65be
fix: remove cap from array
enghitalo Dec 1, 2023
2322ea8
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 1, 2023
e83f453
v test-cleancode
enghitalo Dec 3, 2023
188bc0c
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 3, 2023
f3bf6ca
fix: fix fails on ci
enghitalo Dec 3, 2023
be2f865
fix: fix fails on ci
enghitalo Dec 4, 2023
158cd0e
fix: undo nosense change
enghitalo Dec 5, 2023
e0e20cc
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 5, 2023
652bb28
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 5, 2023
1e17934
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 10, 2023
141ebd2
fix: cmd/tools/vls.v update_manifest
enghitalo Dec 10, 2023
b1f97ae
style: fmt
enghitalo Dec 10, 2023
6dcd3c8
fix
enghitalo Dec 11, 2023
289621a
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 14, 2023
7b1cc1a
ci: fix windows tests
enghitalo Dec 14, 2023
8609d2b
ci: fix windows tests
enghitalo Dec 14, 2023
12757a3
ci: fix windows tests
enghitalo Dec 15, 2023
8b6159b
fix: assigning to a reference field is only allowed in blocks
enghitalo Dec 15, 2023
cc969ca
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 16, 2023
bf9ac83
improve vlib/v/tests/bench/bench_json_vs_json2.v
enghitalo Dec 16, 2023
0ec7e06
skip js tests with error C2099: initializer is not a constant
enghitalo Dec 16, 2023
1cd6431
refactor: make const array fixed
enghitalo Dec 19, 2023
ec2f6de
fix: Code CI / windows / msvc
enghitalo Dec 19, 2023
5daa567
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 19, 2023
17179c5
fix: undo previous commit
enghitalo Dec 19, 2023
69536af
fix: cgen const declaration on windows
enghitalo Dec 19, 2023
2c58030
fix: cgen const declaration on windows
enghitalo Dec 19, 2023
6fff01b
fix: include msvc as compiletime condition to avoid fails
enghitalo Dec 19, 2023
fad9623
fix: new try
enghitalo Dec 19, 2023
e0a5add
remove some json2 skips from skip_on_windows_msvc
enghitalo Dec 20, 2023
efba47e
fix: remove pub const default_encoder = Encoder{}, and free array on …
enghitalo Dec 20, 2023
5b38a69
Merge remote-tracking branch 'upstream/master' into perf/make-encode-…
enghitalo Dec 20, 2023
bc891ea
merge
enghitalo Dec 27, 2023
be0ec8e
fix: merge
enghitalo Dec 27, 2023
3146418
fix: use buf.push_many inside unsafe
enghitalo Dec 27, 2023
3794f10
fix
enghitalo Dec 27, 2023
85bf157
perf: slow bool encode
enghitalo Dec 27, 2023
5db6ea2
fix: try use #define macro
enghitalo Dec 28, 2023
0292cdf
fix: remove fixed array
enghitalo Dec 28, 2023
724810f
cleanup
enghitalo Dec 28, 2023
5094500
ops
enghitalo Dec 28, 2023
290b3b4
]Merge remote-tracking branch 'upstream/master' into perf/make-encode…
enghitalo Dec 29, 2023
ee8444e
undo not related change on vlib/v/gen/js/js.v
enghitalo Dec 29, 2023
e020093
from code review -> move that right above line 590, where it is pushe…
enghitalo Dec 29, 2023
7487402
perf, refactor: use temporary var
enghitalo Dec 29, 2023
3f7757c
doc: pub fn (e &Encoder) encode_value[T](val T, mut buf []u8) ! {
enghitalo Dec 29, 2023
85a897f
fix: ops
enghitalo Dec 29, 2023
c18e85a
use fixed array
enghitalo Dec 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/tools/vls.v
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ fn (upd VlsUpdater) update_manifest(new_path string, from_source bool, timestamp
manifest['last_updated'] = json2.Any(timestamp.format_ss())
manifest['from_source'] = json2.Any(from_source)

json_enc.encode_value(manifest, mut manifest_file)!
mut buffer := []u8{}

json_enc.encode_value(manifest, mut buffer)!

manifest_file.write(buffer)!

unsafe { buffer.free() }
}

fn (upd VlsUpdater) init_download_prebuilt() ! {
Expand Down
7 changes: 6 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5519,7 +5519,12 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
return false
}
escval := util.smart_quote(u8(rune_code).ascii_str(), false)
g.const_decl_write_precomputed(mod, styp, cname, field_name, "'${escval}'")

g.global_const_defs[util.no_dots(field_name)] = GlobalConstDef{
mod: mod
def: "#define ${cname} '${escval}'"
order: -1
}
enghitalo marked this conversation as resolved.
Show resolved Hide resolved
} else {
g.const_decl_write_precomputed(mod, styp, cname, field_name, u32(ct_value).str())
}
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ pub fn gen(files []&ast.File, table &ast.Table, pref_ &pref.Preferences) string
definitions: strings.new_builder(100)
table: table
pref: pref_
fn_decl: 0
fn_decl: unsafe { nil }
empty_line: true
doc: 0
ns: 0
doc: unsafe { nil }
ns: unsafe { nil }
enable_doc: true
file: 0
sourcemap: 0
file: unsafe { nil }
sourcemap: unsafe { nil }
}
enghitalo marked this conversation as resolved.
Show resolved Hide resolved
g.doc = new_jsdoc(g)
// TODO: Add '[-no]-jsdoc' flag
Expand Down
16 changes: 8 additions & 8 deletions vlib/v/tests/bench/bench_json_vs_json2.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import time
import benchmark

// recommendations:
// MAX_ITERATIONS=90_000 ./v run vlib/v/tests/bench/bench_json_vs_json2.v
// MAX_ITERATIONS=90_000 ./v -no-bounds-checking -prod -cc clang-15 crun vlib/v/tests/bench/bench_json_vs_json2.v
// MAX_ITERATIONS=100_000 ./v run vlib/v/tests/bench/bench_json_vs_json2.v
// MAX_ITERATIONS=100_000 ./v -no-bounds-checking -prod -cc clang-15 crun vlib/v/tests/bench/bench_json_vs_json2.v

const max_iterations = os.getenv_opt('MAX_ITERATIONS') or { '1000' }.int()

Expand Down Expand Up @@ -77,7 +77,7 @@ fn benchmark_measure_json_vs_json2_on_complex_struct() ! {
return error('json.decode ${p}')
}
}
b.measure('json.decode')
b.measure('json.decode\n')

measure_json_encode_old_vs_new(json.decode(Person, s)!)!
}
Expand Down Expand Up @@ -124,7 +124,7 @@ fn benchmark_measure_decode_by_type() ! {
return error('json.decode ${d}')
}
}
b.measure(' json.decode StructType[string]')
b.measure(' json.decode StructType[string]\n')

vb := '{"val": true}'
for _ in 0 .. max_iterations {
Expand All @@ -140,7 +140,7 @@ fn benchmark_measure_decode_by_type() ! {
return error('json.decode ${d}')
}
}
b.measure(' json.decode StructType[bool]')
b.measure(' json.decode StructType[bool]\n')

v0 := '{"val": 0}'
for _ in 0 .. max_iterations {
Expand All @@ -156,7 +156,7 @@ fn benchmark_measure_decode_by_type() ! {
return error('json.decode ${d}')
}
}
b.measure(' json.decode StructType[int]')
b.measure(' json.decode StructType[int]\n')

vt := '{"val": "2015-01-06 15:47:32"}'
for _ in 0 .. max_iterations {
Expand All @@ -172,7 +172,7 @@ fn benchmark_measure_decode_by_type() ! {
return error('json2.decode ${d}')
}
}
b.measure(' json.decode StructType[time.Time]')
b.measure(' json.decode StructType[time.Time]\n')
}

fn measure_json_encode_old_vs_new[T](val T) ! {
Expand All @@ -191,5 +191,5 @@ fn measure_json_encode_old_vs_new[T](val T) ! {
return error('json.encode ${e}')
}
}
b.measure(' json.encode ${typename}')
b.measure(' json.encode ${typename}\n')
}
6 changes: 3 additions & 3 deletions vlib/x/json2/encode_struct_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,19 @@ fn test_alias() {

fn test_pointer() {
mut string_initialized_with_reference := ''
assert json.encode(StructTypePointer[string]{ val: 0 }) == '{}'
assert json.encode(StructTypePointer[string]{ val: unsafe { nil } }) == '{}'
assert json.encode(StructTypePointer[string]{ val: &string_initialized_with_reference }) == '{"val":""}'
string_initialized_with_reference = 'a'
assert json.encode(StructTypePointer[string]{ val: &string_initialized_with_reference }) == '{"val":"a"}'

mut bool_initialized_with_reference := false
assert json.encode(StructTypePointer[bool]{ val: 0 }) == '{}'
assert json.encode(StructTypePointer[bool]{ val: unsafe { nil } }) == '{}'
assert json.encode(StructTypePointer[bool]{ val: &bool_initialized_with_reference }) == '{"val":false}'
bool_initialized_with_reference = true
assert json.encode(StructTypePointer[bool]{ val: &bool_initialized_with_reference }) == '{"val":true}'

mut int_initialized_with_reference := 0
assert json.encode(StructTypePointer[int]{ val: 0 }) == '{}'
assert json.encode(StructTypePointer[int]{ val: unsafe { nil } }) == '{}'
assert json.encode(StructTypePointer[int]{ val: &int_initialized_with_reference }) == '{"val":0}'
int_initialized_with_reference = 1
assert json.encode(StructTypePointer[int]{ val: &int_initialized_with_reference }) == '{"val":1}'
Expand Down