You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides basic and experimental pub/sub features. In order to send the messages among the different subscribers per topic, the code maintains a map of maps, with a global lock:
The first level map, holds the topics name as key, having as value the second level map, which holds as a key the pointer address of the connection slot, and the related sender as a value.
In reality, senders contains the underlying connection slot, but having the connection slot pointer address as key its very handy, as its ideal for quick removal of connections on each topic. We could not use the messsage.Sender pointer as key, as this address could change over time.
Problem
We must take into account, that the publish method is a hot path of the program. We could expect high concurrency there:
The current global lock will lock all the structure each time a message its published, for any of the topics.
The current "map inside map" strategy was very convenient for the POC, as we can eliminate entries very easily each time a disconnection from a client happens. But, we are iterating a map, which means we need to search across the entire key space of the hashmap.
Goals
Evaluate the if this optimisations makes sense, by taking into account the status of the project and uses. Taking into account the publish method is going to be called much more times than the connection removal, probably we should make improvements at the cost of complexity.
Solve the problem of the global lock.
Solve the problem of the iteration over a map.
The text was updated successfully, but these errors were encountered:
We discussed with @mmontes11 about this subject. The first question in the air was ... is it necessary ? Well, maybe for a reduced connection set would be fast enough. Maybe, it would be positive to just put on hold the development of this issue, until we need to improve performance numbers.
However, we did some bench tests in order to quantify the possible improvements and better decide. If we were iterating over a slice instead of a map, it looks like we could improve by 8x the iteration speed, as we would be iterating a contiguous space of memory of the slice backing array. Here are the results of the tests:
One possible solution would be to replace the second level map with a pre allocated slice. Such slice should be completely managed by the library (subscribe, remove, compaction).
Context
This library provides basic and experimental pub/sub features. In order to send the messages among the different subscribers per topic, the code maintains a map of maps, with a global lock:
The first level map, holds the topics name as key, having as value the second level map, which holds as a key the pointer address of the connection slot, and the related sender as a value.
In reality, senders contains the underlying connection slot, but having the connection slot pointer address as key its very handy, as its ideal for quick removal of connections on each topic. We could not use the
messsage.Sender
pointer as key, as this address could change over time.Problem
We must take into account, that the publish method is a hot path of the program. We could expect high concurrency there:
Goals
The text was updated successfully, but these errors were encountered: