Skip to content

Commit

Permalink
reduces chunking allocations for wide relations
Browse files Browse the repository at this point in the history
For most situations, when the number of elements to dispatch
is below the default 100 elements, the slice with elements
to dispatch was appropriately pre-allocated.

However, when dispatching large number of elements, the
silence will grow from 1 up to the number of chunks, causing
wasteful allocations in the critical path.

To simplify the logic, +1 is added to account for the situation
the number of elements is less than the chunk size. For values
above that it will cause one excess slice entry allocation, but seems
like a reasonable tradeoff w.r.t extra annoying code to handle it.
  • Loading branch information
vroldanbet committed Feb 21, 2024
1 parent 380da03 commit 31defe5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions internal/graph/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ func (cc *ConcurrentChecker) checkDirect(ctx context.Context, crc currentRequest
it.Close()

// Convert the subjects into batched requests.
toDispatch := make([]directDispatch, 0, subjectsToDispatch.Len())
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 +602,8 @@ func (cc *ConcurrentChecker) checkTupleToUserset(ctx context.Context, crc curren
it.Close()

// Convert the subjects into batched requests.
toDispatch := make([]directDispatch, 0, subjectsToDispatch.Len())
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 31defe5

Please sign in to comment.