Skip to content

Commit

Permalink
Pre-allocate ops slices that are appended in a loop where possible. (#…
Browse files Browse the repository at this point in the history
…743)

This ensures that only one larger allocation is done instead of multiple
small ones, reducing pressure on the garbage collector.
  • Loading branch information
bnewbold committed Sep 8, 2024
2 parents f9efd8c + 3d2f28e commit 5ea1281
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backfill/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (bf *Backfiller) HandleEvent(ctx context.Context, evt *atproto.SyncSubscrib
return fmt.Errorf("failed to read event repo: %w", err)
}

var ops []*BufferedOp
ops := make([]*BufferedOp, 0, len(evt.Ops))
for _, op := range evt.Ops {
kind := repomgr.EventKind(op.Action)
switch kind {
Expand Down
6 changes: 2 additions & 4 deletions cmd/supercollider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func initSpeedyRepoMan(key *godid.PrivKey) (*repomgr.RepoManager, *godid.PrivKey

// HandleRepoEvent is the callback for the RepoManager
func (s *Server) HandleRepoEvent(ctx context.Context, evt *repomgr.RepoEvent) {
var outops []*comatproto.SyncSubscribeRepos_RepoOp
outops := make([]*comatproto.SyncSubscribeRepos_RepoOp, 0, len(evt.Ops))
for _, op := range evt.Ops {
link := (*lexutil.LexLink)(op.RecCid)
outops = append(outops, &comatproto.SyncSubscribeRepos_RepoOp{
Expand All @@ -596,8 +596,6 @@ func (s *Server) HandleRepoEvent(ctx context.Context, evt *repomgr.RepoEvent) {
})
}

toobig := false

if err := s.Events.AddEvent(ctx, &events.XRPCStreamEvent{
RepoCommit: &comatproto.SyncSubscribeRepos_Commit{
Repo: s.Dids[evt.User-1],
Expand All @@ -606,7 +604,7 @@ func (s *Server) HandleRepoEvent(ctx context.Context, evt *repomgr.RepoEvent) {
Commit: lexutil.LexLink(evt.NewRoot),
Time: time.Now().Format(util.ISO8601),
Ops: outops,
TooBig: toobig,
TooBig: false,
},
PrivUid: evt.User,
}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (ix *Indexer) HandleRepoEvent(ctx context.Context, evt *repomgr.RepoEvent)

log.Debugw("Handling Repo Event!", "uid", evt.User)

var outops []*comatproto.SyncSubscribeRepos_RepoOp
outops := make([]*comatproto.SyncSubscribeRepos_RepoOp, 0, len(evt.Ops))
for _, op := range evt.Ops {
link := (*lexutil.LexLink)(op.RecCid)
outops = append(outops, &comatproto.SyncSubscribeRepos_RepoOp{
Expand Down
6 changes: 3 additions & 3 deletions repomgr/repomgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func (rm *RepoManager) HandleExternalUserEvent(ctx context.Context, pdsid uint,

}

var evtops []RepoOp
evtops := make([]RepoOp, 0, len(ops))

for _, op := range ops {
parts := strings.SplitN(op.Path, "/", 2)
Expand Down Expand Up @@ -679,7 +679,7 @@ func (rm *RepoManager) BatchWrite(ctx context.Context, user models.Uid, writes [
return err
}

var ops []RepoOp
ops := make([]RepoOp, 0, len(writes))
for _, w := range writes {
switch {
case w.RepoApplyWrites_Create != nil:
Expand Down Expand Up @@ -826,7 +826,7 @@ func (rm *RepoManager) ImportNewRepo(ctx context.Context, user models.Uid, repoD
return fmt.Errorf("diff trees (curhead: %s): %w", curhead, err)
}

var ops []RepoOp
ops := make([]RepoOp, 0, len(diffops))
for _, op := range diffops {
repoOpsImported.Inc()
out, err := processOp(ctx, bs, op, rm.hydrateRecords)
Expand Down

0 comments on commit 5ea1281

Please sign in to comment.