-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from Cysharp/feature/ReduceAllocations
Reduce allocations in requests and responses.
- Loading branch information
Showing
12 changed files
with
525 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Profiler.Api" Version="1.4.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\YetAnotherHttpHandler\YetAnotherHttpHandler.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Cysharp.Net.Http; | ||
using JetBrains.Profiler.Api; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
MemoryProfiler.CollectAllocations(true); | ||
|
||
using var httpHandler = new YetAnotherHttpHandler(); | ||
using var client = new HttpClient(httpHandler); | ||
|
||
var buffer = new byte[1024 * 64]; | ||
MemoryProfiler.GetSnapshot("Handler and HttpClient are created."); | ||
|
||
for (var i = 0; i < 10; i++) | ||
{ | ||
Console.WriteLine($"Request: Begin ({i})"); | ||
var stream = await client.GetStreamAsync("https://cysharp.co.jp"); | ||
while (await stream.ReadAsync(buffer) != 0) | ||
{ | ||
} | ||
|
||
MemoryProfiler.GetSnapshot($"Request End ({i})"); | ||
Console.WriteLine($"Request: End ({i})"); | ||
await Task.Delay(1000); | ||
} | ||
|
||
public class NativeLibraryResolver | ||
{ | ||
[ModuleInitializer] | ||
public static void Initialize() | ||
{ | ||
NativeLibrary.SetDllImportResolver(typeof(Cysharp.Net.Http.YetAnotherHttpHandler).Assembly, (name, assembly, path) => | ||
{ | ||
var ext = ""; | ||
var prefix = ""; | ||
var platform = ""; | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
platform = "win"; | ||
prefix = ""; | ||
ext = ".dll"; | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
platform = "osx"; | ||
prefix = "lib"; | ||
ext = ".dylib"; | ||
} | ||
else | ||
{ | ||
platform = "linux"; | ||
prefix = "lib"; | ||
ext = ".so"; | ||
} | ||
|
||
var arch = RuntimeInformation.OSArchitecture switch | ||
{ | ||
Architecture.Arm64 => "arm64", | ||
Architecture.X64 => "x64", | ||
Architecture.X86 => "x86", | ||
_ => throw new NotSupportedException(), | ||
}; | ||
|
||
return NativeLibrary.Load(Path.Combine($"runtimes/{platform}-{arch}/native/{prefix}{name}{ext}")); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.