What is the NO_PERF flag in reactive extensions source code doing

家住魔仙堡 提交于 2019-12-11 11:07:35

问题


In the ReactiveExtensions source code there are huge swathes of codes switching between different implementations such as

https://github.com/Reactive-Extensions/Rx.NET/blob/master/Rx.NET/Source/System.Reactive.Linq/Reactive/Linq/QueryLanguage.StandardSequenceOperators.cs

and a snippet

#if !NO_PERF
    return new Distinct<TSource, TSource>(source, x => x, EqualityComparer<TSource>.Default);
#else
     return Distinct_(source, x => x, EqualityComparer<TSource>.Default);
#endif

what is the intention of this flag NO_PERF and what is the difference in behaviour between the library compiled with it and without it.


回答1:


As a member of the Rx team, I can answer this:

The answer goes back to our changes from Rx 1.1 to Rx 2.0. We wanted the ability to keep the old style of using AnonymouseObservable around just in case, but for most of the time, you want the performance enhanced version.

There are big differences in both performance, and in some cases more eager disposal. You can find out more information about our changes here.




回答2:


It's used to switch in/out logic that favours safety over performance. For example, in most of the operator implementations an AnonymousObservable is created to wrap OnXXX invocations and catch exceptions in observers.

When !NO_PERF is true this wrapper is not created - this shortens the call chain for queries and results in less objects, less GC pressure and faster code - but it's less safe as it assumes well-behaved observers.

This code has lots of examples.

I don't know, but I can see it being used inside other MS code that is a client of Rx, understands the consequences and is prepared to take on the responsibility of creating well-behaved clients.



来源:https://stackoverflow.com/questions/22680417/what-is-the-no-perf-flag-in-reactive-extensions-source-code-doing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!