Skip to content

Commit

Permalink
FlattenJson: skip string values.
Browse files Browse the repository at this point in the history
```
benchmark                                                old ns/op     new ns/op     delta
Benchmark_JsonFlattener_ContextFields-10                 17230         15985         -7.23%
Benchmark_JsonFlattener_MiddleNestedField-10             17392         16225         -6.71%
Benchmark_JsonFlattener_LastField-10                     17029         15847         -6.94%
Benchmark_JsonFlattner_Evaluate_ContextFields-10         17596         16434         -6.60%
Benchmark_JsonFlattner_Evaluate_MiddleNestedField-10     17840         16836         -5.63%

benchmark                                                old allocs     new allocs     delta
Benchmark_JsonFlattener_ContextFields-10                 23             3              -86.96%
Benchmark_JsonFlattener_MiddleNestedField-10             24             4              -83.33%
Benchmark_JsonFlattener_LastField-10                     22             2              -90.91%
Benchmark_JsonFlattner_Evaluate_ContextFields-10         33             13             -60.61%
Benchmark_JsonFlattner_Evaluate_MiddleNestedField-10     33             13             -60.61%

benchmark                                                old bytes     new bytes     delta
Benchmark_JsonFlattener_ContextFields-10                 560           48            -91.43%
Benchmark_JsonFlattener_MiddleNestedField-10             576           64            -88.89%
Benchmark_JsonFlattener_LastField-10                     544           32            -94.12%
Benchmark_JsonFlattner_Evaluate_ContextFields-10         992           480           -51.61%
Benchmark_JsonFlattner_Evaluate_MiddleNestedField-10     1000          488           -51.20%
```
  • Loading branch information
yosiat committed Oct 3, 2022
1 parent 814f0a7 commit bd0c943
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion flatten_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ func (fj *flattenJSON) readObject(pathName []byte) error {
var alt []byte
switch ch {
case '"':
val, err = fj.readStringValue()
if fj.skipping > 0 || !memberIsUsed {
err = fj.skipStringValue()
} else {
val, err = fj.readStringValue()
}
isLeaf = true
case 't':
val, err = fj.readLiteral(trueBytes)
Expand Down Expand Up @@ -475,6 +479,42 @@ func (fj *flattenJSON) readLiteral(literal []byte) ([]byte, error) {
return literal, nil
}

func (fj *flattenJSON) skipStringValue() error {
if fj.step() != nil {
return fj.error("event truncated in mid-string")
}

escaped := false
data := fj.event[fj.eventIndex:]

for i, c := range data {
if c == '"' {
if !escaped {
fj.eventIndex = fj.eventIndex + i
return nil
} else {
j := i - 1
for {
if j < 0 || data[j] != '\\' {
fj.eventIndex = fj.eventIndex + i
return nil
}
j--
if j < 0 || data[j] != '\\' {
break // odd number of backslashes
}
j--

}
}
} else if c == '\\' {
escaped = true
}
}

return nil
}

// we're positioned at the " that marks the start of a string value in an array or object.
// ideally, we'd like to construct the member name as just a slice of the event buffer,
// but will have to find a new home for it if it has JSON \-escapes
Expand Down Expand Up @@ -510,6 +550,8 @@ func (fj *flattenJSON) readStringValWithEscapes(nameStart int) ([]byte, error) {
if ch == '"' {
fj.eventIndex = from
val = append(val, '"')
//fmt.Printf("readStringValWithEscapes skipping [%d] value [%s]\n", fj.skipping, string(val))
//panic(fj.error("fuck"))
return val, nil
} else if ch == '\\' {
var unescaped []byte
Expand Down

0 comments on commit bd0c943

Please sign in to comment.