0
0
Fork 0
mirror of https://github.com/matrix-org/dendrite synced 2024-12-15 10:53:46 +01:00

Try to reduce re-allocations a bit in resolveConflictsV2

This commit is contained in:
Neil Alexander 2021-11-04 10:50:52 +00:00
parent 83e4a67194
commit 665ca9c6cc
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -778,7 +778,8 @@ func (v *StateResolution) resolveConflictsV2(
ctx context.Context, ctx context.Context,
notConflicted, conflicted []types.StateEntry, notConflicted, conflicted []types.StateEntry,
) ([]types.StateEntry, error) { ) ([]types.StateEntry, error) {
eventIDMap := make(map[string]types.StateEntry) estimate := len(conflicted) + len(notConflicted)
eventIDMap := make(map[string]types.StateEntry, estimate)
// Load the conflicted events // Load the conflicted events
conflictedEvents, conflictedEventMap, err := v.loadStateEvents(ctx, conflicted) conflictedEvents, conflictedEventMap, err := v.loadStateEvents(ctx, conflicted)
@ -800,18 +801,20 @@ func (v *StateResolution) resolveConflictsV2(
// For each conflicted event, we will add a new set of auth events. Auth // For each conflicted event, we will add a new set of auth events. Auth
// events may be duplicated across these sets but that's OK. // events may be duplicated across these sets but that's OK.
authSets := make(map[string][]*gomatrixserverlib.Event) authSets := make(map[string][]*gomatrixserverlib.Event, len(conflicted))
var authEvents []*gomatrixserverlib.Event authEvents := make([]*gomatrixserverlib.Event, 0, estimate*3)
var authDifference []*gomatrixserverlib.Event authDifference := make([]*gomatrixserverlib.Event, 0, estimate)
// For each conflicted event, let's try and get the needed auth events. // For each conflicted event, let's try and get the needed auth events.
neededStateKeys := make([]string, 16)
authEntries := make([]types.StateEntry, 16)
for _, conflictedEvent := range conflictedEvents { for _, conflictedEvent := range conflictedEvents {
// Work out which auth events we need to load. // Work out which auth events we need to load.
key := conflictedEvent.EventID() key := conflictedEvent.EventID()
needed := gomatrixserverlib.StateNeededForAuth([]*gomatrixserverlib.Event{conflictedEvent}) needed := gomatrixserverlib.StateNeededForAuth([]*gomatrixserverlib.Event{conflictedEvent})
// Find the numeric IDs for the necessary state keys. // Find the numeric IDs for the necessary state keys.
var neededStateKeys []string neededStateKeys = neededStateKeys[:0]
neededStateKeys = append(neededStateKeys, needed.Member...) neededStateKeys = append(neededStateKeys, needed.Member...)
neededStateKeys = append(neededStateKeys, needed.ThirdPartyInvite...) neededStateKeys = append(neededStateKeys, needed.ThirdPartyInvite...)
stateKeyNIDMap, err := v.db.EventStateKeyNIDs(ctx, neededStateKeys) stateKeyNIDMap, err := v.db.EventStateKeyNIDs(ctx, neededStateKeys)
@ -821,7 +824,7 @@ func (v *StateResolution) resolveConflictsV2(
// Load the necessary auth events. // Load the necessary auth events.
tuplesNeeded := v.stateKeyTuplesNeeded(stateKeyNIDMap, needed) tuplesNeeded := v.stateKeyTuplesNeeded(stateKeyNIDMap, needed)
var authEntries []types.StateEntry authEntries = authEntries[:0]
for _, tuple := range tuplesNeeded { for _, tuple := range tuplesNeeded {
if eventNID, ok := stateEntryMap(notConflicted).lookup(tuple); ok { if eventNID, ok := stateEntryMap(notConflicted).lookup(tuple); ok {
authEntries = append(authEntries, types.StateEntry{ authEntries = append(authEntries, types.StateEntry{