From 5f0ee3c97408723f40101e97b0734303fadb1cce Mon Sep 17 00:00:00 2001 From: Pawan Rawal <pawan0201@gmail.com> Date: Thu, 12 May 2016 13:46:19 +0530 Subject: [PATCH] Changing properties in graph.Node to a slice from a map. --- query/benchmark/README.txt | 12 +++-- query/graph/graphresponse.pb.go | 79 ++++++++++++++++++++------------- query/graph/graphresponse.proto | 7 ++- query/query.go | 17 ++++--- query/query_test.go | 18 ++++++-- 5 files changed, 89 insertions(+), 44 deletions(-) diff --git a/query/benchmark/README.txt b/query/benchmark/README.txt index 64f152a9..d70e0e11 100644 --- a/query/benchmark/README.txt +++ b/query/benchmark/README.txt @@ -29,8 +29,14 @@ Directors query Benchmarking tests were run for ToJson and ToProtocolBuffer methods. Results from the `go test` command are tabulated below. -BenchmarkToJson 500 3939003 ns/op 957723 B/op 16115 allocs/op -BenchmarkToProtocolBuffer 1000 2288681 ns/op 566287 B/op 7542 allocs/op +BenchmarkToJson 500 3583970 ns/op 957747 B/op 16115 allocs/op +BenchmarkToProtocolBuffer 1000 2299409 ns/op 566288 B/op 7542 allocs/op We can see that ToProtocolBuffer method allocates less memory and takes lesser -time than ToJson method. \ No newline at end of file +time than ToJson method. + +After changing properties inside a graph.Node from a map to a slice, we can see +further improvements. + +BenchmarkToJson 500 3726982 ns/op 957679 B/op 16115 allocs/op +BenchmarkToProtocolBuffer 1000 1954618 ns/op 395603 B/op 7377 allocs/op diff --git a/query/graph/graphresponse.pb.go b/query/graph/graphresponse.pb.go index 14a86faa..c56d6df9 100644 --- a/query/graph/graphresponse.pb.go +++ b/query/graph/graphresponse.pb.go @@ -12,6 +12,7 @@ It has these top-level messages: Request Value Latency + Property Node Response */ @@ -64,20 +65,37 @@ func (m *Latency) String() string { return proto.CompactTextString(m) func (*Latency) ProtoMessage() {} func (*Latency) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } +type Property struct { + Prop string `protobuf:"bytes,1,opt,name=prop" json:"prop,omitempty"` + Val *Value `protobuf:"bytes,2,opt,name=val" json:"val,omitempty"` +} + +func (m *Property) Reset() { *m = Property{} } +func (m *Property) String() string { return proto.CompactTextString(m) } +func (*Property) ProtoMessage() {} +func (*Property) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } + +func (m *Property) GetVal() *Value { + if m != nil { + return m.Val + } + return nil +} + type Node struct { - Uid uint64 `protobuf:"varint,1,opt,name=uid" json:"uid,omitempty"` - Xid string `protobuf:"bytes,2,opt,name=xid" json:"xid,omitempty"` - Attribute string `protobuf:"bytes,3,opt,name=attribute" json:"attribute,omitempty"` - Properties map[string]*Value `protobuf:"bytes,4,rep,name=properties" json:"properties,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` - Children []*Node `protobuf:"bytes,5,rep,name=children" json:"children,omitempty"` + Uid uint64 `protobuf:"varint,1,opt,name=uid" json:"uid,omitempty"` + Xid string `protobuf:"bytes,2,opt,name=xid" json:"xid,omitempty"` + Attribute string `protobuf:"bytes,3,opt,name=attribute" json:"attribute,omitempty"` + Properties []*Property `protobuf:"bytes,4,rep,name=properties" json:"properties,omitempty"` + Children []*Node `protobuf:"bytes,5,rep,name=children" json:"children,omitempty"` } func (m *Node) Reset() { *m = Node{} } func (m *Node) String() string { return proto.CompactTextString(m) } func (*Node) ProtoMessage() {} -func (*Node) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } +func (*Node) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } -func (m *Node) GetProperties() map[string]*Value { +func (m *Node) GetProperties() []*Property { if m != nil { return m.Properties } @@ -99,7 +117,7 @@ type Response struct { func (m *Response) Reset() { *m = Response{} } func (m *Response) String() string { return proto.CompactTextString(m) } func (*Response) ProtoMessage() {} -func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } +func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} } func (m *Response) GetN() *Node { if m != nil { @@ -119,6 +137,7 @@ func init() { proto.RegisterType((*Request)(nil), "graph.Request") proto.RegisterType((*Value)(nil), "graph.Value") proto.RegisterType((*Latency)(nil), "graph.Latency") + proto.RegisterType((*Property)(nil), "graph.Property") proto.RegisterType((*Node)(nil), "graph.Node") proto.RegisterType((*Response)(nil), "graph.Response") } @@ -195,26 +214,26 @@ var _DGraph_serviceDesc = grpc.ServiceDesc{ } var fileDescriptor0 = []byte{ - // 336 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x5c, 0x52, 0x4d, 0x4f, 0x02, 0x31, - 0x10, 0xb5, 0xc0, 0xf2, 0x31, 0x18, 0x30, 0xa3, 0x87, 0x05, 0x8d, 0x92, 0x5e, 0x24, 0x1e, 0xf6, - 0x80, 0x1e, 0x8c, 0x1e, 0xd5, 0x78, 0xd0, 0x18, 0xad, 0x89, 0xf7, 0x05, 0x1a, 0xd8, 0xb8, 0xe9, - 0xae, 0x6d, 0xd7, 0xc8, 0x6f, 0xf7, 0xe2, 0x76, 0xb6, 0x20, 0xe1, 0xf6, 0xe6, 0xbd, 0xe9, 0x7b, - 0xed, 0x4c, 0xe1, 0x70, 0xa1, 0xe3, 0x7c, 0xa9, 0xa5, 0xc9, 0x33, 0x65, 0x64, 0x94, 0xeb, 0xcc, - 0x66, 0x18, 0x10, 0xc9, 0xcf, 0xa0, 0x25, 0xe4, 0x57, 0x21, 0x8d, 0xc5, 0x23, 0x08, 0x4a, 0xa0, - 0x57, 0x21, 0x1b, 0xb1, 0x71, 0x47, 0x54, 0x05, 0x1f, 0x40, 0xf0, 0x11, 0xa7, 0x85, 0xc4, 0x03, - 0xa8, 0x1b, 0xab, 0xbd, 0xe8, 0x20, 0x7f, 0x87, 0xd6, 0x73, 0x6c, 0xa5, 0x9a, 0xad, 0x30, 0x84, - 0x56, 0x1e, 0x6b, 0x93, 0xa8, 0x85, 0x6f, 0x58, 0x97, 0x78, 0x0a, 0x50, 0x06, 0xce, 0xa4, 0x21, - 0xb1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0xff, 0xff, 0xeb, 0x75, 0x69, 0x89, 0x2e, 0x02, 0x00, 0x00, + // 327 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x5c, 0x91, 0x3f, 0x4f, 0xc3, 0x30, + 0x10, 0xc5, 0x71, 0x93, 0xf4, 0xcf, 0x15, 0xb5, 0xe8, 0x60, 0x48, 0x11, 0x82, 0xca, 0x0b, 0x88, + 0xa1, 0x48, 0x85, 0x99, 0x05, 0x24, 0x16, 0x84, 0xc0, 0x48, 0xec, 0x69, 0x6b, 0xb5, 0x91, 0xa2, + 0xc4, 0xd8, 0x0e, 0xa2, 0x5f, 0x87, 0x4f, 0x8a, 0x7d, 0x71, 0x4a, 0xc5, 0xf6, 0xfc, 0xde, 0x8b, + 0xef, 0xe7, 0x1c, 0x1c, 0xaf, 0x75, 0xa6, 0x36, 0x5a, 0x1a, 0x55, 0x95, 0x46, 0xce, 0x94, 0xae, + 0x6c, 0x85, 0x09, 0x99, 0xfc, 0x02, 0x7a, 0x42, 0x7e, 0xd6, 0xd2, 0x58, 0x3c, 0x81, 0xc4, 0x09, + 0xbd, 0x4d, 0xd9, 0x94, 0x5d, 0x0d, 0x44, 0x73, 0xe0, 0x13, 0x48, 0x3e, 0xb2, 0xa2, 0x96, 0x78, + 0x04, 0x91, 0xb1, 0x3a, 0x84, 0x5e, 0xf2, 0x77, 0xe8, 0x3d, 0x67, 0x56, 0x96, 0xcb, 0x2d, 0xa6, + 0xd0, 0x53, 0x99, 0x36, 0x79, 0xb9, 0x0e, 0x85, 0xf6, 0x88, 0xe7, 0x00, 0x6e, 0xe0, 0x52, 0x1a, + 0x0a, 0x3b, 0x14, 0xee, 0x39, 0x38, 0x82, 0x8e, 0x5a, 0xa4, 0x11, 0xf9, 0x4e, 0xf1, 0x7b, 0xe8, + 0xbf, 0xea, 0x4a, 0x49, 0x6d, 0xb7, 0x88, 0x10, 0xbb, 0xa6, 0x0a, 0x57, 0x92, 0x76, 0xf7, 0x45, + 0x5f, 0x59, 0x41, 0x17, 0x0d, 0xe7, 0x87, 0x33, 0x7a, 0xc5, 0x8c, 0x08, 0x85, 0x0f, 0xf8, 0x0f, + 0x83, 0xf8, 0xa5, 0x5a, 0x11, 0x6f, 0x9d, 0xaf, 0xe8, 0xdb, 0x58, 0x78, 0xe9, 0x9d, 0x6f, 0xe7, + 0x34, 0x0c, 0x5e, 0xe2, 0x19, 0x0c, 0x32, 0x6b, 0x75, 0xbe, 0xa8, 0xad, 0x0c, 0x0c, 0x7f, 0x06, + 0xde, 0x10, 0xba, 0x47, 0xc9, 0xa5, 0x49, 0xe3, 0x69, 0xe4, 0x26, 0x8e, 0xc3, 0xc4, 0x96, 0x51, + 0xec, 0x55, 0xf0, 0x12, 0xfa, 0xcb, 0x4d, 0x5e, 0xac, 0xb4, 0x2c, 0xd3, 0x84, 0xea, 0xc3, 0x50, + 0xf7, 0x44, 0x62, 0x17, 0xf2, 0x07, 0xe8, 0x8b, 0xb0, 0x0e, 0x9c, 0x00, 0x2b, 0x89, 0xf2, 0x5f, + 0x9b, 0x95, 0x0e, 0x8f, 0xb5, 0x2f, 0x1d, 0x85, 0x28, 0xfc, 0x70, 0xc1, 0x8a, 0xf9, 0x1d, 0x74, + 0x1f, 0x9f, 0xbc, 0x89, 0xd7, 0x90, 0xbc, 0xf9, 0x65, 0x61, 0xdb, 0x0a, 0x2b, 0x3d, 0x1d, 0xef, + 0xce, 0xcd, 0x30, 0x7e, 0xb0, 0xe8, 0xd2, 0xfa, 0x6f, 0x7f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x34, + 0xba, 0xdf, 0x53, 0x15, 0x02, 0x00, 0x00, } diff --git a/query/graph/graphresponse.proto b/query/graph/graphresponse.proto index 9c1086d8..6f35f87f 100644 --- a/query/graph/graphresponse.proto +++ b/query/graph/graphresponse.proto @@ -22,11 +22,16 @@ message Latency { string pb = 3; } +message Property { + string prop = 1; + Value val = 2; +} + message Node { uint64 uid = 1; string xid = 2; string attribute = 3; - map<string, Value> properties = 4; + repeated Property properties = 4; repeated Node children = 5; // Each node can have multiple children } diff --git a/query/query.go b/query/query.go index 956fc9cb..64234638 100644 --- a/query/query.go +++ b/query/query.go @@ -277,8 +277,7 @@ func indexOf(uid uint64, q *task.Query) int { // This method gets the values and children for a subgraph. func (g *SubGraph) preTraverse(uid uint64, dst *graph.Node) error { - var properties map[string]*graph.Value - properties = make(map[string]*graph.Value) + var properties []*graph.Property var children []*graph.Node // We go through all predicate children of the subgraph. @@ -339,12 +338,18 @@ func (g *SubGraph) preTraverse(uid uint64, dst *graph.Node) error { } v.Str = ival.(string) - properties[pc.Attr] = v + properties = append(properties, + &graph.Property{Prop: pc.Attr, Val: v}) } } - if val, ok := properties["_xid_"]; ok { - dst.Xid = val.Str - delete(properties, "_xid_") + + for i, p := range properties { + if p.Prop == "_xid_" { + dst.Xid = p.Val.Str + // Deleting the _xid_ property if it exists + properties = append(properties[:i], properties[i+1:]...) + break + } } dst.Properties, dst.Children = properties, children return nil diff --git a/query/query_test.go b/query/query_test.go index 5bcd9ad4..7f1b41fc 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -28,6 +28,7 @@ import ( "github.com/dgraph-io/dgraph/commit" "github.com/dgraph-io/dgraph/gql" "github.com/dgraph-io/dgraph/posting" + "github.com/dgraph-io/dgraph/query/graph" "github.com/dgraph-io/dgraph/store" "github.com/dgraph-io/dgraph/task" "github.com/dgraph-io/dgraph/worker" @@ -325,6 +326,15 @@ func TestToJson(t *testing.T) { fmt.Printf(string(js)) } +func getProperty(properties []*graph.Property, prop string) (v *graph.Value) { + for _, p := range properties { + if p.Prop == prop { + return p.Val + } + } + return v +} + func TestToProtocolBuffer(t *testing.T) { dir, _ := populateGraph(t) defer os.RemoveAll(dir) @@ -380,9 +390,9 @@ func TestToProtocolBuffer(t *testing.T) { t.Errorf("Expected values map to contain 3 properties, Got: %v", len(gr.Properties)) } - if gr.Properties["name"].Str != "Michonne" { + if getProperty(gr.Properties, "name").Str != "Michonne" { t.Errorf("Expected property name to have value Michonne, Got: %v", - gr.Properties["name"].Str) + getProperty(gr.Properties, "name").Str) } if len(gr.Children) != 10 { t.Errorf("Expected 10 children, Got: %v", len(gr.Children)) @@ -399,9 +409,9 @@ func TestToProtocolBuffer(t *testing.T) { t.Errorf("Expected values map to contain 1 property, Got: %v", len(child.Properties)) } - if child.Properties["name"].Str != "Rick Grimes" { + if getProperty(child.Properties, "name").Str != "Rick Grimes" { t.Errorf("Expected property name to have value Rick Grimes, Got: %v", - child.Properties["name"].Str) + getProperty(child.Properties, "name").Str) } if len(child.Children) != 0 { t.Errorf("Expected 0 children, Got: %v", len(child.Children)) -- GitLab