forked from minio/simdjson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_json_amd64.go
104 lines (89 loc) · 2.67 KB
/
parse_json_amd64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//+build !noasm
//+build !appengine
//+build gc
/*
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package simdjson
import (
"bytes"
"errors"
"sync"
)
func (pj *internalParsedJson) initialize(size int) {
// Estimate the tape size to be about 15% of the length of the JSON message
avgTapeSize := size * 15 / 100
if cap(pj.Tape) < avgTapeSize {
pj.Tape = make([]uint64, 0, avgTapeSize)
}
pj.Tape = pj.Tape[:0]
stringsSize := size / 10
if stringsSize < 128 {
stringsSize = 128 // always allocate at least 128 for the string buffer
}
if cap(pj.Strings) < stringsSize {
pj.Strings = make([]byte, 0, stringsSize)
}
pj.Strings = pj.Strings[:0]
if cap(pj.containing_scope_offset) < DEFAULTDEPTH {
pj.containing_scope_offset = make([]uint64, 0, DEFAULTDEPTH)
}
pj.containing_scope_offset = pj.containing_scope_offset[:0]
}
func (pj *internalParsedJson) parseMessage(msg []byte) error {
return pj.parseMessageInternal(msg, false)
}
func (pj *internalParsedJson) parseMessageNdjson(msg []byte) error {
return pj.parseMessageInternal(msg, true)
}
func (pj *internalParsedJson) parseMessageInternal(msg []byte, ndjson bool) (err error) {
// Cache message so we can point directly to strings
// TODO: Find out why TestVerifyTape/instruments fails without bytes.TrimSpace
pj.Message = bytes.TrimSpace(msg)
pj.initialize(len(pj.Message))
if ndjson {
pj.ndjson = 1
} else {
pj.ndjson = 0
}
var wg sync.WaitGroup
wg.Add(2)
// Make the capacity of the channel smaller than the number of slots.
// This way the sender will automatically block until the consumer
// has finished the slot it is working on.
pj.index_chan = make(chan indexChan, INDEX_SLOTS-2)
pj.buffers_offset = ^uint64(0)
var errStage1 error
go func() {
if !find_structural_indices(pj.Message, pj) {
errStage1 = errors.New("Failed to find all structural indices for stage 1")
}
wg.Done()
}()
go func() {
if !unified_machine(pj.Message, pj) {
err = errors.New("Bad parsing while executing stage 2")
// drain the channel until empty
for range pj.index_chan {
}
}
wg.Done()
}()
wg.Wait()
if errStage1 != nil {
return errStage1
}
return
}