From f213901e29721283639817350d8375d7c8e92658 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 22 Jan 2016 11:11:56 +0000 Subject: [PATCH 1/2] Return early when calculating one-to-one room avatars Object.keys() is O(n) and is wasted because we only care if the keys are 1 or 2. Use `for .. in` instead and return early if there are >2 keys. Profiling indicates this cuts wasted time from ~74ms to ~32ms for me (who has a large number of rooms with large numbers of people in them). --- src/components/views/avatars/RoomAvatar.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/components/views/avatars/RoomAvatar.js b/src/components/views/avatars/RoomAvatar.js index 72ca5f7f21..eed4070fd8 100644 --- a/src/components/views/avatars/RoomAvatar.js +++ b/src/components/views/avatars/RoomAvatar.js @@ -67,14 +67,24 @@ module.exports = React.createClass({ }, getOneToOneAvatar: function(props) { - var userIds = Object.keys(props.room.currentState.members); + var mlist = props.room.currentState.members; + var userIds = []; + // for .. in optimisation to return early if there are >2 keys + for (var uid in mlist) { + if (mlist.hasOwnProperty(uid)) { + userIds.push(uid); + } + if (userIds.length > 2) { + return null; + } + } if (userIds.length == 2) { var theOtherGuy = null; - if (props.room.currentState.members[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) { - theOtherGuy = props.room.currentState.members[userIds[1]]; + if (mlist[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) { + theOtherGuy = mlist[userIds[1]]; } else { - theOtherGuy = props.room.currentState.members[userIds[0]]; + theOtherGuy = mlist[userIds[0]]; } return theOtherGuy.getAvatarUrl( MatrixClientPeg.get().getHomeserverUrl(), @@ -82,7 +92,7 @@ module.exports = React.createClass({ false ); } else if (userIds.length == 1) { - return props.room.currentState.members[userIds[0]].getAvatarUrl( + return mlist[userIds[0]].getAvatarUrl( MatrixClientPeg.get().getHomeserverUrl(), props.width, props.height, props.resizeMethod, false From 7ef1ba0b44e0e2c73e4e30af99c27932fd05ebe1 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 22 Jan 2016 11:48:26 +0000 Subject: [PATCH 2/2] Unbreak RoomHeader --- src/components/views/rooms/RoomHeader.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/components/views/rooms/RoomHeader.js b/src/components/views/rooms/RoomHeader.js index 6fab2a9989..0b52281507 100644 --- a/src/components/views/rooms/RoomHeader.js +++ b/src/components/views/rooms/RoomHeader.js @@ -20,6 +20,7 @@ var React = require('react'); var sdk = require('../../../index'); var dis = require("../../../dispatcher"); var MatrixClientPeg = require('../../../MatrixClientPeg'); +var Modal = require("../../../Modal"); var linkify = require('linkifyjs'); var linkifyElement = require('linkifyjs/element'); @@ -103,17 +104,14 @@ module.exports = React.createClass({ console.error("No ChangeAvatar found to upload image to!"); return; } - changeAvatar.onFileSelected(ev).done(function() { - // dunno if the avatar changed, re-check it. - self._refreshFromServer(); - }, function(err) { + changeAvatar.onFileSelected(ev).catch(function(err) { var errMsg = (typeof err === "string") ? err : (err.error || ""); var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); Modal.createDialog(ErrorDialog, { title: "Error", description: "Failed to set avatar. " + errMsg }); - }); + }).done(); }, getRoomName: function() {