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
Conversation
Fixes server crash, fixes losing channels
sorted.push(find); | ||
} | ||
this.networks.sort((a, b) => { | ||
return order.indexOf(a.id) > order.indexOf(b.id); | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Rewrite server code of channel sorting
Sending malformed
order
would crash the server. Sending incompleteorder
would lose references to channels or networks.