不要一口流利的接口显著影响.NET应用程序的运行时性能?流利、应用程序、显著、接口

2023-09-03 06:40:38 作者:难过了,闭眼倾听周围的声音,让自己沉浸在喧嚣中

我目前占据自己与实现流畅的界面,用于现有技术,这将使code类似于下面的代码片段:

I'm currently occupying myself with implementing a fluent interface for an existing technology, which would allow code similar to the following snippet:

using (var directory = Open.Directory(@"path\to\some\directory"))
{
    using (var file = Open.File("foobar.html").In(directory))
    {
        // ...
    }
}

为了实现这种结构,需要类累积参数并将它们传递给其他对象。例如,为贯彻落实 Open.File(...)在(...)结构,则需要两个类:

In order to implement such constructs, classes are needed that accumulate arguments and pass them on to other objects. For example, to implement the Open.File(...).In(...) construct, you would need two classes:

// handles 'Open.XXX':
public static class OpenPhrase
{
    // handles 'Open.File(XXX)':
    public static OpenFilePhrase File(string filename)
    {
        return new OpenFilePhrase(filename);
    }

    // handles 'Open.Directory(XXX)':
    public static DirectoryObject Directory(string path)
    {
        // ...
    }
}

// handles 'Open.File(XXX).XXX':
public class OpenFilePhrase
{
    internal OpenFilePhrase(string filename)
    {
        _filename = filename
    }

    // handles 'Open.File(XXX).In(XXX):
    public FileObject In(DirectoryObject directory)
    {
        // ...
    }

    private readonly string _filename;
}

即,多个构成部件语句,如初始的例子有,在多个对象需要被用于传递参数给后续对象链中的,直到实际语句终于可以执行创建

That is, the more constituent parts statements such as the initial examples have, the more objects need to be created for passing on arguments to subsequent objects in the chain until the actual statement can finally execute.

我感兴趣的一些看法:请问这是利用以上技术显著影响使用它的应用程序的运行时性能实现流畅的界面?随着运行时间的表现,我指的两个的速度和内存使用方面的问题。

I am interested in some opinions: Does a fluent interface which is implemented using the above technique significantly impact the runtime performance of an application that uses it? With runtime performance, I refer to both speed and memory usage aspects.

请记住,一个潜在的大量临时,参数的节能对象将不得不只有很短暂的时间跨度,我认为可以把某一pressure垃圾收集器创建的。

Bear in mind that a potentially large number of temporary, argument-saving objects would have to be created for only very brief timespans, which I assume may put a certain pressure on the garbage collector.

如果你认为有显著的性能影响,你知道一个更好的方式来实现流畅的接口?

If you think there is significant performance impact, do you know of a better way to implement fluent interfaces?

推荐答案

一般来说,一个非常小的一生对象正是那种物体的GC交易最有效地使用,因为他们大多会死人的时候下一个小的收集运行 - 和任何像样的GC执行,未成年人收集的成本是成正比的生活的对象的总大小。因此,短期对象花费很少,而且它们的分配意味着只有撞上一个指针了,这就是快。

Generally speaking, objects with a very small lifetime are exactly the kind of objects that the GC deals most efficiently with, because most of them will be dead at the time the next minor collection runs -- and on any decent GC implementation, the cost of a minor collection is proportional to the total size of live objects. Thus, short-lived objects cost very little, and their allocation means only bumping a pointer up, which is fast.

所以,我要说:大概没有显著的性能影响

So I would say: probably no significant performance impact.