Skip to content

Commit

Permalink
Merge pull request #1419 from matrix-org/travis/general/perf/js-rooms…
Browse files Browse the repository at this point in the history
…tate-map

[BREAKING] Convert RoomState's stored state map to a real map
  • Loading branch information
turt2live authored Jul 16, 2020
2 parents 2a688bd + 0af7632 commit 7d398d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
9 changes: 5 additions & 4 deletions examples/node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function printMemberList(room) {
}

function printRoomInfo(room) {
var eventDict = room.currentState.events;
var eventMap = room.currentState.events;
var eTypeHeader = " Event Type(state_key) ";
var sendHeader = " Sender ";
// pad content to 100
Expand All @@ -300,14 +300,15 @@ function printRoomInfo(room) {
var contentHeader = padSide + "Content" + padSide;
print(eTypeHeader+sendHeader+contentHeader);
print(new Array(100).join("-"));
Object.keys(eventDict).forEach(function(eventType) {
eventMap.keys().forEach(function(eventType) {
if (eventType === "m.room.member") { return; } // use /members instead.
Object.keys(eventDict[eventType]).forEach(function(stateKey) {
var eventEventMap = eventMap.get(eventType);
eventEventMap.keys().forEach(function(stateKey) {
var typeAndKey = eventType + (
stateKey.length > 0 ? "("+stateKey+")" : ""
);
var typeStr = fixWidth(typeAndKey, eTypeHeader.length);
var event = eventDict[eventType][stateKey];
var event = eventEventMap.get(stateKey);
var sendStr = fixWidth(event.getSender(), sendHeader.length);
var contentStr = fixWidth(
JSON.stringify(event.getContent()), contentHeader.length
Expand Down
29 changes: 13 additions & 16 deletions src/models/room-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ export function RoomState(roomId, oobMemberFlags = undefined) {
this.members = {
// userId: RoomMember
};
this.events = {
// eventType: { stateKey: MatrixEvent }
};
this.events = new Map(); // Map<eventType, Map<stateKey, MatrixEvent>>
this.paginationToken = null;

this._sentinels = {
Expand Down Expand Up @@ -211,14 +209,14 @@ RoomState.prototype.getSentinelMember = function(userId) {
* <code>undefined</code>, else a single event (or null if no match found).
*/
RoomState.prototype.getStateEvents = function(eventType, stateKey) {
if (!this.events[eventType]) {
if (!this.events.has(eventType)) {
// no match
return stateKey === undefined ? [] : null;
}
if (stateKey === undefined) { // return all values
return utils.values(this.events[eventType]);
return Array.from(this.events.get(eventType).values());
}
const event = this.events[eventType][stateKey];
const event = this.events.get(eventType).get(stateKey);
return event ? event : null;
};

Expand All @@ -238,9 +236,8 @@ RoomState.prototype.clone = function() {
const status = this._oobMemberFlags.status;
this._oobMemberFlags.status = OOB_STATUS_NOTSTARTED;

Object.values(this.events).forEach((eventsByStateKey) => {
const eventsForType = Object.values(eventsByStateKey);
copy.setStateEvents(eventsForType);
Array.from(this.events.values()).forEach((eventsByStateKey) => {
copy.setStateEvents(Array.from(eventsByStateKey.values()));
});

// Ugly hack: see above
Expand Down Expand Up @@ -276,8 +273,8 @@ RoomState.prototype.clone = function() {
*/
RoomState.prototype.setUnknownStateEvents = function(events) {
const unknownStateEvents = events.filter((event) => {
return this.events[event.getType()] === undefined ||
this.events[event.getType()][event.getStateKey()] === undefined;
return !this.events.has(event.getType()) ||
!this.events.get(event.getType()).has(event.getStateKey());
});

this.setStateEvents(unknownStateEvents);
Expand Down Expand Up @@ -386,15 +383,15 @@ RoomState.prototype._getOrCreateMember = function(userId, event) {
};

RoomState.prototype._setStateEvent = function(event) {
if (this.events[event.getType()] === undefined) {
this.events[event.getType()] = {};
if (!this.events.has(event.getType())) {
this.events.set(event.getType(), new Map());
}
this.events[event.getType()][event.getStateKey()] = event;
this.events.get(event.getType()).set(event.getStateKey(), event);
};

RoomState.prototype._getStateEventMatching = function(event) {
if (!this.events[event.getType()]) return null;
return this.events[event.getType()][event.getStateKey()];
if (!this.events.has(event.getType())) return null;
return this.events.get(event.getType()).get(event.getStateKey());
};

RoomState.prototype._updateMember = function(member) {
Expand Down

0 comments on commit 7d398d4

Please sign in to comment.