mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-29 08:18:27 +00:00
Refactor removeDuplicates (might use slightly more memory but less CPU)
This commit is contained in:
parent
dd781907b1
commit
0096fc850a
2 changed files with 13 additions and 34 deletions
|
@ -64,7 +64,6 @@ type filterResponse struct {
|
||||||
FilterID string `json:"filter_id"`
|
FilterID string `json:"filter_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: asdf
|
|
||||||
//PutFilter implements POST /_matrix/client/r0/user/{userId}/filter
|
//PutFilter implements POST /_matrix/client/r0/user/{userId}/filter
|
||||||
func PutFilter(
|
func PutFilter(
|
||||||
req *http.Request, device *api.Device, syncDB storage.Database, userID string,
|
req *http.Request, device *api.Device, syncDB storage.Database, userID string,
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/matrix-org/dendrite/syncapi/types"
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
userapi "github.com/matrix-org/dendrite/userapi/api"
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PDUStreamProvider struct {
|
type PDUStreamProvider struct {
|
||||||
|
@ -103,7 +102,6 @@ func (p *PDUStreamProvider) CompleteSync(
|
||||||
req.Log.WithError(err).Error("p.getLeaveResponseForCompleteSync failed")
|
req.Log.WithError(err).Error("p.getLeaveResponseForCompleteSync failed")
|
||||||
return from
|
return from
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Response.Rooms.Leave[roomID] = *lr
|
req.Response.Rooms.Leave[roomID] = *lr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -347,9 +345,6 @@ func (p *PDUStreamProvider) filterStreamEventsAccordingToHistoryVisibility(
|
||||||
}
|
}
|
||||||
switch content.HistoryVisibility {
|
switch content.HistoryVisibility {
|
||||||
case "world_readable", "shared":
|
case "world_readable", "shared":
|
||||||
logrus.WithFields(logrus.Fields{
|
|
||||||
"content.HistoryVisibility": content.HistoryVisibility,
|
|
||||||
}).Info("filterStreamEventsAccordingToHistoryVisibility: No filtering needed for these events")
|
|
||||||
return recentStreamEvents, limited
|
return recentStreamEvents, limited
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
|
@ -399,38 +394,23 @@ func (p *PDUStreamProvider) filterStreamEventsAccordingToHistoryVisibility(
|
||||||
sliceEnd = leaveEventIndex + 1
|
sliceEnd = leaveEventIndex + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
events := make([]string, len(recentStreamEvents))
|
|
||||||
for i, v := range recentStreamEvents {
|
|
||||||
events[i] = string(v.HeaderedEvent.Event.JSON())
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.WithFields(logrus.Fields{
|
|
||||||
"sliceStart": sliceStart,
|
|
||||||
"sliceEnd": sliceEnd,
|
|
||||||
"before recentStreamEvents": fmt.Sprintf("%+v", events),
|
|
||||||
}).Info("filterStreamEventsAccordingToHistoryVisibility: cutting down the events")
|
|
||||||
|
|
||||||
return recentStreamEvents[sliceStart:sliceEnd], limited
|
return recentStreamEvents[sliceStart:sliceEnd], limited
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeDuplicates(stateEvents, recentEvents []*gomatrixserverlib.HeaderedEvent) []*gomatrixserverlib.HeaderedEvent {
|
func removeDuplicates(stateEvents, recentEvents []*gomatrixserverlib.HeaderedEvent) []*gomatrixserverlib.HeaderedEvent {
|
||||||
for _, recentEv := range recentEvents {
|
timeline := map[string]struct{}{}
|
||||||
if recentEv.StateKey() == nil {
|
for _, event := range recentEvents {
|
||||||
continue // not a state event
|
if event.StateKey() == nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
// TODO: This is a linear scan over all the current state events in this room. This will
|
timeline[event.EventID()] = struct{}{}
|
||||||
// be slow for big rooms. We should instead sort the state events by event ID (ORDER BY)
|
|
||||||
// then do a binary search to find matching events, similar to what roomserver does.
|
|
||||||
for j := 0; j < len(stateEvents); j++ {
|
|
||||||
if stateEvents[j].EventID() == recentEv.EventID() {
|
|
||||||
// overwrite the element to remove with the last element then pop the last element.
|
|
||||||
// This is orders of magnitude faster than re-slicing, but doesn't preserve ordering
|
|
||||||
// (we don't care about the order of stateEvents)
|
|
||||||
stateEvents[j] = stateEvents[len(stateEvents)-1]
|
|
||||||
stateEvents = stateEvents[:len(stateEvents)-1]
|
|
||||||
break // there shouldn't be multiple events with the same event ID
|
|
||||||
}
|
}
|
||||||
|
state := []*gomatrixserverlib.HeaderedEvent{}
|
||||||
|
for _, event := range stateEvents {
|
||||||
|
if _, ok := timeline[event.EventID()]; ok {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
state = append(state, event)
|
||||||
}
|
}
|
||||||
return stateEvents
|
return state
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue