I got an observable of KeyValuePair<int,double>:
--(7|45.2)--(3|11.1)--(5|13.2)--(6|36.2)--(3|57.4) I got a list of consumers defined at runtime. They are only interested in values produced for a single key (myKey).
For example:
- the consumer 7, is only interested in the value 45.2.
- the consumer 3, is only interested in the values 11.1 and 57.4
- the consumer 1, is only interested in values with myKey = 1, so none here.
Here is my consumer subscription code (one per consumer):
myObservable.Where(t => t.Key == myKey).Subscribe(t => /* DoSomething*/); Let's take:
- N = number of messages being produced by
myObservable - M = number of consumers
Let's call Comparison the code t.Key == myKey
For every new message being published, Comparison will be executed M times (once per consumer). In the case of N messages, Comparison will be executed N * M
Is RX Extension offering another way to do to avoid executing that many comparisons?
Do I need to make it myself? (using the dictionary of (mykey, consumers) for example, and forwarding the messages to the right consumer(s))
Thanks
GroupByand each subscribers is only subscribing to the group their interested in.source.GroupBy(kvp => kvp.Key).Select(grp => grp.Subscribe(kvp => /*each group do something*/));.GroupBy(kvp => kvp.Key)on myObservable and the consumer do:myObservable.Where(gp => gp.Key == myKey).SelectMany(gp=>gp).Select(kvp=>kvp.Value).Subscribe(v => /* DoSomething*/)it will give a different result. The complexity of this code, if I take K = the number of myKey possible (myKey in the [1...K] interval), will now be at worst K * M times. As for any new myKey an observable will be created and pushed through every new consumer that will execute the comparison on it. Do you agree on it?