ASP.NET 编译模式“自动”与“从不”

发布于 2024-08-04 09:54:11 字数 280 浏览 3 评论 0原文

假设我的 ASPX 页面没有内联 C# 代码块。

因此,我可以安全地

<pages compilationMode="Never" />

在 web.config 文件中设置 ...,而不必担心编译错误。

从性能角度来看,使用以下设置会有任何损失吗?

<pages compilationMode="Auto" />

即“自动”检测是否需要花费大量时间?

Let's assume my ASPX page has no inline C# code blocks.

So, I can safely set

<pages compilationMode="Never" />

...in my web.config file and not worry about compilation errors.

Performance-wise, would there be any penalty to using the following setting?

<pages compilationMode="Auto" />

i.e. does the "auto" detection take any significant amount of time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-08-11 09:54:11

自动的影响似乎很小。 (尽管显然比从不多)。

如果我们检查 System.Web.UI 中的代码。 TemplateParser,我们在 ImportSourceFile 中看到,如果模式设置为 Never,进程会提前中止:

if (this.CompilationMode == CompilationMode.Never)
{
    return null;
}

这当然是有帮助的,而且绝对是影响最小的。然而,继续执行 TemplateParser 中的例程,我们可以在 ParseStringInternal 中看到解析器逐字扫描加载的模板,搜索 <% 的变体:

if (!this.flags[2] && (match = BaseParser.aspCodeRegex.Match(text, startat)).Success)
{
    string str3 = match.Groups["code"].Value.Trim();
    if (str3.StartsWith("$", StringComparison.Ordinal))
    {
        this.ProcessError(SR.GetString("ExpressionBuilder_LiteralExpressionsNotAllowed", new object[] { match.ToString(), str3 }));
    }
    else
    {
        this.ProcessCodeBlock(match, CodeBlockType.Code, text);
    }
}

请注意 BaseParser.aspCodeRegex,它是此模式的一个实例:

public AspCodeRegex()
{
    base.pattern = @"\G<%(?!@)(?<code>.*?)%>";
    ...
}

如果没有遇到,它就会继续前进。搜索是一个相当便宜的操作 - 最大的打击是当实际找到代码块并编译它们时。

The impact of Auto appears to be very little. (Although obviously more than Never).

If we examine the code in System.Web.UI.TemplateParser, we see in ImportSourceFile that process is aborted early if mode is set to Never:

if (this.CompilationMode == CompilationMode.Never)
{
    return null;
}

Which is of course helpful, and definitely the lowest-impact. However, continuing through the routines in TemplateParser, we can see in ParseStringInternal the parser literally scans the loaded template searching for variations of <%:

if (!this.flags[2] && (match = BaseParser.aspCodeRegex.Match(text, startat)).Success)
{
    string str3 = match.Groups["code"].Value.Trim();
    if (str3.StartsWith("$", StringComparison.Ordinal))
    {
        this.ProcessError(SR.GetString("ExpressionBuilder_LiteralExpressionsNotAllowed", new object[] { match.ToString(), str3 }));
    }
    else
    {
        this.ProcessCodeBlock(match, CodeBlockType.Code, text);
    }
}

Note the BaseParser.aspCodeRegex, which is an instance of this pattern:

public AspCodeRegex()
{
    base.pattern = @"\G<%(?!@)(?<code>.*?)%>";
    ...
}

If it encounters none, it simply moves on. The searching is a fairly inexpensive operation - the biggest hit is when code blocks are actually found, compiling them.

人│生佛魔见 2024-08-11 09:54:11

我不确定我是否同意自动应该比始终性能更高的建议。关于此有一个类似问题,我认为最终“自动””(以及一般的非编译页面)是为了提供更好的可扩展性,而不一定是更好的性能(超出初始编译/解析开销)。

如果自动在每种情况下都具有更高的性能,为什么不将其作为默认设置呢?

具有少量、固定数量的程序集的应用程序将受益于标准的始终设置和站点范围的预编译;对于类似 CMS 的场景,例如 SharePoint,页面在运行时更改,出于剪切的需要,自动 是唯一的选项。

在我们的场景中(几百个程序集,在运行时不改变),我无法解释为什么 % Time in JIT 在应用程序运行后很长时间内波动,有时会高于 60%已经预热,甚至进行了全站预编译?如果这在 200-500 个程序集部署中很常见,那么我也可以在这些场景中看到自动的优势。

I'm not sure I agree with the suggestion that Auto should be more performant than Always. There's a similar question on this and I think that ultimately 'Auto' (and non-compiled pages, in general) were introduced as a means to offer better scalability, not necessarily better performance (beyond the initial compilation/parse overhead).

If Auto was more performant in every scenario, why wouldn't it be the default?

Applications that have a small, fixed number of assemblies would benefit from the standard Always setting and site-wide pre-compilation; for CMS-esque scenarios, like SharePoint, where pages are changed at run-time, Auto is the only option, out of shear necessity.

What I'm at a loss to explain in our scenario (few hundred assemblies, not changing at run-time) is why % Time in JIT is fluctuating and occasionally higher than 60%, long after the app has been warmed up and even with site-wide pre-compilation? If this common with 200-500 assembly deployments, then I can see the benefit of Auto in those scenarios as well.

久光 2024-08-11 09:54:11

自动 应该比始终 具有更高的性能,并且在您添加内联 C# 代码时是一种安全的故障保护。

将花费额外的时间来确定是否需要编译页面,这与从不选项相比增加了一些开销。

Auto should be more performant than Always, and is a safe failsafe for when you do add inline C# code.

Extra time will be spent determining if a page needs to be compiled, which adds a little overhead in comparison to the Never option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文