Skip to content

Commit

Permalink
Merge pull request #1751 from authzed/reduce-allocations-for-wide-rel…
Browse files Browse the repository at this point in the history
…ations

reduces chunking allocations for wide relations
  • Loading branch information
vroldanbet authored Feb 23, 2024
2 parents 2ebd9fc + 1d46c13 commit ba2547f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 8 additions & 2 deletions internal/graph/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ func (cc *ConcurrentChecker) checkDirect(ctx context.Context, crc currentRequest
it.Close()

// Convert the subjects into batched requests.
toDispatch := make([]directDispatch, 0, subjectsToDispatch.Len())
// To simplify the logic, +1 is added to account for the situation where
// the number of elements is less than the chunk size, and spare us some annoying code.
expectedNumberOfChunks := subjectsToDispatch.ValueLen()/int(crc.maxDispatchCount) + 1
toDispatch := make([]directDispatch, 0, expectedNumberOfChunks)
subjectsToDispatch.ForEachType(func(rr *core.RelationReference, resourceIds []string) {
chunkCount := 0.0
slicez.ForEachChunk(resourceIds, crc.maxDispatchCount, func(resourceIdChunk []string) {
Expand Down Expand Up @@ -601,7 +604,10 @@ func (cc *ConcurrentChecker) checkTupleToUserset(ctx context.Context, crc curren
it.Close()

// Convert the subjects into batched requests.
toDispatch := make([]directDispatch, 0, subjectsToDispatch.Len())
// To simplify the logic, +1 is added to account for the situation where
// the number of elements is less than the chunk size, and spare us some annoying code.
expectedNumberOfChunks := subjectsToDispatch.ValueLen()/int(crc.maxDispatchCount) + 1
toDispatch := make([]directDispatch, 0, expectedNumberOfChunks)
subjectsToDispatch.ForEachType(func(rr *core.RelationReference, resourceIds []string) {
chunkCount := 0.0
slicez.ForEachChunk(resourceIds, crc.maxDispatchCount, func(resourceIdChunk []string) {
Expand Down
14 changes: 12 additions & 2 deletions pkg/tuple/onrbytypeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ func (s *ONRByTypeSet) IsEmpty() bool {
return len(s.byType) == 0
}

// Len returns the number of keys in the set.
func (s *ONRByTypeSet) Len() int {
// KeyLen returns the number of keys in the set.
func (s *ONRByTypeSet) KeyLen() int {
return len(s.byType)
}

// ValueLen returns the number of values in the set.
func (s *ONRByTypeSet) ValueLen() int {
var total int
for _, vals := range s.byType {
total += len(vals)
}

return total
}

0 comments on commit ba2547f

Please sign in to comment.