2 Commits

Author SHA1 Message Date
7e9c87de44 feat: convert png and jpeg to webp to reduce usage. 2025-03-24 22:39:10 +08:00
fda4c01c22 feat: compress command. 2025-03-24 22:38:18 +08:00
298 changed files with 292 additions and 3552 deletions

View File

@@ -15,9 +15,6 @@ trim_trailing_whitespace = true
[project.json]
indent_size = 2
[*.{yaml,yml}]
indent_size = 2
# C# and Visual Basic files
[*.{cs,vb}]
charset = utf-8-bom

3
.gitattributes vendored
View File

@@ -1,5 +1,2 @@
*.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.avif filter=lfs diff=lfs merge=lfs -text
*.webp filter=lfs diff=lfs merge=lfs -text

View File

@@ -1,30 +1,33 @@
name: Build blog docker image
on:
push:
branches:
- master
push:
branches:
- master
jobs:
Build-Blog-Image:
runs-on: archlinux
steps:
- name: Check out code.
uses: https://mirrors.rrricardo.top/actions/checkout.git@v4
with:
lfs: true
- name: Build project.
run: |
podman pull mcr.azure.cn/dotnet/aspnet:10.0
cd YaeBlog
pwsh build.ps1 build
- name: Workaround to make sure podman-login working.
run: |
mkdir /root/.docker
- name: Login tencent cloud docker registry.
uses: https://mirrors.rrricardo.top/actions/podman-login.git@v1
with:
registry: ccr.ccs.tencentyun.com
username: 100044380877
password: ${{ secrets.TENCENT_REGISTRY_PASSWORD }}
auth_file_path: /etc/containers/auth.json
- name: Push docker image.
run: podman push ccr.ccs.tencentyun.com/jackfiled/blog:latest
Build-Blog-Image:
runs-on: archlinux
steps:
- uses: https://mirrors.rrricardo.top/actions/checkout.git@v4
name: Check out code
with:
lfs: true
- name: Build project
run: |
cd YaeBlog
dotnet publish
- name: Build docker image
run: |
cd YaeBlog
podman build . -t registry.cn-beijing.aliyuncs.com/jackfiled/blog:latest
- name: Workaround to make sure podman login succeed
run: |
mkdir /root/.docker
- name: Login aliyun docker registry
uses: https://mirrors.rrricardo.top/actions/podman-login.git@v1
with:
registry: registry.cn-beijing.aliyuncs.com
username: 初冬的朝阳
password: ${{ secrets.ALIYUN_PASSWORD }}
auth_file_path: /etc/containers/auth.json
- name: Push docker image
run: podman push registry.cn-beijing.aliyuncs.com/jackfiled/blog:latest

View File

@@ -1,13 +0,0 @@
namespace YaeBlog.Tests;
public class DateTimeOffsetTests
{
[Fact]
public void DateTimeOffsetParseTest()
{
const string input = "2026-01-04T16:36:36.5629759+08:00";
DateTimeOffset time = DateTimeOffset.Parse(input);
Assert.Equal("2026年01月04日 16:36:36", time.ToString("yyyy年MM月dd日 HH:mm:ss"));
}
}

View File

@@ -1,25 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\YaeBlog\YaeBlog.csproj" />
</ItemGroup>
</Project>

View File

@@ -10,6 +10,5 @@
<File Path="LICENSE" />
<File Path="README.md" />
</Folder>
<Project Path="YaeBlog.Tests/YaeBlog.Tests.csproj" />
<Project Path="YaeBlog/YaeBlog.csproj" />
</Solution>

View File

@@ -6,11 +6,5 @@ public interface IEssayScanService
{
public Task<BlogContents> ScanContents();
/// <summary>
/// 将对应的博客文章保存在磁盘上
/// </summary>
/// <param name="content"></param>
/// <param name="isDraft">指定对应博客文章是否为草稿。因为BlogContent是不可变对象因此提供该参数以方便publish的实现。</param>
/// <returns></returns>
public Task SaveBlogContent(BlogContent content, bool isDraft = true);
}

View File

@@ -19,7 +19,6 @@ public sealed class YaeBlogCommand
AddWatchCommand(_rootCommand);
AddListCommand(_rootCommand);
AddNewCommand(_rootCommand);
AddUpdateCommand(_rootCommand);
AddPublishCommand(_rootCommand);
AddScanCommand(_rootCommand);
AddCompressCommand(_rootCommand);
@@ -47,7 +46,7 @@ public sealed class YaeBlogCommand
WebApplication application = builder.Build();
application.MapStaticAssets();
application.UseStaticFiles();
application.UseAntiforgery();
application.UseYaeBlog();
@@ -77,7 +76,7 @@ public sealed class YaeBlogCommand
WebApplication application = builder.Build();
application.MapStaticAssets();
application.UseStaticFiles();
application.UseAntiforgery();
application.UseYaeBlog();
@@ -110,12 +109,7 @@ public sealed class YaeBlogCommand
await essayScanService.SaveBlogContent(new BlogContent(
new FileInfo(Path.Combine(blogOption.Value.Root, "drafts", file + ".md")),
new MarkdownMetadata
{
Title = file,
Date = DateTimeOffset.Now.ToString("o"),
UpdateTime = DateTimeOffset.Now.ToString("o")
},
new MarkdownMetadata { Title = file, Date = DateTime.Now },
string.Empty, true, [], []));
Console.WriteLine($"Created new blog '{file}.");
@@ -123,32 +117,6 @@ public sealed class YaeBlogCommand
new EssayScanServiceBinder());
}
private static void AddUpdateCommand(RootCommand rootCommand)
{
Command newCommand = new("update", "Update the blog essay.");
rootCommand.AddCommand(newCommand);
Argument<string> filenameArgument = new(name: "blog name", description: "The blog filename to update.");
newCommand.AddArgument(filenameArgument);
newCommand.SetHandler(async (file, _, _, essayScanService) =>
{
Console.WriteLine("HINT: The update command only consider published blogs.");
BlogContents contents = await essayScanService.ScanContents();
BlogContent? content = contents.Posts.FirstOrDefault(c => c.BlogName == file);
if (content is null)
{
Console.WriteLine($"Target essay {file} is not exist.");
return;
}
content.Metadata.UpdateTime = DateTimeOffset.Now.ToString("o");
await essayScanService.SaveBlogContent(content, content.IsDraft);
}, filenameArgument,
new BlogOptionsBinder(), new LoggerBinder<EssayScanService>(), new EssayScanServiceBinder());
}
private static void AddListCommand(RootCommand rootCommand)
{
Command command = new("list", "List all blogs");
@@ -243,8 +211,7 @@ public sealed class YaeBlogCommand
}
// 设置发布的时间
content.Metadata.Date = DateTimeOffset.Now.ToString("o");
content.Metadata.UpdateTime = DateTimeOffset.Now.ToString("o");
content.Metadata.Date = DateTime.Now;
// 将选中的博客文件复制到posts
await essayScanService.SaveBlogContent(content, isDraft: false);

View File

@@ -5,33 +5,16 @@
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<base href="/"/>
<link rel="stylesheet" href="@Assets["YaeBlog.styles.css"]"/>
<link rel="icon" href="@Assets["images/favicon.ico"]"/>
<link rel="stylesheet" href="@Assets["tailwind.g.css"]"/>
<style>
@@font-face {
font-family: "Font Awesome 6 Free";
font-style: normal;
font-weight: 400;
font-display: block;
src: url(@Assets["fonts/fa-regular-400.woff2"]) format("woff2") url(@Assets["fonts/fa-regular-400.ttf"]) format("truetype");
}
@@font-face {
font-family: "Font Awesome 6 Free";
font-style: normal;
font-weight: 900;
font-display: block;
src: url(@Assets["fonts/fa-solid-900.woff2"]) format("woff2"), url(@Assets["fonts/fa-solid-900.ttf"]) format("truetype")
}
</style>
<link rel="stylesheet" href="YaeBlog.styles.css"/>
<link rel="icon" href="images/favicon.ico"/>
<link rel="stylesheet" href="globals.css"/>
<link rel="stylesheet" href="tailwind.g.css"/>
<HeadOutlet/>
<ImportMap/>
</head>
<body>
<Routes/>
<script src="@Assets["_framework/blazor.web.js"]"></script>
<Routes/>
<script src="_framework/blazor.web.js"></script>
</body>
</html>

View File

@@ -1,29 +0,0 @@
<div class="flex flex-wrap justify-center gap-12 max-w-md md:max-w-lg">
<div class="relative w-40 h-48 md:w-48 md:w-48 overflow-hidden
transition-all duration-300 ease-out hover:scale-125 group">
<img
src="./images/wechat-code.jpeg"
alt="微信赞赏码"
class="w-full h-full object-cover"
/>
<div class="absolute -bottom-8 left-0 right-0 text-center
text-white bg-black opacity-60 text-sm font-medium
backdrop-blur-sm group-hover:bottom-2 transition-all duration-300">
请我喝奶茶<br/>
</div>
</div>
<div class="relative w-40 h-48 md:w-48 md:h-48 overflow-hidden
transition-all duration-300 ease-out hover:scale-125 group">
<img
src="./images/alipay-code.jpeg"
alt="支付宝赞赏码"
class="w-full h-full object-cover"/>
<div class="absolute -bottom-8 left-0 right-0 text-center
text-white bg-black opacity-60 text-sm font-medium
backdrop-blur-sm group-hover:bottom-2 transition-all duration-300">
请我吃晚饭<br/>
</div>
</div>
</div>

View File

@@ -3,7 +3,7 @@
<div class="flex flex-col p-3">
<div class="text-3xl font-bold py-2">
<a href="/blog/essays/@(Essay.FileName)">@(Essay.Title)</a>
<a href="/blog/essays/@(Essay.FileName)" target="_blank">@(Essay.Title)</a>
</div>
<div class="p-2 flex flex-row justify-content-start gap-2">
@@ -14,7 +14,9 @@
@foreach (string key in Essay.Tags)
{
<div class="text-sky-600">
<Anchor Address="@($"/blog/tags/?tagName={UrlEncoder.Default.Encode(key)}")" Text="@($"# {key}")"/>
<a href="/blog/tags/?tagName=@(UrlEncoder.Default.Encode(key))">
# @key
</a>
</div>
}
</div>

View File

@@ -7,15 +7,11 @@
<Anchor Address="https://dotnet.microsoft.com" Text="@DotnetVersion"/>
驱动。
</p>
<p class="text-md">
Build Commit #
<Anchor Address="@BuildCommitUrl" Text="@BuildCommitId"/>
</p>
</div>
<div>
<p class="text-md">
<Anchor Address="https://beian.miit.gov.cn" Text="蜀ICP备2022004429号-1" NewPage="true"/>
<a href="https://beian.miit.gov.cn" target="_blank" class="text-black">蜀ICP备2022004429号-1</a>
</p>
</div>
</div>
@@ -23,8 +19,4 @@
@code
{
private string DotnetVersion => $".NET {Environment.Version}";
private string BuildCommitId => Environment.GetEnvironmentVariable("COMMIT_ID") ?? "local_build";
private string BuildCommitUrl => $"https://git.rrricardo.top/jackfiled/YaeBlog/commit/{BuildCommitId}";
}

View File

@@ -1,28 +0,0 @@
@inherits LayoutComponentBase
@attribute [StreamRendering]
<main class="container mx-auto flex flex-col min-h-screen">
<div class="grid grid-cols-3 mx-3">
<div class="md:col-span-2 col-span-3 h-20 flex items-center">
<a href="/blog/">
<span class="text-blue-600 text-2xl">Ricardo's Blog</span>
</a>
</div>
<div class="md:col-span-1 col-span-3 h-20 flex items-center">
<div class="flex flex-row w-full px-2 md:justify-center justify-end text-xl gap-3">
<Anchor Address="/blog/archives" Text="归档"/>
<Anchor Address="/blog/tags/" Text="标签"/>
<Anchor Address="/about/" Text="关于" NewPage="@(true)"/>
<Anchor Address="/friends" Text="友链" NewPage="@(true)"/>
</div>
</div>
</div>
<div class="px-4 py-2 flex-grow">
@Body
</div>
<Foonter/>
</main>

View File

@@ -1,3 +1,6 @@
@using YaeBlog.Models
@inject BlogOptions Options
<div class="px-4 py-8 border border-sky-700 rounded-md bg-sky-200">
<div class="flex flex-col gap-3 text-md">
<div>
@@ -21,17 +24,6 @@
Ricardo's Blog
</a>”。
</div>
<div class="flex flex-col">
<div class="flex justify-center">
<p>如果觉得不错的话,可以支持一下作者哦~</p>
</div>
<div class="flex justify-center">
<AppreciationCode/>
</div>
</div>
</div>
</div>

View File

@@ -1,53 +0,0 @@
<Project>
<PropertyGroup>
<ClientAssetsRestoreCommand Condition="'$(ClientAssesRestoreCommand)' == ''">pnpm install</ClientAssetsRestoreCommand>
<ClientAssetsBuildCommand Condition="'$(ClientAssetsBuildCommand)' == ''">pnpm run build</ClientAssetsBuildCommand>
<ClientAssetsBuildOutputParameter Condition="'$(ClientAssetsBuildOutputParameter)' == ''">--output</ClientAssetsBuildOutputParameter>
</PropertyGroup>
<PropertyGroup>
<_RestoreClientAssetsBeforeTargets Condition="'$(TargetFramework)' == ''">DispatchToInnerBuilds</_RestoreClientAssetsBeforeTargets>
</PropertyGroup>
<Target Name="RestoreClientAssets" BeforeTargets="$(_RestoreClientAssetsBeforeTargets)">
<Message Importance="high" Text="Running $(ClientAssetsRestoreCommand)"/>
<Exec Command="$(ClientAssetsRestoreCommand)"/>
</Target>
<Target Name="BuildClientAssets" DependsOnTargets="RestoreClientAssets" BeforeTargets="AssignTargetPaths">
<PropertyGroup>
<_ClientAssetsOutputFullPath>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)ClientAssets'))</_ClientAssetsOutputFullPath>
</PropertyGroup>
<MakeDir Directories="$(_ClientAssetsOutputFullPath)"/>
<Exec Command="$(ClientAssetsBuildCommand) $(ClientAssetsBuildOutputParameter) $(_ClientAssetsOutputFullPath)"/>
<ItemGroup>
<_ClientAssetsBuildOutput Include="$(IntermediateOutputPath)ClientAssets\**"/>
</ItemGroup>
</Target>
<Target Name="DefineClientAssets" AfterTargets="BuildClientAssets" DependsOnTargets="ResolveStaticWebAssetsConfiguration">
<ItemGroup>
<FileWrites Include="@(_ClientAssetsBuildOutput)"/>
</ItemGroup>
<DefineStaticWebAssets
CandidateAssets="@(_ClientAssetsBuildOutput)"
SourceId="$(PackageId)"
SourceType="Computed"
ContentRoot="$(_ClientAssetsOutputFullPath)"
BasePath="$(StaticWebAssetBasePath)"
>
<Output TaskParameter="Assets" ItemName="StaticWebAsset"/>
<Output TaskParameter="Assets" ItemName="_ClientAssetsStaticWebAsset"/>
</DefineStaticWebAssets>
<DefineStaticWebAssetEndpoints
CandidateAssets="@(_ClientAssetsStaticWebAsset)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="StaticWebAssetEndpoint" />
</DefineStaticWebAssetEndpoints>
</Target>
</Project>

View File

@@ -1,10 +1,7 @@
FROM mcr.azure.cn/dotnet/aspnet:10.0
ARG COMMIT_ID
ENV COMMIT_ID=${COMMIT_ID}
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /app
COPY bin/Release/net10.0/publish/ ./
COPY bin/Release/net9.0/publish/ ./
COPY source/ ./source/
COPY appsettings.json .

View File

@@ -1,12 +0,0 @@
namespace YaeBlog.Core.Exceptions;
public class BlogCommandException : Exception
{
public BlogCommandException(string message) : base(message)
{
}
public BlogCommandException(string message, Exception innerException) : base(message, innerException)
{
}
}

View File

@@ -0,0 +1,44 @@
@inherits LayoutComponentBase
@attribute [StreamRendering]
<main class="container mx-auto flex flex-col min-h-screen">
<div class="grid grid-cols-3 mx-3">
<div class="md:col-span-2 col-span-3 h-20 flex items-center">
<a href="/blog/">
<span class="text-blue-600 text-2xl">Ricardo's Blog</span>
</a>
</div>
<div class="md:col-span-1 col-span-3 h-20 flex items-center">
<div class="flex flex-row w-full px-2 gap-3 md:justify-center justify-end">
<div>
<a href="/blog/archives/">
<span class="text-xl text-blue-600">归档</span>
</a>
</div>
<div>
<a href="/blog/tags/">
<span class="text-xl text-blue-600">标签</span>
</a>
</div>
<div>
<a href="/about/" target="_blank">
<span class="text-xl text-blue-600">关于</span>
</a>
</div>
<div>
<a href="/friends/" target="_blank">
<span class="text-xl text-blue-600">友链</span>
</a>
</div>
</div>
</div>
</div>
<div class="px-4 py-2 flex-grow">
@Body
</div>
<Foonter/>
</main>

View File

