Skip to content

Commit

Permalink
Remove profiling in reader.Check (#115)
Browse files Browse the repository at this point in the history
* Remove profiling in reader.Check

* Use NewSlicePool from azm/mempool
  • Loading branch information
ronenh authored Dec 11, 2024
1 parent 107dd7f commit 548e833
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 0 additions & 3 deletions pkg/directory/v3/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
48 changes: 21 additions & 27 deletions pkg/ds/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
},
}

0 comments on commit 548e833

Please sign in to comment.