Couchbase RX 客户端 - 合并多个可观察对象与平面图

Couchbase RX Client - Merging multiple observables vs flatmap

以下代码片段之间的性能差异是什么?

用例:使用 Couchbase 响应式 SDK 从 Couchbase 检索多个键。

片段 #1

 return Observable
            .from(keys)
            .flatMap(id ->
                    bucket.get(id, RawJsonDocument.class)
            )
            .toList()
            .toBlocking()
            .single();

片段 #2

     List<Observable<RawJsonDocument>> list = new ArrayList<>();
     keys.forEach(key -> list.add(bucket.get(key, RawJsonDocument.class)));
     return Observable.merge(list)
            .toList()
            .toBlocking()
            .single();

第一个片段是根据 CB documentation.

推荐的方式

Also, at the very end the observable is converted into a blocking one, but everything before that, including the network calls and the aggregation, is happening completely asynchronously.

Inside the SDK, this provides much more efficient resource utilization because the requests are very quickly stored in the internal Request RingBuffer and the I/O threads are able to pick batches as large as they can. Afterward, whatever server returns a result first it is stored in the list, so there is no serialization of responses going on.

第二个片段是我们今天在代码库中发现的一个案例,其中有一个Observable列表,其中每个observable都一个一个地触发Bucket的get方法,然后将它们全部合并。

什么 flatMap 它为每个键调用映射器函数,然后合并这些调用的结果。我不希望这种合并算法与 merge 中使用的算法有任何不同。所以如果 bucket.get() 是 non-blocking,两者之间应该没有性能差异。

代码段 #1 对我来说更具可读性。

这两个片段之间应该没有性能差异,从 Couchbase SDK 和 RxJava 的角度来看,它们非常相似(因为 flatMap 基本上做了 mapObservables 喜欢你的 keys.forEach,然后 merge 循环中的可观察值)。

编辑:从个人角度来看,我还是更喜欢第一种风格;)