@@ -6,7 +6,10 @@ namespace YaeBlog.Models;
public record BlogContents(ConcurrentBag<BlogContent> Drafts, ConcurrentBag<BlogContent> Posts)
: IEnumerable<BlogContent>
{
public IEnumerator<BlogContent> GetEnumerator() => Posts.Concat(Drafts).GetEnumerator();
IEnumerator<BlogContent> IEnumerable<BlogContent>.GetEnumerator()
{
return Posts.Concat(Drafts).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator GetEnumerator() => ((IEnumerable<BlogContent>)this).GetEnumerator();
}

View File

@@ -8,9 +8,7 @@ public class BlogEssay : IComparable<BlogEssay>
public required bool IsDraft { get; init; }
public required DateTimeOffset PublishTime { get; init; }
public required DateTimeOffset UpdateTime { get; init; }
public required DateTime PublishTime { get; init; }
public required string Description { get; init; }
@@ -30,7 +28,6 @@ public class BlogEssay : IComparable<BlogEssay>
FileName = FileName,
IsDraft = IsDraft,
PublishTime = PublishTime,
UpdateTime = UpdateTime,
Description = Description,
WordCount = WordCount,
ReadTime = ReadTime,

View File

@@ -4,9 +4,7 @@ public class MarkdownMetadata
{
public string? Title { get; set; }
public string? Date { get; set; }
public string? UpdateTime { get; set; }
public DateTime? Date { get; set; }
public List<string>? Tags { get; set; }
}

View File

@@ -19,7 +19,7 @@
</span>
</div>
@foreach (IGrouping<DateTimeOffset, BlogEssay> group in _essays)
@foreach (IGrouping<DateTime, BlogEssay> group in _essays)
{
<div class="p-2">
<div class="flex flex-col">
@@ -30,7 +30,7 @@
<div class="px-4 py-4 flex flex-col">
@foreach (BlogEssay essay in group)
{
<a href="@($"/blog/essays/{essay.FileName}")">
<a target="_blank" href="@($"/blog/essays/{essay.FileName}")">
<div class="flex flex-row p-2 mx-1 rounded-lg hover:bg-gray-300">
<div class="w-20">
@(essay.PublishTime.ToString("MM月dd日"))
@@ -51,13 +51,13 @@
</div>
@code {
private readonly List<IGrouping<DateTimeOffset, BlogEssay>> _essays = [];
private readonly List<IGrouping<DateTime, BlogEssay>> _essays = [];
protected override void OnInitialized()
{
base.OnInitialized();
_essays.AddRange(from essay in Contents.Essays
group essay by new DateTimeOffset(essay.PublishTime.Year, 1, 1,0, 0, 0, TimeSpan.Zero));
group essay by new DateTime(essay.PublishTime.Year, 1, 1));
}
}

View File

@@ -39,11 +39,6 @@
{
_page = Page ?? 1;
_pageCount = Contents.Count / EssaysPerPage + 1;
(_pageCount, int reminder) = int.DivRem(Contents.Count, EssaysPerPage);
if (reminder > 0)
{
_pageCount += 1;
}
if (EssaysPerPage * _page > Contents.Count + EssaysPerPage)
{

View File

@@ -12,42 +12,47 @@
<div class="flex flex-col py-8">
<div>
<div class="flex flex-col items-center">
<div>
<h1 id="title" class="text-4xl">@(_essay!.Title)</h1>
<h1 id="title" class="text-4xl">@(_essay!.Title)</h1>
<div class="col-auto">
</div>
</div>
<div class="px-6 pt-4 pb-2">
<div class="flex flex-row gap-4">
<div class="font-light">
@(_essay!.PublishTime.ToString("yyyy-MM-dd"))
</div>
<div class="flex flex-row gap-4 py-2">
@foreach (string tag in _essay!.Tags)
{
<div class="text-sky-500">
<a href="/blog/tags/?tagName=@(UrlEncoder.Default.Encode(tag))">
# @(tag)
</a>
</div>
}
</div>
<div class="font-light pb-1">
发布于: @(_essay.PublishTime.ToString("yyyy年MM月dd日 HH:mm:ss"))
</div>
@if (_essay.UpdateTime != _essay.PublishTime)
@foreach (string tag in _essay!.Tags)
{
<div class="font-light pb-1">
更新于: @(_essay.UpdateTime.ToString("yyyy年MM月dd日 HH:mm:ss"))
<div class="text-sky-500">
<a href="/blog/tags/?tagName=@(UrlEncoder.Default.Encode(tag))">
# @(tag)
</a>
</div>
}
</div>
</div>
<div class="font-light pb-1">
总字数:@(_essay!.WordCount)字,预计阅读时间 @(_essay!.ReadTime)
</div>
<div class="px-6 pt-2 pb-4">
<div class="font-light">
总字数:@(_essay!.WordCount)字,预计阅读时间 @(_essay!.ReadTime)。
</div>
</div>
<div class="grid grid-cols-3">
<div class="col-span-3 md:col-span-2 flex flex-col gap-3">
<div>
@((MarkupString)_essay!.HtmlContent)
</div>
<div>
<LicenseDisclaimer EssayFilename="@BlogKey"/>
</div>
</div>
<div class="col-span-3 md:col-span-1">
<div class="flex flex-col sticky top-20 px-8 pt-20">
<div class="flex flex-col sticky top-0 px-8">
<div>
<h3 class="text-2xl">文章目录</h3>
</div>
@@ -88,17 +93,8 @@
}
</div>
</div>
<div class="col-span-3 md:col-span-2 flex flex-col gap-3">
<div>
@((MarkupString)_essay!.HtmlContent)
</div>
<div>
<LicenseDisclaimer EssayFilename="@BlogKey"/>
</div>
</div>
</div>
</div>
@code {

View File

@@ -7,7 +7,7 @@
<div class="mx-20">
<div class="grid grid-cols-3 py-4">
<div class="col-span-3 md:col-span-1 p-5 p-lg-0">
<img src="@Assets["images/avatar.png"]" alt="Ricardo's Avatar" class="h-auto max-w-full">
<img src="images/avatar.png" alt="Ricardo's Avatar" class="h-auto max-w-full">
</div>
<div class="col-span-3 md:col-span-2">
@@ -26,7 +26,7 @@
<div class="">
<p class="text-lg">
学过一些基础的计算机知识,略懂一些代码
平平无奇的计算机科学与技术学徒,连微小的贡献都没做
</p>
</div>
</div>

View File

@@ -16,7 +16,8 @@ public sealed class EssayStylesPostRenderProcessor : IPostRenderProcessor
public async Task<BlogEssay> ProcessAsync(BlogEssay essay)
{
BrowsingContext context = new(Configuration.Default);
IDocument document = await context.OpenAsync(req => req.Content(essay.HtmlContent));
IDocument document = await context.OpenAsync(
req => req.Content(essay.HtmlContent));
ApplyGlobalCssStyles(document);
BeatifyTable(document);
@@ -35,7 +36,6 @@ public sealed class EssayStylesPostRenderProcessor : IPostRenderProcessor
{ "h5", "text-lg font-bold py-1" },
{ "p", "p-2" },
{ "img", "w-11/12 block mx-auto my-2 rounded-md shadow-md" },
{ "a", "text-blue-600" }
};
private void ApplyGlobalCssStyles(IDocument document)

View File

@@ -16,11 +16,11 @@ public sealed class BlogHotReloadService(
await rendererService.RenderAsync(true);
Task[] reloadTasks = [WatchFileAsync(stoppingToken)];
Task[] reloadTasks = [FileWatchTask(stoppingToken)];
await Task.WhenAll(reloadTasks);
}
private async Task WatchFileAsync(CancellationToken token)
private async Task FileWatchTask(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
@@ -33,15 +33,6 @@ public sealed class BlogHotReloadService(
break;
}
FileInfo changeFileInfo = new(changeFile);
if (changeFileInfo.Name.StartsWith('.'))
{
// Ignore dot-started file and directory.
logger.LogDebug("Ignore hidden file: {}.", changeFile);
continue;
}
logger.LogInformation("{} changed, re-rendering.", changeFile);
essayContentService.Clear();
await rendererService.RenderAsync(true);

View File

@@ -109,12 +109,6 @@ public partial class EssayScanService : IEssayScanService
{
foreach (BlogResult blog in fileContents)
{
if (blog.BlogContent.Length < 4)
{
// Even not contains a legal header.
continue;
}
int endPos = blog.BlogContent.IndexOf("---", 4, StringComparison.Ordinal);
if (!blog.BlogContent.StartsWith("---") || endPos is -1 or 0)
{
@@ -127,14 +121,14 @@ public partial class EssayScanService : IEssayScanService
try
{
MarkdownMetadata metadata = _yamlDeserializer.Deserialize<MarkdownMetadata>(metadataString);
_logger.LogDebug("Scan metadata title: '{title}' for {name}.", metadata.Title, blog.BlogFile.Name);
_logger.LogDebug("Scan metadata title: '{}' for {}.", metadata.Title, blog.BlogFile.Name);
contents.Add(new BlogContent(blog.BlogFile, metadata, blog.BlogContent[(endPos + 3)..], isDraft,
blog.Images, blog.NotFoundImages));
}
catch (YamlException e)
{
_logger.LogWarning("Failed to parser metadata from {name} due to {exception}, skipping", blog.BlogFile.Name, e);
_logger.LogWarning("Failed to parser metadata from {} due to {}, skipping", blog.BlogFile.Name, e);
}
}
});

View File

@@ -56,7 +56,7 @@ public sealed class ImageCompressService(IEssayScanService essayScanService, ILo
}
CompressResult[] compressedImages = (await Task.WhenAll(from image in uncompressedImages
select Task.Run(async () => new CompressResult(image, await ConvertToWebp(image))))).ToArray();
select Task.Run(async () => new CompressResult(image, await ConvertToWebp(image.Content))))).ToArray();
compressedSize += compressedImages.Select(i => i.CompressContent.Length).Sum();
@@ -65,8 +65,7 @@ public sealed class ImageCompressService(IEssayScanService essayScanService, ILo
select r.ImageInfo with
{
File = new FileInfo(r.ImageInfo.File.FullName.Split('.')[0] + ".webp"),
Content = r.CompressContent,
MineType = "image/webp"
Content = r.CompressContent
}).ToList();
// 修改文本
string blogContent = compressedImages.Aggregate(content.Content, (c, r) =>
@@ -89,31 +88,21 @@ public sealed class ImageCompressService(IEssayScanService essayScanService, ILo
}
}
private static async Task<byte[]> ConvertToWebp(BlogImageInfo image)
private static async Task<byte[]> ConvertToWebp(byte[] image)
{
using ImageJob job = new();
BuildJobResult result = await job.Decode(MemorySource.Borrow(image.Content))
.Branch(f => f.EncodeToBytes(new WebPLosslessEncoder()))
BuildJobResult result = await job.Decode(MemorySource.Borrow(image))
.EncodeToBytes(new WebPLossyEncoder(75))
.Finish()
.InProcessAsync();
// 超过128KB的图片使用有损压缩
// 反之使用无损压缩
ArraySegment<byte>? array = result.First?.TryGetBytes();
ArraySegment<byte>? losslessImage = result.TryGet(1)?.TryGetBytes();
ArraySegment<byte>? lossyImage = result.TryGet(2)?.TryGetBytes();
if (image.Size <= 128 * 1024 && losslessImage.HasValue)
if (array.HasValue)
{
return losslessImage.Value.ToArray();
return array.Value.ToArray();
}
if (lossyImage.HasValue)
{
return lossyImage.Value.ToArray();
}
throw new BlogCommandException($"Failed to convert {image.File.Name} to webp format: return value is null.");
throw new BlogFileException();
}
}

View File

@@ -1,62 +0,0 @@
using YaeBlog.Extensions;
using YaeBlog.Models;
namespace YaeBlog.Services
{
public class MarkdownWordCounter
{
private bool _inCodeBlock;
private int _index;
private readonly string _content;
private uint WordCount { get; set; }
private MarkdownWordCounter(BlogContent content)
{
_content = content.Content;
}
private void CountWordInner()
{
while (_index < _content.Length)
{
if (IsCodeBlockTag())
{
_inCodeBlock = !_inCodeBlock;
}
if (!_inCodeBlock && char.IsLetterOrDigit(_content, _index))
{
WordCount += 1;
}
_index++;
}
}
private bool IsCodeBlockTag()
{
// 首先考虑识别代码块
bool outerCodeBlock =
Enumerable.Range(0, 3)
.Select(i => _index + i < _content.Length && _content.AsSpan().Slice(_index + i, 1) is "`")
.All(i => i);
if (outerCodeBlock)
{
return true;
}
// 然后识别行内代码
return _index < _content.Length && _content.AsSpan().Slice(_index, 1) is "`";
}
public static uint CountWord(BlogContent content)
{
MarkdownWordCounter counter = new(content);
counter.CountWordInner();
return counter.WordCount;
}
}
}

View File

@@ -38,15 +38,7 @@ public partial class RendererService(
List<BlogEssay> essays = [];
foreach (BlogContent content in preProcessedContents)
{
(uint wordCount, string readTime) = GetWordCount(content);
DateTimeOffset publishDate = content.Metadata.Date is null
? DateTimeOffset.Now
: DateTimeOffset.Parse(content.Metadata.Date);
// 如果不存在最后的更新时间,就把更新时间设置为发布时间
DateTimeOffset updateTime = content.Metadata.UpdateTime is null
? publishDate
: DateTimeOffset.Parse(content.Metadata.UpdateTime);
uint wordCount = GetWordCount(content);
BlogEssay essay = new()
{
Title = content.Metadata.Title ?? content.BlogName,
@@ -54,9 +46,8 @@ public partial class RendererService(
IsDraft = content.IsDraft,
Description = GetDescription(content),
WordCount = wordCount,
ReadTime = readTime,
PublishTime = publishDate,
UpdateTime = updateTime,
ReadTime = CalculateReadTime(wordCount),
PublishTime = content.Metadata.Date ?? DateTime.Now,
HtmlContent = content.Content
};
@@ -191,21 +182,28 @@ public partial class RendererService(
string description = builder.ToString();
logger.LogDebug("Description of {name} is {desc}.", content.BlogName,
logger.LogDebug("Description of {} is {}.", content.BlogName,
description);
return description;
}
private (uint, string) GetWordCount(BlogContent content)
private uint GetWordCount(BlogContent content)
{
uint count = MarkdownWordCounter.CountWord(content);
int count = (from c in content.Content
where char.IsLetterOrDigit(c)
select c).Count();
logger.LogDebug("Word count of {blog} is {count}", content.BlogName,
logger.LogDebug("Word count of {} is {}", content.BlogName,
count);
// 据说语文教学大纲规定中国高中生阅读现代文的速度是600字每分钟
uint second = count / 10;
TimeSpan span = new(0, 0, (int)second);
return (uint)count;
}
return (count, span.ToString("mm'分'ss'秒'"));
private static string CalculateReadTime(uint wordCount)
{
// 据说语文教学大纲规定中国高中生阅读现代文的速度是600字每分钟
int second = (int)wordCount / 10;
TimeSpan span = new(0, 0, second);
return span.ToString("mm'分 'ss'秒'");
}
}

View File

@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="ImageFlow.NativeRuntime.ubuntu-x86_64" Version="2.1.0-rc11" Condition="$([MSBuild]::IsOsPlatform('Linux'))"/>
<PackageReference Include="ImageFlow.NativeRuntime.osx-arm64" Version="2.1.0-rc11" Condition="$([MSBuild]::IsOsPlatform('OSX'))"/>
<PackageReference Include="ImageFlow.NativeRuntime.ubuntu-x86_64" Version="2.1.0-rc11"/>
<PackageReference Include="ImageFlow.Net" Version="0.13.2"/>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/>
<PackageReference Include="AngleSharp" Version="1.1.0"/>
@@ -11,13 +10,30 @@
</ItemGroup>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<ClientAssetsRestoreCommand>pnpm install</ClientAssetsRestoreCommand>
<ClientAssetsBuildCommand>pwsh build.ps1 tailwind</ClientAssetsBuildCommand>
</PropertyGroup>
<Target Name="EnsurePnpmInstalled" BeforeTargets="BeforeBuild">
<Message Importance="low" Text="Ensure pnpm is installed..."/>
<Exec Command="pnpm --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
</Exec>
<Error Condition="$(ErrorCode) != 0" Text="Pnpm is not installed which is required for build."/>
<Message Importance="normal" Text="Installing pakages using pnpm..."/>
<Exec Command="pnpm install"/>
</Target>
<Target Name="TailwindGenerate" AfterTargets="EnsurePnpmInstalled" BeforeTargets="BeforeBuild" Condition="'$(_IsPublishing)' == 'yes'">
<Message Importance="normal" Text="Generate css files using tailwind..."/>
<Exec Command="pnpm tailwindcss -i wwwroot/tailwind.css -o $(IntermediateOutputPath)tailwind.g.css"/>
<ItemGroup>
<Content Include="$(IntermediateOutputPath)tailwind.g.css" Visible="false" TargetPath="wwwroot/tailwind.g.css"/>
</ItemGroup>
</Target>
</Project>

View File

@@ -6,4 +6,5 @@
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using YaeBlog
@using YaeBlog.Components

View File

@@ -37,7 +37,7 @@
"Name": "万木长风",
"Description": "世界渲染中...",
"Link": "https://ryohai.fun",
"AvatarImage": "https://ryohai.fun/static/favicons/favicon-32x32.png"
"AvatarImage": "https://ryohai.fun/icon.jpg"
}
]
}

View File

@@ -1,124 +0,0 @@
#!pwsh
[cmdletbinding()]
param(
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "Specify the build target")]
[ValidateSet("tailwind", "publish", "compress", "build", "dev", "new")]
[string]$Target,
[string]$Output = "wwwroot",
[string]$Essay,
[switch]$Compress
)
begin {
Write-Host "Building $Target..."
if ($Target -eq "publish")
{
if ($Essay -eq "")
{
Write-Error "No publish target, please add with --essay argument."
exit 1
}
}
if ($Target -eq "new")
{
if ($Essay -eq "")
{
Write-Error "No new name, please add with --essay argument."
exit 1
}
}
}
process {
function Compress-Image
{
Write-Host "Compress image assets..."
dotnet run -- compress --dry-run
$confirm = Read-Host "Really compress images? (y/n)"
if ($confirm -notmatch "^[yY]$")
{
Write-Host "Not compress images."
return
}
Write-Host "Do compress image..."
dotnet run -- compress
dotnet run -- scan
$confirm = Read-Host "Really delete unused images? (y/n)"
if ($confirm -notmatch "^[yY]$")
{
Write-Host "Not delete images."
return
}
Write-Host "Do delete unused images.."
dotnet run -- scan --rm
}
function Build-Image
{
$commitId = git rev-parse --short=10 HEAD
dotnet publish
podman build . -t ccr.ccs.tencentyun.com/jackfiled/blog --build-arg COMMIT_ID=$commitId
}
function Start-Develop {
Write-Host "Start tailwindcss and dotnet watch servers..."
$pnpmProcess = Start-Process pnpm "tailwindcss -i wwwroot/tailwind.css -o obj/Debug/net10.0/ClientAssets/tailwind.g.css -w" `
-PassThru
try
{
Write-Host "Started pnpm process exit? " $pnpmProcess.HasExited
Start-Process dotnet "watch -- serve" -PassThru | Wait-Process
}
finally
{
if ($pnpmProcess.HasExited)
{
Write-Error "pnpm process has exited!"
exit 1
}
Write-Host "Kill tailwindcss and dotnet watch servers..."
$pnpmProcess | Stop-Process
}
}
switch ($Target)
{
"tailwind" {
Write-Host "Build tailwind css into $Output."
pnpm tailwindcss -i wwwroot/tailwind.css -o $Output/tailwind.g.css
break
}
"publish" {
Write-Host "Publish essay $Essay..."
dotnet run -- publish $Essay
if ($Compress)
{
Compress-Image
}
break
}
"compress" {
Compress-Image
break
}
"build" {
Build-Image
break
}
"dev" {
Start-Develop
break
}
"new" {
dotnet run -- new $Essay
}
}
}

View File

@@ -1,13 +1,13 @@
version: '3.8'
services:
blog:
image: registry.cn-beijing.aliyuncs.com/jackfiled/blog:latest
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.blog.rule=Host(`rrricardo.top`) || Host(`www.rrricardo.top`)"
- "traefik.http.services.blog.loadbalancer.server.port=8080"
- "traefik.http.routers.blog.tls=true"
- "traefik.http.routers.blog.tls.certresolver=myresolver"
- "com.centurylinklabs.watchtower.enable=true"
blog:
image: registry.cn-beijing.aliyuncs.com/jackfiled/blog:latest
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.blog.rule=Host(`rrricardo.top`) || Host(`www.rrricardo.top`)"
- "traefik.http.services.blog.loadbalancer.server.port=8080"
- "traefik.http.routers.blog.tls=true"
- "traefik.http.routers.blog.tls.certresolver=myresolver"
- "com.centurylinklabs.watchtower.enable=true"

View File

@@ -1,12 +1,11 @@
---
title: 2021年终总结
date: 2022-01-12T16:27:19.0000000
date: 2022-01-12 16:27:19
tags:
- 杂谈
- 年终总结
- 杂谈
- 年终总结
---
2021年已经过去2022年已经来临。每每一年开始的时候我都会展开一张纸或者新建一个文档思量着又是一年时光也该同诸大杂志一般写几句意味深长的话语怀念过去的时光也祝福未来的自己。可往往脑海中已是三万字的长篇落在笔头却又是一个字都没有了。
如今跨年的时候已经过去朋友圈中已经不见文案的踪影我也该重新提笔细说自己2021年中做过的种种。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

@@ -1,13 +1,12 @@
---
title: 2022年终总结
date: 2022-12-30T14:58:12.0000000
tags:
- 杂谈
- 年终总结
- 杂谈
- 年终总结
date: 2022-12-30 14:58:12
---
2022是困难的一年。我们需要为2023年做好准备。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 54 KiB

View File

@@ -1,11 +1,11 @@
---
title: 2022年暑假碎碎念
date: 2022-08-22T15:39:13.0000000
tags:
- 杂谈
- 杂谈
typora-root-url: 2022-summer-vacation
date: 2022-08-22 15:39:13
---
在8个月的漫长寒假的最后两个月~~也就是俗称的暑假中~~,我都干了些什么?
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 134 KiB

View File

@@ -1,12 +1,11 @@
---
title: 2023年年终总结
date: 2024-02-29T20:18:19.0000000
tags:
- 杂谈
- 年终总结
- 杂谈
- 年终总结
date: 2024-2-29 20:18:19
---
虽然2023年已经过去了两个月但是年终总结还是要发的。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -6,7 +6,6 @@ tags:
- 年终总结
---
欸,年终总结难道不是应该在新年当天发出吗,什么已经是新年第三天了?!
然而年末偶遇流感病毒,头疼脑热强如怪物,拼尽全力也无法战胜。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 38 KiB

View File

@@ -1,224 +0,0 @@
---
title: ASP.NET Core中的静态Web资产
date: 2026-01-04T16:36:36.5629759+08:00
tags:
- 技术笔记
- dotnet
- ASP.NET Core
---
Web服务器应该如何扫描与提供静态Web文件尤其是在考虑到缓存、压缩的情况下还需要正确的处理开发环境和部署环境之间的差异让我们来看看ASP.NET Core是如何处理这个问题的。以及如何将通过其他工具例如`pnpm`生成的前端资产文件集成到ASP.NET Core中。
<!--more-->
### 引言——Blazor开发中的静态Web资源
Blazor是ASP.NET Core中~~新推出的~~Web应用程序开发框架通过一系列精巧的设计实现了使用HTML和C#编写运行在浏览器中的应用程序避免了使用丑陋的JavaScript。但是现代的前端开发生态几乎都建立在JavaScript之上尤其是考虑在JavaScript在很长的一段时间都是浏览器唯一支持的脚本语言在Blazor项目开发的过程中必然会遇到一些只能编写JavaScript才能解决的问题。同时一系列的现代前端工具例如[tailwindcss](https://tailwindcss.com/)提供了更加优秀的前端开发体验但是这些都基于NodeJS和NPM等前端工具。以上的前端生态引入了一个问题如何在MSBuild驱动的Blazor应用构建流程中自然地运行前端工具链和ASP.NET Core支持的服务器部署生成的静态资源
Blazor目前提供了一个入口简洁但是功能丰富的静态Web资源提供功能。在使用默认应用目录的情况下项目将会提供一个`wwwroot`文件夹,这个文件夹中的内容将可以从`/`直接寻址。为了提升前端静态文件的使用体验,该文件夹下的文件将会经过一个复杂的管道:
- 在构建扫描到这些资产文件之后MSBuild将会给静态文件加上内容指纹以防止重复使用旧文件。资源还会被压缩以减少资产交付的时间。
- 在运行时,所发现的资产文件将会作为终结点公开,并添加上合适的缓存头和内容类型头。在设置`ETtag``Last-Modified``Content-Type`头之后,浏览器将可以合理的缓存这些静态文件直到应用更新。
该静态文件功能还需要适应应用程序的部署状态:
- 在开发时,或者说运行`dotnet run``dotnet build`该功能需要将对应的静态文件终结点URL映射到磁盘上存储的实际静态文件上就像它们实际上就在`wwwroot`文件夹中一样。考虑到实际上开发过程中会用到Blazor内部的资产文件`blazor.web.js`,引用项目中的资产文件等等,这实际上一个相当复杂的检测-映射流程。
- 在发布时,或者说运行`dotnet publish`时,该功能需要收集所有需要的静态文件并复制到最终发布文件夹的`wwwroot`文件夹之下。
### Microsoft.AspNetCore.ClientAssets
在默认的应用模板下如果需要使用其他的现代前端工具生成静态资产文件最简单的方法就是手动或者编写MSBuild目标Target生成资产文件并放在`wwwroot`文件夹中。但是这个方法存在着如下几个问题:
- 开发者需要编写 MSBuild 目标targets来调用他们的工具。
- 开发者通常没有在构建过程的恰当时机执行其自定义目标。
- 开发者将工具生成的输出文件放入应用的 wwwroot 文件夹中,这会导致这些文件在下一次构建时被误认为是输入文件。
- 开发者没有为这些工具正确声明输入和输出依赖,导致即使输出文件已是最新,工具仍会重复运行,从而增加构建耗时。
面对这些问题M$提供了一个Alpha状态的库`Microsoft.AspNetCore.ClientAssets`来解决这个问题。不幸的是,这个库已经因为年久失修(上一次[更新](https://github.com/aspnet/AspLabs/pull/572)是在3年前引入对于.NET 7的支持在.NET 9引入新的静态资产部署管线之后使用会直接报错了。
^^ 相关的Issues链接[#38445](https://github.com/dotnet/aspnetcore/issues/38445)[#62925](https://github.com/dotnet/aspnetcore/issues/62925)
为了良好地解决如上的问题我们需要首先了解一下ASP.NET Core中静态资产文件的构建和部署过程。
### StaticWebAssetsSdk
在.NET中构建静态资产文件的相关代码在[dotnet/sdk](https://github.com/dotnet/sdk)仓库中,称作`StaticWebAssetsSdk`
静态 Web 资源会接管应用程序 wwwroot 文件夹中的内容项,并全面管理这些内容。在开发过程中,系统会生成一个 JSON 清单manifest其中包含以下信息
- 版本号version number标识清单格式的版本。
- 清单内容的哈希值hash用于判断清单内容是否发生变化。
- 库的包 IDlibrary package id用于区分当前项目与其他项目所提供的资源。
- 库的资源基础路径asset base path在将其他库的路径应用“发现模式”时用于确定要添加的基础路径。
- 清单模式manifest mode定义来自特定项目的资源在构建和发布时应如何处理。
- 相关项目清单及其哈希值的列表:用于判断自清单生成以来,项目引用是否发生变化,或是否有清单被更新。
- “发现模式”discovery patterns列表用于在清单构建完成后有选择性地在运行时提供某些资源。例如可以使用如下模式
```json
{ "Path": "<Project>/Pages", "BasePath": "_content/Something", "Pattern": "**/*.js" }
```
表示仅提供该目录下扩展名为`js`的文件。如果有人添加了图片或其他文件,它们将不会被提供。(这一点很重要,因为这些文件并不符合任何资源规则,也不会包含在发布输出目录中。)
- 构建/发布过程中生成的静态 Web 资源列表。
系统会生成两套清单:**构建清单build manifest** 和 **发布清单publish manifest**
- **构建清单**在构建过程中生成,用于开发阶段,使资源表现得如同它们属于应用程序本身。
- **发布清单**在发布过程中生成,记录了发布阶段对资源执行的所有转换操作。
资源可以在构建阶段或发布阶段定义,并可在任意阶段被标记为“仅构建”或“仅发布”。例如,你可以有两个文件:一个用于开发,一个用于发布,但它们都需要通过相同的 URL 路径提供服务。`service-worker.js` 就是一个典型例子。
**构建时清单**由项目中发现的资源以及来自被引用项目和包的资源共同组成。
**发布清单**则以构建清单为基础,过滤掉仅用于构建的文件,并包含在发布过程中对这些文件执行的所有转换(如链接、打包、压缩等)。
这种机制使得在发布阶段可以执行如链接Linking、打包Bundling、压缩Compression等优化操作。被引用的项目也会生成自己的发布清单其内容会在发布过程中与当前项目的清单合并。同时在发布过程中我们仍会保留被引用项目的原始构建清单以便应用程序可以选择忽略被引用项目的发布资源并对整个依赖传递闭包中的资源执行全局优化。例如一个类库在发布时可能生成一个压缩后的 JS 包,而主应用可以选择不使用多个独立的包,而是收集所有原始构建阶段的资源,生成一个统一的包。通常情况下,构建清单和发布清单内容相同,除非存在仅在发布阶段才应用的转换。
每份清单中会列出在构建/发布过程中生成或计算出的所有资源及其属性。这些属性包括:
- **Identity**:资源的唯一标识(文件的完整路径)。
- **SourceType**:资源类型('Discovered', 'Computed', 'Project', 'Package')。
- **ContentRoot**:开发阶段资源暴露的原始路径。
- **BasePath**:资源暴露的基础路径。
- **RelativePath**:资源的相对路径。
- **AssetKind**:资源用途('Build', 'Publish', 'All'),由 `CopyToOutputDirectory` / `CopyToPublishDirectory` 推断得出。
- **AssetMode**:资源作用范围('CurrentProject', 'Reference', 'All')。
- **AssetRole**:资源角色('Primary', 'Related', 'Alternative')。
- **AssetMergeSource**:当资源被嵌入到其他 TFM目标框架时的来源。
- **AssetMergeBehavior**:当同一 TFM 中出现资源冲突时的合并行为。
- **RelatedAsset**:当前资源所依赖的主资源的 Identity。
- **AssetTraitName**:区分相关或替代资源与主资源的特征名称(如语言、编码格式等)。
- **AssetTraitValue**:该特征的具体值。
- **CopyToBuildDirectory**:与 Content 项一致(如 PreserveNewest、Always
- **CopyToPublishDirectory**:与 Content 项一致。
- **OriginalItemSpec**:定义该资源的原始项规范。
关于资源在不同场景下的使用(作为主项目的一部分,或作为被引用项目的一部分),有三种可能的选项:
- **All**:资源在所有情况下都应被使用。
- **Root**:资源仅在当前项目作为主项目构建时使用。
- **Reference**:资源仅在当前项目被其他项目引用时使用。
例如CSS 隔离CSS isolation生成的两个包
- `<<Project>>.styles.css` 是 **Root** 资源,仅在作为主项目时使用。
- `<<Project>>.lib.bundle.css` 是 **Reference** 资源,仅在被其他项目引用时使用。
除了上述三种使用模式,项目还需定义其在构建和发布过程中如何处理清单中的文件。对此有三种模式:
- **Default**:项目在发布时将所有内容复制到发布输出目录,但当被其他项目引用时不做任何操作,而是期望引用方负责处理静态 Web 资源的发布。
→ 通常用于类库class libraries
- **Root**:项目被视为静态 Web 资源的“根”,即使被引用,其资源也应像主项目一样被处理(例如,不复制传递依赖资源,而只复制 Root 资源)。
→ 用于如 Blazor WebAssembly 托管项目(被 ASP.NET Core 主机项目引用,但资源应视为根项目)。
- **Isolated**:与 Root 类似,但引用项目完全不知道静态 Web 资源的存在;当前项目会自行在发布时设置处理程序,将资源复制到正确位置。
→ 用于如 Blazor 桌面应用,将静态 Web 资源自动纳入 `GetCopyToPublishDirectoryItems`,使引用方无需了解静态 Web 资源机制。
关于资源类型,静态 Web 资源可分为四类:
- **Discovered assets**:从项目中已有项(如 Content、None 等)中发现的资源。
- **Computed assets**:在构建过程中生成、需要在构建时复制到最终位置的资源。
- **Project**:来自被引用项目的资源。当合并被引用项目的清单时,其 Discovered 和 Computed 资源会转换为 Project 类型。
- **Package**:来自被引用 NuGet 包的资源。
关于资源角色Asset Role有三种
- **Primary主资源**:表示可通过其相对路径直接访问的资源。大多数资源属于此类。
- **Related相关资源**:表示与另一个资源相关,但两者都可通过各自的相对路径独立访问。
- **Alternative替代资源**:表示是另一个资源的替代形式,例如预压缩版本或不同格式版本。通常应通过与主资源相同的相对路径提供(具体实现由运行时决定)。静态 Web 资源层仅记录这种关系。
对于 Related 和 Alternative 资源,其 `RelatedAsset` 属性指向其所依赖的主资源。这种依赖链可多层嵌套,以表示一个资源的多种表示形式。静态 Web 资源仅记录这些信息,具体如何使用由 MSBuild 目标决定。
`AssetTraitName` 和 `AssetTraitValue` 用于区分相关/替代资源与其主资源。例如:
- 对于全球化程序集可记录程序集的文化culture
- 对于压缩资源,可记录编码方式(如 gzip、brotli
下图展示了在构建过程中被调用的MSBuild Target
![image-20251231225433184](./aspnetcore-swa/image-20251231225433184.webp)
Sdk提供了一些重要的MSBuild Task供程序员调用
- `DefineStaticWebAssets`该Task扫描提供了一系列候选的资产文件并构建一个*标准化的*静态资产对象;
- `DefineStaticWebAssetEndpoints`该Task以上一个任务输出的静态资产对象为输入输出每个静态资产文件的Web终结点
在构建中过程中`GenerateStaticWebAssetsManifest`和`GenerateStaticWebAssetsDevelopmentManifest`、`GenerateStaticWebAssetEndpointsManifest`等几个任务会产生一个重要的清单文件,这些文件通常存放在*obj*文件夹中,名称为`staticwebassets.*.json`。其中一个较为重要的清单文件是`staticwebassets.development.json`,其存储了所有的静态资产文件和对应的存储目录。这个文件在构建的过程中会被复制到输出目录`bin`中,名称为`$(PackageId).staticwebassets.runtime.json`。这个文件将会在生产模式下被静态资产中间件读取,作为建立静态文件终结点到实际物理文件的索引。这个文件也为需要调试`StaticWebAssetsSdk`的程序员提供了重要的调试信息是解决ASP.NET Core中静态资产问题的不二法门。
### 解决方案
现在已经充分了解了`StaticWebAssetsSdk`可以来设计在MSBuild中集成前端工具并生成最终静态资产文件的管线了。
首先来研究过程的步骤,`npm`或者其类似物也使用类似于MSBuild的先还原再构建两步首先需要安装程序中使用到的包然后在运行构建指令构建对应的静态文件构建完成之后还需要将构建产物交给MSBuild中的静态资产处理管线进行进一步的处理。因此设计如下的三个步骤
1. `RestoreClientAssets`这个Target需要运行`npm install`或者类似的指令安装依赖包;
2. `BuildClientAssets`这个Target运行`npm run build`或者类似的指令构建项目;
3. `DefineClientAssets`这个Target调用`DefineStaticWebAssets`等Task声明静态资产文件。
确定好生成步骤之后,声明一下会在生成过程中会用到的,可以提供给用户自定义的属性。安装和构建的相应软件包肯定是需要提供给用户自定义的。在一般情况下,前端工具链把将静态文件生成到`dist`文件夹中。为了符合MSBuild的惯例这里将中间静态文件生成到*obj*文件夹下的`ClientAssets`文件夹中。为了实现这一点,构建过程中的指令就需要支持一个指定生成目录的参数,这个参数也作为一个属性暴露给用户可以自定义。这里就形成了下面三个提供给用户自定义的参数。
```xml
<PropertyGroup>
<ClientAssetsRestoreCommand Condition="'$(ClientAssesRestoreCommand)' == ''">pnpm install</ClientAssetsRestoreCommand>
<ClientAssetsBuildCommand Condition="'$(ClientAssetsBuildCommand)' == ''">pnpm run build</ClientAssetsBuildCommand>
<ClientAssetsBuildOutputParameter Condition="'$(ClientAssetsBuildOutputParameter)' == ''">--output</ClientAssetsBuildOutputParameter>
</PropertyGroup>
```
最终就是完成的构建原始代码了。第一个运行的构建目标`RestoreClientAssets`将会在`DispatchToInnerBuilds`任务运行之后运行这个目标是MSBuild构建管线中一个不论是针对单架构生成还是多架构生成都只会运行一次的目标这样在项目需要同时编译到.NET 8和.NET 10的情况下仍然只会运行前端的安装命令一次。`BuildClientAssets`目标紧接着`RestoreClientAssets`目标的运行而运行,并将所有生成的前端文件添加到`_ClientAssetsBuildOutput`项中。最终的`DefineClientAssets`目标在负责解析项目中的所有静态文件的目标`ResolveWebAssetsConfiguration`运行之前运行,调用`DefineStaticWebAssets`和`DefineStaticWebAssetEndpoints`将前面生成的所有前端静态文件添加到MSBuild的静态文件处理管线中。
```xml
<PropertyGroup>
<_RestoreClientAssetsBeforeTargets Condition="'$(TargetFramework)' == ''">DispatchToInnerBuilds</_RestoreClientAssetsBeforeTargets>
</PropertyGroup>
<Target Name="RestoreClientAssets" BeforeTargets="$(_RestoreClientAssetsBeforeTargets)">
<Message Importance="high" Text="Running $(ClientAssetsRestoreCommand)"/>
<Exec Command="$(ClientAssetsRestoreCommand)"/>
</Target>
<Target Name="BuildClientAssets" DependsOnTargets="RestoreClientAssets" BeforeTargets="AssignTargetPaths">
<PropertyGroup>
<_ClientAssetsOutputFullPath>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)ClientAssets'))</_ClientAssetsOutputFullPath>
</PropertyGroup>
<MakeDir Directories="$(_ClientAssetsOutputFullPath"/>
<Exec Command="$(ClientAssetsBuildCommand) -- $(ClientAssetsBuildOutputParameter) $(_ClientAssetsOutputFullPath)"/>
<ItemGroup>
<_ClientAssetsBuildOutput Include="$(IntermediateOutputPath)ClientAssets\**"/>
</ItemGroup>
</Target>
<Target Name="DefineClientAssets" AfterTargets="BuildClientAssets" DependsOnTargets="ResolveWebAssetsConfiguration">
<ItemGroup>
<FileWrites Include="@(_ClientAssetsBuildOutput)"/>
</ItemGroup>
<DefineStaticWebAssets
CandidateAssets="@(_ClientAssetsBuildOutput)"
SourceId="$(PackageId)"
SourceType="Computed"
ContentRoot="$(_ClientAssetsOutputFullPath)"
BasePath="$(StaticWebAssetBasePath)"
>
<Output TaskParameter="Assets" ItemName="StaticWebAsset"/>
<Output TaskParameter="Assets" ItemName="_ClientAssetsStaticWebAsset"/>
</DefineStaticWebAssets>
<DefineStaticWebAssetEndpoints
CandidateAssets="@(_ClientAssetsStaticWebAsset)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="StaticWebAssetEndpoint" />
</DefineStaticWebAssetEndpoints>
</Target>
```
为了测试如下的代码,可以在项目中新建一个`Directory.Build.targets`文件,将上述的内容复制进去进行测试,当然别忘了用`<Project>`标签包裹这一切。

Binary file not shown.

View File

@@ -1,11 +1,11 @@
---
title: 人生代码大作业初体验
date: 2022-07-27T11:34:49.0000000
tags:
- 杂谈
- 杂谈
typora-root-url: big-homework
date: 2022-07-27 11:34:49
---
在大学也呆了一年了,终于遇上了第一个需要多人合作的写代码项目。从四月底分组完成,任务部署下来到七月初接近尾声,在这两个多月的时间里,也算是经历了不少,学到了不少。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -1,12 +1,12 @@
---
title: 建立博客过程的记录
date: 2022-04-08T11:52:32.0000000
typora-root-url: 建立博客过程的记录
date: 2022-04-08 11:52:32
tags:
- 技术笔记
- 技术笔记
---
当我已经在Python的浩瀚大海遨zhengzha了半个暑假后我决定尝试一下传说中程序员专用的学(zhuang)习(bi)手(fangfa)段(fa)——建立自己的个人博客。作为一个半懂不懂的Python程序员心中冒出的第一个想法自然是采用Python的Django作为开发自己的个人博客的手段。然而在阅读了[用Django搭建个人博客](https://www.dusaiphoto.com/article/2/)等的其他人搭建这类动态博客的过程记录之后我便义无反顾的转向了采用javascript开发的博客框架[Hexo](https://hexo.io)<del>说好的Python信仰呢</del>。无他,唯简单尔。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 79 KiB

View File

@@ -1,12 +1,12 @@
---
title: C项目中有关头文件的一些问题
date: 2022-05-08T11:35:19.0000000
tags:
- 技术笔记
- C/C++
- 技术笔记
- C/C++
typora-root-url: c-include-problems
date: 2022-05-08 11:35:19
---
最近在完成一门`C`语言课程的大作业,课设老师要求我们将程序分模块的开发。在编写项目头文件的时候,遇到了一些令本菜鸡大开眼界的问题。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 205 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 238 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 639 KiB

View File

@@ -1,12 +1,11 @@
---
title: 编译MediaPipe框架
date: 2022-11-11T22:20:25.0000000
tags:
- C/C++
- 技术笔记
- C/C++
- 技术笔记
date: 2022-11-11 22:20:25
---
编译MediaPipe框架。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 57 KiB

View File

@@ -1,12 +1,12 @@
---
title: 日用Linux挑战 第0篇 初见Arch Linux
date: 2023-01-15T22:23:08.0000000
tags:
- Linux
- 杂谈
- Linux
- 杂谈
date: 2023-01-15 22:23:08
typora-root-url: daily-linux-0
---
在将开发重心移到`WSL`上一年之后我最终还是决定完全抛弃Windows转向使用Linux作为我日常使用的主力系统。目前我已经使用Linux作为主力系统一个月了。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 73 KiB

View File

@@ -1,13 +1,13 @@
---
title: 日用Linux挑战 第2篇 Wayland
date: 2023-07-23T11:44:34.0000000
tags:
- 杂谈
- Linux
- 杂谈
- Linux
date: 2023-07-23 11:44:34
typora-root-url: daily-linux-2
---
使用`Linux`6个月我成功戒掉了原神。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 53 KiB

View File

@@ -1,13 +1,13 @@
---
title: 日用Linux挑战 第3篇 放弃Wayland
date: 2023-09-04T14:47:46.0000000
tags:
- 杂谈
- Linux
- 杂谈
- Linux
typora-root-url: daily-linux-3
date: 2023-09-04 14:47:46
---
成也开源,败也开源。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -214,7 +214,7 @@ void Run(string name, Action<int> body)
| -------- | ------------- | ------------- | ------------ |
| 原子指令 | 2,000,000,000 | 2,000,000,000 | 22241.9848ms |
| 朴素 | 2,000,000,000 | 220,525,235 | 277.3435ms |
| 随机 | 2,000,000,000 | 2,024,587,268 | 527.5323ms |
| 随机 | 2,000,000,000 | 2,024,,587,268 | 527.5323ms |
从数据上就可以发现,新方法可以在和朴素方法接近的运行时间下获得和使用原子指令接近的实际数值,而且运行时间会随着数值的增加进一步的减少,逐渐逼近朴素方法的运行时间。
@@ -457,170 +457,4 @@ static int ConditionalSelect(bool condition, int whenTrue, int whenFalse) =>
.NET提供的一种特性就是运行时安全这其中重要的一点就是对于数组、字符串和切片在运行时进行边界检查。但是这些边界检查就会在实际生成的代码中生成大量的分支判断这会导致程序运行的效率严重下降。因此如何让编译器在能够保证访问安全的情况下消除掉部分不必要的边界检查是编译器优化中的一个重要课题。
例如在一个常用数据结构——哈希表中,通常的实现是计算键的哈希值,并利用该哈希值作为下标在数组中获得存储的对象。考虑到哈希值是一个`int`类型的变量但是哈希表中很少需要存储高达21亿对象因此往往需要对哈希值取模之后再作为数组的下标此时取模的值常常就是数组的长度。也就是说在这种情况下对于数组的访问是不可能出现越界的情况下。因此编译器可以为类似与如下的代码取消访问数组时的边界检查
```csharp
public class Tests
{
private readonly int[] _array = new int[7];
public int GetBucket() => GetBucket(_array, 42);
private static int GetBucket(int[] buckets, int hashcode) =>
buckets[(uint)hashcode % buckets.Length];
}
```
同样的,对于下面这些代码,编译器也可以取消访问数组时的边界检查:
```csharp
public class Tests
{
private readonly string _s = "\"Hello, World!\"";
public bool IsQuoted() => IsQuoted(_s);
private static bool IsQuoted(string s) =>
s.Length >= 2 && s[0] == '"' && s[^1] == '"';
}
```
### 常量折叠
常量折叠Constant Folding同样是一个编译器在生成代码时可以进行的重要优化这让编译器在计算在编译器时就可以确定的值而不是让他们留到运行时进行。最朴素的常量折叠——例如计算一个数学表达式的值——在这里不在赘述。在上面介绍函数内联时也涉及到了常量折叠的内容分层编译的引入也会使得常量折叠的应用范围变广这些都不在这里重复。
进行常量折叠优化时一个重要的问题是“教会”编译器哪些变量是常量。这方面编译器得到的提升有:
- 可以将一个字面值字符串的长度视为一个常数;
- 在进行空安全的检查时字面值字符串是必定不为空的;
- 编译器在编译时除了可以进行一些简单的数学运算,现在整个`System.Math`命名空间中提供的算法都可以在编译时进行运算;
- `static readonly`类型的字符串和数组长度被视为一个常数;
- `obj.GetType()`现在在JIT编译器明确了解类型的情况下可以被替换为一个常量
- `DateTime`等时间类型初始化时可以在编译期计算内存存储的时间。例如对于`new DateTime(2023, 9, 1)`将会直接被编译到`new DateTime(0x8DBAA7E629B4000)`。
上述这些并不能完全覆盖在.NET 6到.NET 8三个大版本之中引入的所有JIT编译器优化但是从中也可以一窥编译器优化的精巧之处。首先编译器的优化并不是一个个独立优化策略的组合而且各种优化策略的有机组合。方法的内联就是一个典型例子通过将被调用方法的内容暴露给调用者或者反过来让其他的各种优化策略发挥更大的作用。其次JIT编译器在编译优化方面可以发挥更伟大的作用。通过在程序运行时对于运行环境和程序本身有着更加深刻的理解JIT编译器可以在运行时发挥出更高的性能。
## 内存管理
.NET中的垃圾回收器GC负责管理应用程序的内存分配和释放。每当有对象新建时运行时都会将从托管堆为对象分配内存主要托管堆中还有地址空间运行时就会从托管堆为对象分配内存。不过内存并不是无限的垃圾回收器就负责执行垃圾回收来释放一些内存。垃圾回收器的优化引擎会根据所执行的分配来确定执行收回的最佳时机。
.NET中内存管理中的一个显著变更为将内存的抽象从段Segment修改为区域Region。段和区域之前最明显的区别是大小段是较大的内存——在64位的机器上一个段的大小万网是1GB、2GB或者是4GB而区域是非常小的单元在默认情况下只有4MB的大小。从宏观上来说之前的GC是为每个代的堆维持一个GB级别的内存范围而现在GC则是维持了许多个较小的内存区域这些内存区域可以被分配给各个代的堆或者其他可能涉及的堆使用。
垃圾回收器中还有两个引人注意的特性增加。第一个是动态的代提升和下降Dynamic Promotion and Demotion`DPAD`第二个是动态适应应用程序大小Dynamic Adaptive To Application Size`DATAS`)。`DPAD`特性允许GC在工作的过程中动态的设置一个区域的代数例如直接将一个可能存活时间非常长的对象配置为第2代而这在之前的GC模型中需要通过两次垃圾回收才能实现。而第二个特性`DATAS`旨在适应应用程序的内存要求即应用程序堆的大小和长期数据大小大致成正比即使在不同规格的计算机上执行相同的工作时运行时中堆的大小也是类似的。相比如下传统的服务器模式下的GC旨在提高程序的吞吐量允许内存的分配量基于吞吐量而不是应用程序的大小。`DATAS`对于各种突发类型的工作负载是非常有利的,同时通过允许堆大小按照工作负载的要求进行调整,这将让一些内存首先的环境直接受益。
### 无垃圾回收的堆
在程序中大量会涉及到使用常量字符串的情形,例如下面这个例子:
```csharp
public class Tests
{
public string GetPrefix() => "https://";
}
```
在.NET 7平台上这个方法会被JIT编译器编译之后得到下面这段本机代码
```assembly
; Tests.GetPrefix()
mov rax,126A7C01498
mov rax,[rax]
ret
; Total bytes of code 14
```
在这段代码中使用了两个`mov`指令,其中第一个指令加载存储这个字符串对象地址的地址,第二个读取该地址。从这段本机代码可以看见,尽管已经是在处理一个常量的字符串,但是编译器和运行时仍然需要为这个字符串在堆上分配一个`string`对象因为一个在堆上分配的对象在GC的控制下会在内存中发生移动编译器就不能为这个对象使用一个固定的内存地址需用从一个指定的地址读取该对象所在的地址。如果能让这个常量字符串分配在不会移动的内存区域中就能从编译器和GC两个方面上提高程序运行的效率。
为了优化这种生成周期和程序一致对象的内存管理,.NET 8中引入了一个新的堆——没有内存管理的堆。JIT编译器将会保证这些常量类型的对象将会被分配在这个堆中这种没有GC管理的堆也意味着JIT编译器可以为这些对象使用一个固定的内存地址在使用时避免掉了一次内存读取。
![Heaps where .NET Objects Live](./dotnet-performance-8/HeapsWhereNetObjectsLive.webp)
将上述提高的示例代码使用.NET 8版本进行编译得到的代码如下从中也可以看出JIT编译器生成的代码只有一条`mov`指令,避免了一次内存访问。
```assembly
; Tests.GetPrefix()
mov rax,227814EAEA8
ret
; Total bytes of code 11
```
这个没有内存管理的堆引入还可以让其他的类型受益。例如对于`typeof(T)`返回的类型对象,容易想到一个程序集中所有类型对象的生命周期应该是和程序一致的,因此也可以在这个堆上分配所有这些类型对象。`Array.Empty<T>`也可以利用类似的思路分配在这个堆上。
### 值类型
因为可以避免在堆上分配内存,值类型已经在.NET的高性能代码中得到了广泛的应用虽然频繁的内存拷贝可能带来额外的性能开销。因此编译器对于值类型的各种优化就显得至关重要。
这部分优化中一个引人注目的点是值类型的“推广”promotion这里的推广意味着将一个结果划分为组成它的各种字段来区别对待。可以利用下面这个示例代码进行理解
```csharp
public class Tests
{
private ParsedStat _stat;
[Benchmark]
public ulong GetTime()
{
ParsedStat stat = _stat;
return stat.utime + stat.stime;
}
internal struct ParsedStat
{
internal int pid;
internal string comm;
internal char state;
internal int ppid;
internal int session;
internal ulong utime;
internal ulong stime;
internal long nice;
internal ulong starttime;
internal ulong vsize;
internal long rss;
internal ulong rsslim;
}
}
```
在这段代码中有一个较大的结构类型其的大小是80个字节。在没有启用推广的条件下进行运行`GetTime`方法编译得到的本机代码如下所示。在汇编代码中将下载栈上分配一片88字节的空间再将整个结构体直接复制到当前方法的栈上在复制完成之后计算两个字段的和并返回。
```assembly
; Tests.GetTime()
push rdi
push rsi
sub rsp,58
lea rsi,[rcx+8]
lea rdi,[rsp+8]
mov ecx,0A
rep movsq
mov rax,[rsp+10]
add rax,[rsp+18]
add rsp,58
pop rsi
pop rdi
ret
; Total bytes of code 40
```
而在打开推广的情况下运行得到的本机代码如下所示:
```assembly
; Tests.GetTime()
add rcx,8
mov rax,[rcx+8]
mov rcx,[rcx+10]
add rax,rcx
ret
; Total bytes of code 16
```
在这段汇编代码中JIT编译器只复制了两个需要使用的字段到当前方法的栈上这就大幅减少了值类型在方法调用之前产生内存复制开销。
## 还有更多……
行文至此,本篇已经字数超过一万字了,毫无疑问这将成为博客历史上最长的一篇文章。在这点字数中我们还只是**简略**的介绍了一下.NET平台过去的几个版本中涉及到的优化还主要聚焦于JIT编译器和内存管理的部分在这两个部分之后还有一个线程管理部分也是影响性能的关键组件同时.NET还提供了一个由数千个API组成的运行库这些类型中无论是基元类型还是泛型集合类型都获得了若干提升这些部分共同组成了这几个版本的性能奇迹。
本篇文章中的主要内容来自于.NET运行时仓库中的[Book of the Runtime](https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/README.md)和微软开发者博客上的[Performance Improvements in .NET 6](https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/)、[Performance Improvements in .NET 7](https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/)和[Performance Improvements in .NET 8](https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/#whats-next)等几篇文章,上述没有覆盖到的内容推荐读者这些文章。同时算算时间,.NET 9版本引入的性能提升文章应该也要发布了。
回到文章最开始时的问题JIT编译就一定比AOT编译慢吗从启动速度上来说JIT编译当然是完败AOT编译但是在程序长时间运行各项设备JIT编译器、运行时和GC等预热完成之后则是鹿死谁手犹未可知了。
例如在一个常用数据结构——哈希表中,通常的实现是计算键的哈希值,并利用该哈希值作为下标在数组中获得存储的对象。考虑到哈希值是一个

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 B

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -1,11 +1,11 @@
---
title: 环境配置备忘录
date: 2022-01-15T20:19:39.0000000
date: 2022-01-15 20:19:39
tags:
- 技术笔记
- 技术笔记
typora-root-url: 环境配置
---
电脑上的环境三天两头出问题,写下一个备忘录记录一下电脑上环境的配置过程。
<!--more-->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 8.6 KiB

Some files were not shown because too many files have changed in this diff Show More