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

Rewrite server code of channel sorting #1064

merged 1 commit into from Apr 23, 2017

Conversation

xPaw
Copy link
Member

@xPaw xPaw commented Apr 22, 2017

Sending malformed order would crash the server. Sending incomplete order would lose references to channels or networks.

@xPaw xPaw added Type: Bug Issues that report and PRs that solve any defects that cause unexpected behaviors. Type: Security Security concern or PRs that must be reviewed with extra care regarding security. labels Apr 22, 2017
@xPaw xPaw added this to the 2.3.0 milestone Apr 22, 2017
Fixes server crash, fixes losing channels
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.

// 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.

@astorije astorije merged commit 30bf20e into master Apr 23, 2017
@astorije astorije deleted the xpaw/harden-sort branch April 23, 2017 18:12
matburnham pushed a commit to matburnham/lounge that referenced this pull request Sep 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issues that report and PRs that solve any defects that cause unexpected behaviors. Type: Security Security concern or PRs that must be reviewed with extra care regarding security.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants