Skip to content

Commit 9d45a09

Browse files
committed
perf, refactor: simplify and performe strings
1 parent d6d9ac3 commit 9d45a09

3 files changed

Lines changed: 40 additions & 31 deletions

File tree

decode.v

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ pub fn (mut d Decoder) decode_to_json[T](src []u8) !string {
3131
d.buffer = src
3232
d.next()!
3333

34-
mut result := ''
34+
mut result := []u8{}
35+
36+
defer {
37+
unsafe { result.free() }
38+
}
3539

3640
data := d.buffer
3741

@@ -41,72 +45,75 @@ pub fn (mut d Decoder) decode_to_json[T](src []u8) !string {
4145

4246
mut d_for_array := new_decoder()
4347

44-
result += '['
48+
result << `[`
4549

4650
for i in 0 .. array_len {
4751
if i > 0 {
48-
result += ','
52+
result << `,`
4953
}
5054

5155
element_json := d_for_array.decode_to_json[T](data[1..]) or {
5256
return error('error converting array element to JSON ${err}')
5357
}
5458

55-
result += element_json
59+
unsafe { result.push_many(element_json.str, element_json.len) }
5660
}
5761

58-
result += ']'
62+
result << `]`
5963
}
6064
mp_map_16, mp_map_32, mp_fix_map_min...mp_fix_map_max {
6165
map_len := d.read_map_len(src) or { return error('error reading map length') }
6266

63-
result += '{'
67+
result << `{`
6468

6569
for i in 0 .. map_len {
6670
if i > 0 {
67-
result += ','
71+
result << `,`
6872
}
6973

7074
key := d.decode_to_json[string](src) or {
7175
return error('error converting map key to JSON: ${err}')
7276
}
77+
unsafe { result.push_many(key.str, key.len) }
78+
result << `:`
7379

7480
value_json := d.decode_to_json[T](src) or {
7581
return error('error converting map value to JSON')
7682
}
77-
78-
result += '${key}:${value_json}'
83+
unsafe { result.push_many(value_json.str().str, value_json.str().len) }
7984
}
8085

81-
result += '}'
86+
result << `}`
8287
}
8388
mp_nil {
84-
result += 'null'
89+
unsafe { result.push_many('null'.str, 'null'.len) }
8590
}
8691
mp_true, mp_false {
8792
mut bool_val := false
8893
d.decode_bool(mut bool_val) or { return error('error decoding boolean: ${err}') }
89-
result += bool_val.str()
94+
unsafe { result.push_many(bool_val.str().str, bool_val.str().len) }
9095
}
9196
mp_f32, mp_f64 {
9297
mut float_val := 0.0
9398
d.decode_float(mut float_val) or { return error('error decoding float: ${err}') }
94-
result += float_val.str()
99+
unsafe { result.push_many(float_val.str().str, float_val.str().len) }
95100
}
96101
mp_u8, mp_u16, mp_u32, mp_u64, mp_i8, mp_i16, mp_i32, mp_i64 {
97102
mut int_val := 0
98103
d.decode_integer(mut int_val) or { return error('error decoding integer: ${err}') }
99-
result += int_val.str()
104+
unsafe { result.push_many(int_val.str().str, int_val.str().len) }
100105
}
101106
mp_str_8, mp_str_16, mp_str_32, mp_fix_str_min...mp_fix_str_max {
102107
mut str_val := ''
103108
d.decode_string(mut str_val) or { return error('error decoding string: ${err}') }
104-
result += '"${str_val}"'
109+
result << `\"`
110+
unsafe { result.push_many(str_val.str, str_val.len) }
111+
result << `\"`
105112
}
106113
mp_bin_8, mp_bin_16, mp_bin_32 {
107114
bin_len := d.read_bin_len(src) or { return error('error reading binary length') }
108115
for i in 0 .. bin_len {
109-
result += src[d.pos + i].str()
116+
unsafe { result.push_many(src[d.pos + i].str().str, src[d.pos + i].str().len) }
110117
}
111118

112119
d.pos += bin_len
@@ -117,7 +124,7 @@ pub fn (mut d Decoder) decode_to_json[T](src []u8) !string {
117124
return error('unsupported descriptor byte for conversion to JSON')
118125
}
119126
}
120-
return result
127+
return result.bytestr()
121128
}
122129

123130
pub fn decode[T](src []u8) !T {

encode.v

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
module msgpack
22

3-
import strings
43
import math
54
import time
65

76
struct Encoder {
87
mut:
9-
buffer strings.Builder = strings.new_builder(1024)
8+
buffer []u8
109
config Config = default_config()
1110
}
1211

@@ -114,9 +113,9 @@ pub fn (mut e Encoder) encode[T](data T) []u8 {
114113

115114
pub fn (mut e Encoder) encode_bool(b bool) {
116115
if b {
117-
e.buffer.write_u8(mp_true)
116+
e.buffer << mp_true
118117
} else {
119-
e.buffer.write_u8(mp_false)
118+
e.buffer << mp_false
120119
}
121120
}
122121

@@ -354,7 +353,9 @@ pub fn (mut e Encoder) write_map_start(length int) {
354353
}
355354

356355
fn (mut e Encoder) write_string(s string) {
357-
e.buffer.write_string(s)
356+
if s.len > 0 {
357+
unsafe { e.buffer.push_many(s.str, s.len) }
358+
}
358359
}
359360

360361
fn (mut e Encoder) write_u16(i u16) {
@@ -379,14 +380,14 @@ pub fn (mut e Encoder) write_f64(f f64) {
379380

380381
// write byte array
381382
fn (mut e Encoder) write(b []u8) {
382-
e.buffer.write(b) or { panic('write error') }
383+
unsafe { e.buffer.push_many(b.bytestr().str, b.bytestr().len) }
383384
}
384385

385386
// write one or more bytes
386387
fn (mut e Encoder) write_u8(b ...u8) {
387388
if b.len > 1 {
388-
e.write(b)
389+
e.buffer << b
389390
} else {
390-
e.buffer.write_u8(b[0])
391+
e.buffer << b[0]
391392
}
392393
}

examples/bench_msgpack_json_vs_json2.v

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import os
44
import json
55
import x.json2
66
import msgpack
7-
import time
87
import benchmark
8+
// import time
99

1010
struct Person {
11-
name string
12-
age int
13-
created_at time.Time
11+
name string
12+
age int
13+
// created_at time.Time
1414
}
1515

1616
fn main() {
17-
max_iterations := os.getenv_opt('MAX_ITERATIONS') or { '1000' }.int()
18-
s := '{"name":"Bilbo Baggins","age":99,"created_at":1670840340}'
17+
max_iterations := os.getenv_opt('MAX_ITERATIONS') or { '1000000' }.int()
18+
// s := '{"name":"Bilbo Baggins","age":99,"created_at":1670840340}'
19+
s := '{"name":"Bilbo Baggins","age":99}'
1920
mut b := benchmark.start()
2021

2122
for _ in 0 .. max_iterations {

0 commit comments

Comments
 (0)