Performance optimizations

This commit is contained in:
Till Faelligen 2023-01-04 11:13:46 +01:00
parent e449d174cc
commit ccb4fe1dd4
No known key found for this signature in database
GPG key ID: ACCDC9606D472758

View file

@ -384,19 +384,32 @@ func applyHistoryVisibilityFilter(
roomID, userID string, roomID, userID string,
recentEvents []*gomatrixserverlib.HeaderedEvent, recentEvents []*gomatrixserverlib.HeaderedEvent,
) ([]*gomatrixserverlib.HeaderedEvent, error) { ) ([]*gomatrixserverlib.HeaderedEvent, error) {
// We need to make sure we always include the latest states events, if they are in the timeline. // We need to make sure we always include the latest state events, if they are in the timeline.
// We grep at least limit * 2 events, to ensure we really get the needed events. alwaysIncludeIDs := make(map[string]struct{})
filter := gomatrixserverlib.DefaultStateFilter() var stateTypes []string
stateEvents, err := snapshot.CurrentState(ctx, roomID, &filter, nil) var senders []string
if err != nil { for _, ev := range recentEvents {
// Not a fatal error, we can continue without the stateEvents, if ev.StateKey() != nil {
// they are only needed if there are state events in the timeline. stateTypes = append(stateTypes, ev.Type())
logrus.WithError(err).Warnf("Failed to get current room state for history visibility") senders = append(senders, ev.Sender())
}
} }
alwaysIncludeIDs := make(map[string]struct{}, len(stateEvents))
for _, ev := range stateEvents { // Only get the state again if there are state events in the timeline
alwaysIncludeIDs[ev.EventID()] = struct{}{} if len(stateTypes) > 0 {
filter := gomatrixserverlib.DefaultStateFilter()
filter.Types = &stateTypes
filter.Senders = &senders
stateEvents, err := snapshot.CurrentState(ctx, roomID, &filter, nil)
if err != nil {
return nil, fmt.Errorf("failed to get current room state for history visibility calculation: %w", err)
}
for _, ev := range stateEvents {
alwaysIncludeIDs[ev.EventID()] = struct{}{}
}
} }
startTime := time.Now() startTime := time.Now()
events, err := internal.ApplyHistoryVisibilityFilter(ctx, snapshot, rsAPI, recentEvents, alwaysIncludeIDs, userID, "sync") events, err := internal.ApplyHistoryVisibilityFilter(ctx, snapshot, rsAPI, recentEvents, alwaysIncludeIDs, userID, "sync")
if err != nil { if err != nil {