简体   繁体   English

在ASP.NET Core MVC中显示速度慢

[英]Slow DisplayFor performance in ASP.NET Core MVC

In my current application I'm generating a fairly lengthy table to display to the user. 在我当前的应用程序中,我正在生成一个相当长的表以显示给用户。 I've been seeing some serious performance issues with it, which I've tracked down to usage of @Html.DisplayFor, and I'm not entirely sure why. 我一直看到它的一些严重的性能问题,我一直跟踪到@ Html.DisplayFor的使用,但我不确定为什么。

Edit: I've replaced the code sample with a more concise and reproducible setup. 编辑:我已经用更简洁和可重复的设置替换了代码示例。

In order to isolate the issue, I have created a new asp.net core MVC project using all default settings in visual studio, with no authentication. 为了找出问题,我使用Visual Studio中的所有默认设置创建了一个新的asp.net核心MVC项目,没有进行身份验证。 I created a view model as such: 我这样创建了一个视图模型:

public class TestingViewModel
{
    public int Id { get; set; }
    public string TextValue1 { get; set; }
    public string TextValue2 { get; set; }
}

Then added a controller which fills the view model with data to pass to the view: 然后添加一个控制器,该控制器用数据填充视图模型以传递到视图:

    public IActionResult TestThings()
    {
        var list = new List<TestingViewModel>();
        for(var i = 0; i < 1000; i++)
            list.Add(new TestingViewModel {Id = i, TextValue1 = "Test", TextValue2 = "Test2"});

        return View(list);
    }

The view is the bare minimum possible to display the data: 该视图是显示数据的可能的最低要求:

@model List<DisplayForTest.ViewModels.TestingViewModel>

@foreach (var item in Model)
{
    @Html.DisplayFor(m => item.Id)
    @Html.DisplayFor(m => item.TextValue1)
    @Html.DisplayFor(m => item.TextValue2)
}

When running this code, it takes over one second to run! 运行此代码时,需要一秒钟以上的时间来运行! The culprit is the DisplayFor. 罪魁祸首是DisplayFor。 If I change the view as follows: 如果我按如下方式更改视图:

@model List<DisplayForTest.ViewModels.TestingViewModel>

@foreach (var item in Model)
{
    @item.Id
    @item.TextValue1
    @item.TextValue2
}

This renders in 13ms. 渲染时间为13ms。 It's clear DisplayFor is adding a huge amount of time to rendering... on my PC that's nearly 0.4ms per call. 很明显,DisplayFor正在为我的PC添加大量渲染时间,每次调用几乎需要0.4ms。 While that's not bad in isolation, it makes it a pretty bad choice for lists or other things. 尽管这在隔离方面还不错,但对于列表或其他事物而言,这却是一个非常糟糕的选择。

Is DisplayFor really just that slow? DisplayFor真的那么慢吗? Or am I using it incorrectly? 还是我使用不正确?

Is DisplayFor really just that slow? DisplayFor真的那么慢吗? Or am I using it incorrectly? 还是我使用不正确?

It takes a bit of overhead to evaluate and execute lambda expressions. 计算和执行lambda表达式会花费一些开销。 First the framework has to validate it , then evaluate it . 首先,框架必须对其进行验证 ,然后对其进行评估 I am speculating a bit here , but it seems likely this is where the performance concerns are coming from; 我在这里进行了一些推测 ,但似乎这可能是性能问题的根源。 both of these methods require reflection. 这两种方法都需要反思。

All of the other display methods I've played with ( ValueFor , DisplayTextFor , etc.) have the same performance effect in your example. 我使用的所有其他显示方法( ValueForDisplayTextFor等)在您的示例中都具有相同的性能效果。

I can't speak for the MVC team as to why it is used for the default scaffolding, but it does make sense to me. 我不能为MVC团队谈论为什么将其用于默认脚手架,但这对我来说确实有意义。 DisplayFor can handle both of the most common use cases (displaying the value of a property, and displaying the value of a property using a custom template), and performs reasonably well in most cases. DisplayFor可以处理两种最常见的用例(显示属性的值,并使用自定义模板显示属性的值),并且在大多数情况下表现良好。

In this case, I don't see a problem with just using the raw value (basically .ToString ing it), or using it within an Html.Encode / Html.Raw method depending on what you're looking for. 在这种情况下,仅使用原始值(基本上是.ToString处理)还是根据您要查找的内容在Html.Encode / Html.Raw方法中使用它就不会出现问题。

Using .Net Core on a Raspberry PI, I experienced the same problem. 在Raspberry PI上使用.Net Core,我遇到了同样的问题。 The performance went really bad. 性能真的很差。 I used dotTrace and found that the reflection uses much of the time. 我使用dotTrace,发现反射花费了很多时间。

Based on the following article , I was able to speedup the application. 根据以下文章 ,我能够加快应用程序的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM