Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite server code of channel sorting #1064

Merged
merged 1 commit into from Apr 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 20 additions & 24 deletions src/client.js
Expand Up @@ -412,44 +412,40 @@ Client.prototype.open = function(socketId, target) {
};

Client.prototype.sort = function(data) {
var self = this;
const order = data.order;

var type = data.type;
var order = data.order || [];
var sorted = [];
if (!_.isArray(order)) {
return;
}

switch (type) {
switch (data.type) {
case "networks":
order.forEach(i => {
var find = _.find(self.networks, {id: i});
if (find) {
sorted.push(find);
}
this.networks.sort((a, b) => {
return order.indexOf(a.id) > order.indexOf(b.id);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming about Node's (unstable) sorting algorithm, this is at best O(n² * log n). Some people hang out in easily dozens of channels: 50 channels and that's 14k operations, 100 channels is 64k operations, etc.

I know this is not worse than it was before (I think), but do you think there is any way we can improve that? The user provides the order themselves, so in my understanding we shouldn't have to do a full sort, instead loop through the given order (which would be O(n²), not much better, surely there is a better way...)

Also, when is this needed exactly? When a user reorders their channels/networks on the UI?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't rely entirely on user given input (will have to filter it to exactly contain every channel ID on the network, missing channels have to be added, invalid ones removed. This was the simplest approach.

This could be improved by modifying order into a hash map so that no indexOf calls are required.

This method is entirely just for the UI sorting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this can be done later though, your fix is a good thing anyway.

self.networks = sorted;

// Sync order to connected clients
this.emit("sync_sort", {order: this.networks.map(obj => obj.id), type: data.type, target: data.target});

break;

case "channels":
var target = data.target;
var network = _.find(self.networks, {id: target});
var network = _.find(this.networks, {id: data.target});
if (!network) {
return;
}
order.forEach(i => {
var find = _.find(network.channels, {id: i});
if (find) {
sorted.push(find);
}

network.channels.sort((a, b) => {
return order.indexOf(a.id) > order.indexOf(b.id);
});
network.channels = sorted;

// Sync order to connected clients
this.emit("sync_sort", {order: network.channels.map(obj => obj.id), type: data.type, target: data.target});

break;
}

self.save();

// Sync order to connected clients
const syncOrder = sorted.map(obj => obj.id);
self.emit("sync_sort", {order: syncOrder, type: type, target: data.target});
this.save();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific reason why you separated the event emit and file save?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing specific, just didn't want to keep the variable.

};

Client.prototype.names = function(data) {
Expand Down