当前位置: 首页 > news >正文

邢台手机网站建设信息2023年最新新闻简短摘抄

邢台手机网站建设信息,2023年最新新闻简短摘抄,企业网站设计收费,网站建设方案书怎么签字上传概要 我们在开发过程中,经常使用async的异步方法,但是有些时候,异步的方法中,可能包含一些同步的处理。本文主要介绍通过ValueTask这个struct,优化异步处理的方法性能。 代码及实现 有些时候我们会缓存一些数据在内…

概要

我们在开发过程中,经常使用async的异步方法,但是有些时候,异步的方法中,可能包含一些同步的处理。本文主要介绍通过ValueTask这个struct,优化异步处理的方法性能。

代码及实现

有些时候我们会缓存一些数据在内存中,这些数据因为不经常改变,所以并不需要每次都要从后台数据库中获取。

例如下面的代码:

 private static List<Branch> _branches;public async Task<List<Branch>> getBranches(){if (_branches is null){using (var context = new BankContext()){_branches = await context.Branches.ToListAsync();}   }return _branches;
}

在一个银行App相关的系统中,我们将分行的基本信息缓存在内存中,方便其它方法调用。

上面的代码有一个问题,无论我们的缓存是否命中,都会以Task<List>的形式返回。也就是说Runtime需要为返回Task相关的内容分配内存空间;如果缓存命中,意味着该方法仅仅是执行同步操作,实际上只是一个同步操作。

如果以Task<List>作为返回值,对于同步操作而言,完全是在浪费系统资源。Task是一个类,这就意味着只要我们要使用该类,就必须创建对象,然后在通过GC收集。

对于一些高吞吐量,高并发的站点,如果可以对其进行适当优化,可以节约大量资源。

ValueTask 解决方法

在.Net Core .2.0中,引入一个结构体类型 ValueTask, 用于处理async方法中,同步和异步返回并存的情况。

因为其只是一个结构体,它并不需要像Task那样去创建对象,再被GC收集。但是它却可以包裹TResult或Task,作为async方法的返回值。

我们将上面的代码进行修改,将Task替换成ValueTask即可

private static List<Branch> _branches;
public async ValueTask<List<Branch>> getBranchesByTaskValue(){if (_branches is null){using (var context = new BankContext()){_branches = await context.Branches.ToListAsync();return _branches;}                 }return _branches;
}

我们用Benchmark测试上述两个方法的性能。
在这里插入图片描述

从测试结果上看,ValueTask作为返回值,消耗时间增加了约27%,但是内存消耗几乎可以忽略不计。

全部代码请参考附录

附录

Programs.cs

using System.Diagnostics;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Net.Mail;
using System.ComponentModel.Design.Serialization;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Collections.Generic;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
namespace IQueryableIEnumerable
{[MemoryDiagnoser]public class Programs{[Benchmark]public async Task getBranches(){TaskValueTest task = new TaskValueTest();for(var i=0;i<5;++i){await task.getBranches();}}[Benchmark]public async Task getBranchesByValueTask(){TaskValueTest task = new TaskValueTest();for(var i=0;i<5;++i){await task.getBranchesByTaskValue();}}public static void Main(string[] args){var summary = BenchmarkRunner.Run<Programs>();  }  }
}

TaskValueTest.cs

namespace IQueryableIEnumerable
{using System;using System.Threading;using System.Threading.Tasks;using Microsoft.EntityFrameworkCore;using System.Linq;using System.Collections.Generic;using BenchmarkDotNet.Running;using BenchmarkDotNet.Attributes;using BenchmarkDotNet.Diagnosers;public class TaskValueTest{private static List<Branch> _branches;private static List<Branch> _branches2;public async ValueTask<List<Branch>> getBranchesByTaskValue(){if (_branches is null){using (var context = new BankContext()){_branches = await context.Branches.ToListAsync();}                 }return _branches;}public async Task<List<Branch>> getBranches(){if (_branches2 is null){using (var context = new BankContext()){_branches2 = await context.Branches.ToListAsync();}   }return _branches2;}}
}
http://www.ds6.com.cn/news/109171.html

相关文章:

  • 淘宝做网站百度一下电脑版网页
  • 长春做网站公司哪家好百度官网app
  • 做电影网站侵权吗seo运营人士揭秘
  • 各种网站推广是怎么做的手机网站建设公司
  • 做a动态网站百度搜索排名怎么做
  • 免费申请大王卡seo网络营销课程
  • html宠物网页简单代码百度seo多久能优化关键词
  • 亚马逊站外推广怎么做淘宝自动推广软件
  • 加工企业网站这么做百度网盘app官网
  • 建设行业门户网站需要什么条件安徽网站优化
  • 网络游戏企业不得向提供游戏服务百度seo手机
  • wordpress整站b站推广网站mmm
  • 梅江区住房和城乡建设局官方网站厦门人才网官网招聘
  • 杭州设计网站最好的公司淘宝推广怎么推
  • 北京网站开发哪家专业最近国际新闻
  • 用名字做头像是什么网站关键词优化报价
  • 海口网络平台网站开发网站排名点击工具
  • 网站图怎么做会高清图片百度关键词排名价格
  • 上海做网站公司做网站的公司北京seo优化排名
  • 做时时彩怎么做网站韶山百度seo
  • 建设网站物业经理上岗证陈俊华宁波seo外包
  • 什么网站做外链优化好哪里有整站优化
  • 做rap的网站收录查询api
  • 哪里有做手工活的可以拿回家的谷歌官方seo入门指南
  • 做网站网页需要学些什么如何优化网站推广
  • 做平台的网站有哪些内容吗广州seo网站管理
  • 四川做网站多少钱浏览器2345网址导航下载安装
  • 购物网站模板带后台网络营销策划书8000字
  • 快速网站排名优化第三方推广平台
  • 动漫制作专业有哪些职业岗位搜索引擎优化叫什么