首页 > 解决方案 > The more request means slower read from cached objects?

问题描述

Please do not judgme for the type of question. It might be about software algorithms and concepts. That is why I must write the link that it is already asked on server fault.

1- > the algorithm and concept of project .net web app.

Client's onblur event makes an ajax call-> UI -> functionUI() -> > APP <<

functionUI()

int limit=450;
public bool functionUI(){

for(int i=0;i< limit ; i++){
 functionApp(i);
}

}


public static bool functionApp(int i){
 foreach(HolidayEntity h in Global.holidayGlobals.Values /*CachedList*/){
  if(h.Value == i){
return false;
}
return true;
}

}

2- The question The limit value from ui side if it is 2 or 20 times the response duration is good . But when it is 450 times. It takes 40 seconds to get a result in client side. I know the code is not well organised but the question is why the application side so slow when it is responsible to make more calculations.

Any clue will be so usefull. Thank you.

Servers are Server 2018 SP 1, .net Frame Work 4.0.30319.42000 It is only happened in production environment. In development side the application runs quite fast even the limit is 450.

标签: c#asp.net.netiis.net-remoting

解决方案


基本上这种类型的缓慢来自于犯了这个错误:

for (var i = 0; i<100; i++)
{
   // do a network-roundtrip/remoting/io/etc
}

解决方案是将其更改为:

// do a network-roundtrip/remoting/io/etc
for (var i = 0; i<100; i++)
{
    // do something with the result of the above call
}

因此,如果我对您的理解正确,则数据Global.holidayGlobals.Values来自另一个进程,例如内存缓存。如果是这样,您最简单的选择是将代码更改为:


int limit=450;
public bool functionUI()
{
    var holidays = Global.holidayGlobals.Values;
    for(int i=0;i< limit ; i++)
    {
        functionApp(i, holidays);
    }
}


public static bool functionApp(int i, IEnumerable<HolidayEntity> holidays)
{
    foreach(var h in holidays)
    {
        if(h.Value == i)
        {
             return false;
        }
    }
    return true;
}

我们仍然在循环中做太多的工作(我们可以通过创建字典并查找它来改进算法)但是对于您的号码(450),与往返内存缓存的时间相比,这可以忽略不计。

更新

从评论到问题,如果holidaysDictionary<int, HolidayEntity>这样,代码可以像这样变得更好:

public bool functionUI()
{
    var holidays = Global.holidayGlobals.Values;

    for(int i=0;i< limit ; i++)
    {
        functionApp(i, holidays);
    }
}

public static bool functionApp(int i, IEnumerable<HolidayEntity> holidays)
{
    if (holidays.TryGetValue(i, out var h))
    {
        return false;
    }
    return true;
}

推荐阅读