Skip to content

Faster channel lookup#1979

Merged
softins merged 5 commits intojamulussoftware:masterfrom
softins:faster-channel-lookup
Sep 1, 2021
Merged

Faster channel lookup#1979
softins merged 5 commits intojamulussoftware:masterfrom
softins:faster-channel-lookup

Conversation

@softins
Copy link
Copy Markdown
Member

@softins softins commented Aug 26, 2021

Improve the performance of FindChannel(), using binary search instead of linear.

Context: Fixes an issue?

Every received audio or connected protocol packet needs to call FindChannel() to find the channel ID from
the incoming IP address and port number. Currently, FindChannel() uses a simple linear search, which becomes
very inefficient when there is a large number of active channels. Later connections will always need to search
most of the vecChannels[] array to find the matching channel.

This PR introduces a channel order array vecChannelOrder[], which lists the active channel IDs in sorted order.
FindChannel() can then use a binary search to locate the channel. For 100 active channels, this would then need on average
about 6 or 7 comparisons instead of an average of 50 or more for a linear search.

A function FreeChannel() is also introduced to return a disconnected channel to the free list. Also a separate Mutex to protect the channel order array.

Does this change need documentation? What needs to be documented and how?

No documentation - just a performance enhancement.

Status of this Pull Request

Working and tested with up to 25 channels, showing a small reduction in CPU usage. The performance improvement would likely be more significant with a larger number of channels. But in any case, the improved algorithm is worth having.

What is missing until this pull request can be merged?

More testing is always good, but I am confident in the algorithm.

Checklist

  • I've verified that this Pull Request follows the general code principles
  • I tested my code and it does what I want
  • My code follows the style guide
  • I waited some time after this Pull Request was opened and all GitHub checks completed without errors.
  • I've filled all the content above

@softins softins force-pushed the faster-channel-lookup branch from d0a589d to 98d4d6c Compare August 26, 2021 22:13
Comment thread src/server.cpp
emit ClientDisconnected ( iCurChanID ); // TODO do this outside the mutex lock?
}

FreeChannel ( iCurChanID ); // note that the channel is now not in use
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this be before telling the JamController the client left? (JamController has its own structures to worry about IIRC.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't think so. I thought it would be better if the channel still existed at the point the jam controller was notified. But in fact the channel object continues to exist - FreeChannel() just removes it from the channel order list and the binary search.

But I admit I haven't yet tested this code with recording enabled. It would be good to do so.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well I've run a test server with recording enabled, and some test clients. All appears to work ok. I've also looked at the code for CJamRecorder::OnDisconnected(), and there doesn't appear to be anything that would interact with the server.cpp code.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm okay if it makes no difference.

Comment thread src/server.cpp
Comment thread src/server.cpp
Comment thread src/server.cpp
Comment thread src/server.cpp
@pljones
Copy link
Copy Markdown
Collaborator

pljones commented Aug 27, 2021

I can't imagine it causes any measurable performance degradation on small servers (only 22 out of 145 servers on Any Genre 1 are 25 or above, all the rest lower, the vast majority on the default of 10). The memory overhead is also trivial, even for 150 channels as it's just ints.

All looks pretty good. Shame the Windows auto-build is currently broken for some reason.

@ann0see
Copy link
Copy Markdown
Member

ann0see commented Aug 29, 2021

Needs a rebase once #1981 is merged.

Please ping me if I‘m needed here (approval).

As I said I won’t comment much on the code.

This is preparatory for improving the performance
of FindChannel() with high numbers of channels.
Also call IsConnected() separately from GetAddress().
The old algorithm used sequential search, which was very inefficient
for large numbers of channels. This is now changed to use a binary
search, by maintaining an array of channel IDs ordered by IP+port.
The actual ordering is not important, so long as it is deterministic.
@softins softins force-pushed the faster-channel-lookup branch from 98d4d6c to be326c0 Compare August 30, 2021 08:17
@softins
Copy link
Copy Markdown
Member Author

softins commented Aug 30, 2021

Needs a rebase once #1981 is merged.

Done. Will respond to @pljones comments soon when I get time.

@softins softins added this to the Release 3.8.1 milestone Aug 31, 2021
@pljones
Copy link
Copy Markdown
Collaborator

pljones commented Aug 31, 2021

OK, spent time with the client - it's had no unexpected adverse effects there (given it shouldn't touch it at all). I've not had a chance to try the server and I don't run with sufficient load to notice, I would expect. I'll get it installed now.


Oh, command line git diff is much easier to see what's going on... or again, maybe I'm just happier with it than the Github online display...

... to the extent I'm happy it's sane and sound.

Jamulus, Version 3.8.0dev-be326c04 running at jamulus.drealm.info on Any Genre 2.

@softins softins requested a review from ann0see August 31, 2021 22:51
Copy link
Copy Markdown
Member

@ann0see ann0see left a comment

Choose a reason for hiding this comment

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

Tried locally on my intel core m5 (skylake) laptop and sound is still ok/so-so with 15 clients (however, the server is probably coming close to its limits and my device is not powerful at all).

Scanned the code
Installed it locally
... and I trust Peter that everything else is ok ;-).

So happy to approve

@ann0see
Copy link
Copy Markdown
Member

ann0see commented Sep 1, 2021

BTW: maybe there are also other places where binary search might be useful. Didn't check the code though.

@softins
Copy link
Copy Markdown
Member Author

softins commented Sep 1, 2021

BTW: maybe there are also other places where binary search might be useful. Didn't check the code though.

Yes, I plan to have a look when I get time.

@softins softins merged commit 15ee609 into jamulussoftware:master Sep 1, 2021
dtinth added a commit to dtinth/jamulus that referenced this pull request Sep 24, 2021
…hannel-lookup"

This reverts commit 15ee609, reversing
changes made to 82a0008.
dtinth added a commit to dtinth/jamulus that referenced this pull request Sep 28, 2021
…faster-channel-lookup""

This reverts commit b69c3b3.
@softins softins deleted the faster-channel-lookup branch January 11, 2022 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants