From 548e8338c18fca5564b0462e0f35936f20bdf3cf Mon Sep 17 00:00:00 2001 From: Ronen Hilewicz Date: Wed, 11 Dec 2024 08:47:34 -0500 Subject: [PATCH] Remove profiling in reader.Check (#115) * Remove profiling in reader.Check * Use NewSlicePool from azm/mempool --- go.mod | 4 ++-- go.sum | 2 ++ pkg/directory/v3/reader.go | 3 --- pkg/ds/relation.go | 48 +++++++++++++++++--------------------- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 56b51b3..da6b36a 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,14 @@ go 1.22.10 toolchain go1.23.4 -replace github.com/aserto-dev/azm => ../azm +// replace github.com/aserto-dev/azm => ../azm // replace github.com/aserto-dev/go-directory => ../go-directory require ( github.com/Masterminds/semver/v3 v3.3.1 github.com/aserto-dev/aserto-grpc v0.2.6 - github.com/aserto-dev/azm v0.2.2-0.20241210214915-de60dbb1c83b + github.com/aserto-dev/azm v0.2.2-0.20241210235619-33ab9961b573 github.com/aserto-dev/errors v0.0.11 github.com/aserto-dev/go-directory v0.33.2-0.20241210213355-3d02dab1a2bd github.com/bufbuild/protovalidate-go v0.7.3 diff --git a/go.sum b/go.sum index a452941..b6b8364 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYW github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/aserto-dev/aserto-grpc v0.2.6 h1:h64MYALF5zLm2sSKcLEtyXyrJvZSxfqTOmQ1j/J44kI= github.com/aserto-dev/aserto-grpc v0.2.6/go.mod h1:Vki74KINVfnwtJ8QGzRm+xHNjsJ2KUWFtXhezJK9DEg= +github.com/aserto-dev/azm v0.2.2-0.20241210235619-33ab9961b573 h1:0FLVhcXmAFT1NcMzGjwtJxSXgSuSp5/wwp1NG6EVieI= +github.com/aserto-dev/azm v0.2.2-0.20241210235619-33ab9961b573/go.mod h1:4bU8iz8b4ZC4IuoghLXC4S2TThu9eJwbAfVxosVznLs= github.com/aserto-dev/errors v0.0.11 h1:CXo+Uwmh09doG2HvL1SC8Fnne8f9VPrGyEQPtogAfyY= github.com/aserto-dev/errors v0.0.11/go.mod h1:T1YQOtcxpgBriPTn5HXJkD/QukYz5YojYOIzGMo0ybM= github.com/aserto-dev/go-directory v0.33.2-0.20241210213355-3d02dab1a2bd h1:WOEcH7FURcfZBrQ5GKCsNXcFhqIqhwVQdA2v0JNgI0A= diff --git a/pkg/directory/v3/reader.go b/pkg/directory/v3/reader.go index f1e1e2e..48ed49f 100644 --- a/pkg/directory/v3/reader.go +++ b/pkg/directory/v3/reader.go @@ -9,7 +9,6 @@ import ( "github.com/aserto-dev/go-edge-ds/pkg/bdb" "github.com/aserto-dev/go-edge-ds/pkg/ds" "github.com/pkg/errors" - "github.com/pkg/profile" "github.com/bufbuild/protovalidate-go" "github.com/go-http-utils/headers" @@ -317,8 +316,6 @@ func (s *Reader) GetRelations(ctx context.Context, req *dsr3.GetRelationsRequest // Check, if subject is permitted to access resource (object). func (s *Reader) Check(ctx context.Context, req *dsr3.CheckRequest) (*dsr3.CheckResponse, error) { - defer profile.Start(profile.TraceProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop() - resp := &dsr3.CheckResponse{} if err := s.Validate(req); err != nil { diff --git a/pkg/ds/relation.go b/pkg/ds/relation.go index 87972cd..c8a0ad3 100644 --- a/pkg/ds/relation.go +++ b/pkg/ds/relation.go @@ -5,8 +5,8 @@ package ds import ( "bytes" "strings" - "sync" + "github.com/aserto-dev/azm/mempool" "github.com/aserto-dev/azm/safe" dsc3 "github.com/aserto-dev/go-directory/aserto/directory/common/v3" dsr3 "github.com/aserto-dev/go-directory/aserto/directory/reader/v3" @@ -15,6 +15,10 @@ import ( "github.com/rs/zerolog/log" ) +const maxRelationSize = 832 + +var bufPool = mempool.NewSlicePool[byte](maxRelationSize) + // Relation identifier. type relation struct { *safe.SafeRelation @@ -56,11 +60,10 @@ func (i *relation) Key() []byte { } func (i *relation) ObjKey() []byte { - buf := bytes.NewBuffer(bufPool.Get().([]byte)) - defer func() { - buf.Reset() - bufPool.Put(buf.Bytes()) - }() + ptr := bufPool.Get() + defer bufPool.Put(ptr) + + buf := bytes.NewBuffer(*ptr) buf.WriteString(i.GetObjectType()) buf.WriteByte(TypeIDSeparator) @@ -83,11 +86,10 @@ func (i *relation) ObjKey() []byte { } func (i *relation) SubKey() []byte { - buf := bytes.NewBuffer(bufPool.Get().([]byte)) - defer func() { - buf.Reset() - bufPool.Put(buf.Bytes()) - }() + ptr := bufPool.Get() + defer bufPool.Put(ptr) + + buf := bytes.NewBuffer(*ptr) buf.WriteString(i.GetSubjectType()) buf.WriteByte(TypeIDSeparator) @@ -124,11 +126,10 @@ func (i *relation) PathAndFilter() ([]string, []byte, error) { // format: obj_type : obj_id # relation @ sub_type : sub_id (# sub_relation). // TODO: if subject relation exists add subject relation to filter clause. func (i *relation) ObjFilter() []byte { - buf := bytes.NewBuffer(bufPool.Get().([]byte)) - defer func() { - buf.Reset() - bufPool.Put(buf.Bytes()) - }() + ptr := bufPool.Get() + defer bufPool.Put(ptr) + + buf := bytes.NewBuffer(*ptr) buf.WriteString(i.GetObjectType()) buf.WriteByte(TypeIDSeparator) @@ -162,11 +163,10 @@ func (i *relation) ObjFilter() []byte { // format: sub_type : sub_id (# sub_relation) | obj_type : obj_id # relation. // TODO: if subject relation exists add subject relation to filter clause. func (i *relation) SubFilter() []byte { - buf := bytes.NewBuffer(bufPool.Get().([]byte)) - defer func() { - buf.Reset() - bufPool.Put(buf.Bytes()) - }() + ptr := bufPool.Get() + defer bufPool.Put(ptr) + + buf := bytes.NewBuffer(*ptr) buf.WriteString(i.GetSubjectType()) buf.WriteByte(TypeIDSeparator) @@ -363,9 +363,3 @@ func (i *relation) RelationValueFilter() (path bdb.Path, keyFilter []byte, value return path, keyFilter, valueFilter } - -var bufPool = sync.Pool{ - New: func() interface{} { - return make([]byte, 0, 832) - }, -}