MCPcopy Create free account
hub / github.com/dgraph-io/dgraph / FastParseJSON

Method FastParseJSON

chunker/json_parser.go:671–746  ·  view source on GitHub ↗

FastParseJSON currently parses NQuads about 30% faster than ParseJSON. This function is very similar to buf.ParseJSON, but we just replace encoding/json with simdjson-go.

(b []byte, op int)

Source from the content-addressed store, hash-verified

669// This function is very similar to buf.ParseJSON, but we just replace encoding/json with
670// simdjson-go.
671func (buf *NQuadBuffer) FastParseJSON(b []byte, op int) error {
672 if !simdjson.SupportedCPU() {
673 // default to slower / old parser
674 return buf.ParseJSON(b, op)
675 }
676 // parse the json into tape format
677 tape, err := simdjson.Parse(b, nil)
678 if err != nil {
679 return err
680 }
681
682 // we only need the iter to get the first element, either an array or object
683 iter := tape.Iter()
684
685 tmp := &simdjson.Iter{}
686
687 // if root object, this will be filled
688 obj := &simdjson.Object{}
689 // if root array, this will be filled
690 arr := &simdjson.Array{}
691
692 // grab the first element
693 typ := iter.Advance()
694 switch typ {
695 case simdjson.TypeRoot:
696 if typ, tmp, err = iter.Root(tmp); err != nil {
697 return err
698 }
699 if typ == simdjson.TypeObject {
700 // the root element is an object, so parse the object
701 if obj, err = tmp.Object(obj); err != nil {
702 return err
703 }
704 // attempt to convert to map[string]interface{}
705 m, err := obj.Map(nil)
706 if err != nil {
707 return err
708 }
709 // pass to next parsing stage
710 mr, err := buf.mapToNquads(m, op, "")
711 if err != nil {
712 return err
713 }
714 buf.checkForDeletion(mr, m, op)
715 } else if typ == simdjson.TypeArray {
716 // the root element is an array, so parse the array
717 if arr, err = tmp.Array(arr); err != nil {
718 return err
719 }
720 // attempt to convert to []interface{}
721 a, err := arr.Interface()
722 if err != nil {
723 return err
724 }
725 if len(a) > 0 {
726 // attempt to convert each array element to a
727 // map[string]interface{} for further parsing
728 var o interface{}

Callers 3

FastParseFunction · 0.95
ParseJSONFunction · 0.95

Calls 5

ParseJSONMethod · 0.95
mapToNquadsMethod · 0.95
checkForDeletionMethod · 0.95
MapMethod · 0.80
ParseMethod · 0.65

Tested by 2

FastParseFunction · 0.76