在使用 dotnet core
发布独立项目的时候,会带上大量依赖的库,但是通过微软提供的工具可以去掉一些在代码没有用到的库。
本文介绍的工具是 Microsoft.Packaging.Tools.Trimming
这是一个预览的工具,需要从 myget.org
下载
通过这个工具可以分析有哪些 dll 不是必须的
这个工具使用方法很简单,不过需要先点击 nuget 源添加 https://dotnet.myget.org/F/dotnet-core/api/v3/index.json 才可以
添加方法十分简单,本文这里就不说了
通过 nuget
安装 Microsoft.Packaging.Tools.Trimming
库,这个库是预览版,需要勾选预览才能找到
然后编辑一下 csproj
文件
<PropertyGroup>
<TrimUnusedDependencies>true</TrimUnusedDependencies>
<RootPackageReference>false</RootPackageReference>
</PropertyGroup>
这里添加的属性 TrimUnusedDependencies
就是表示删除不需要的依赖,这里的依赖就是相对于 Root
寻找的依赖,也就是不需要的 PackageReferences
都不会放在发布文件
如果不知道如何编辑 csproj
文件,有一个简单的方法就是在发布的时候加上参数 /p:TrimUnusedDependencies=true
就可以
dotnet publish --self-contained true /p:TrimUnusedDependencies=true
建议是在 build
的时候也使用 /p:TrimUnusedDependencies=true
参数,这样就可以发现一些动态使用的类没有引用
实际我测试通过这个方法没有去掉多少dll
不如使用 CoreRT
编译为 Native
文件
Reducing the size of self-contained .NET Core applications
Creating a Minimal ASP.NET Core Windows Container
standard/trimming.md at release/2.0.0 · dotnet/standard
dotnet core 黑科技·String.IndexOf
性能
本文来告诉大家 dotnet core 里面使用的黑科技,如何提高String.IndexOf(char)
的性能
在Performance Improvements in .NET Core有说道哪些提高性能的代码,所以我就去看了一下,发现有一些黑科技。
里面包括了 Concat
的提升和很多 linq 的提升,我准备在自己的 WPF 项目使用这些代码,因为现在的项目没有使用 .net Framework 4.7
。
感觉垃圾微软把很多功能放在一个 Framework
让很多开发者无法升级
本文主要来让大家看一下 IndexOf
的黑科技
修改的提交在Improve performance of String.IndexOf(char) and String.LastIndexOf(char) by bbowyersmyth
public unsafe int IndexOf(char value, int startIndex, int count)
{
if (startIndex < 0 || startIndex > Length)
throw new ArgumentOutOfRangeException("startIndex", SR.ArgumentOutOfRange_Index);
if (count < 0 || count > Length - startIndex)
throw new ArgumentOutOfRangeException("count", SR.ArgumentOutOfRange_Count);
fixed (char* pChars = &_firstChar)
{
char* pCh = pChars + startIndex;
- for (int i = 0; i < count; i++)
+
+ while (count >= 4)
+ {
+ if (*pCh == value) goto ReturnIndex;
+ if (*(pCh + 1) == value) goto ReturnIndex1;
+ if (*(pCh + 2) == value) goto ReturnIndex2;
+ if (*(pCh + 3) == value) goto ReturnIndex3;
+
+ count -= 4;
+ pCh += 4;
+ }
+
+ while (count > 0)
{
if (*pCh == value)
- return i + startIndex;
+ goto ReturnIndex;
+
+ count--;
pCh++;
}
- }
- return -1;
+ return -1;
+
+ ReturnIndex3: pCh++;
+ ReturnIndex2: pCh++;
+ ReturnIndex1: pCh++;
+ ReturnIndex:
+ return (int)(pCh - pChars);
+ }
}
可以看到.net Framework
的代码是使用循环
fixed (char* pChars = &_firstChar)
{
char* pCh = pChars + startIndex;
for (int i = 0; i < count; i++)
{
if (*pCh == value)
{
return i + startIndex;
}
pCh++;
}
}
代码很简单,但是优化只有就使用了很黑的
fixed (char* pChars = &_firstChar)
{
char* pCh = pChars + startIndex;
while (count >= 4)
{
if (*pCh == value) goto ReturnIndex;
if (*(pCh + 1) == value) goto ReturnIndex1;
if (*(pCh + 2) == value) goto ReturnIndex2;
if (*(pCh + 3) == value) goto ReturnIndex3;
count -= 4;
pCh += 4;
}
while (count > 0)
{
if (*pCh == value)
goto ReturnIndex;
count--;
pCh++;
}
return -1;
ReturnIndex3: pCh++;
ReturnIndex2: pCh++;
ReturnIndex1: pCh++;
ReturnIndex:
return (int)(pCh - pChars);
为什么需要使用这样的方法,因为这样可以直接塞满寄存器,做判断会快很多。这和具体编译有关
测试代码可以使用IndexOfChar.cs
如果想看大神的测试,Measuring Performance Improvements in .NET Core with BenchmarkDotNet