Skip to content

Instantly share code, notes, and snippets.

View ufcpp's full-sized avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
@ufcpp
ufcpp / GCHandleReuse.cs
Created November 17, 2017 11:07
GCHandle の値、どこかのスロットのアドレスがたぶん帰ってきてる。かつ、Free したスロットは即座に再利用されてる
using System;
using System.Runtime.InteropServices;
class X { }
class Y { }
public class Program
{
static void Main()
{
unsafe
{
Circle circle = new() { Radius = 1 };
Rectangle rectangle = new() { Width = 2, Height = 3 };
Rectangle square = new() { Width = 2, Height = 2 };
Console.WriteLine(Algorithm.AreaRatio(0, &circle));
Console.WriteLine(Algorithm.AreaRatio(1, &rectangle));
Console.WriteLine(Algorithm.AreaRatio(1, &square));
for (uint i = 0; i < 12; i++)
{
Console.WriteLine(format(i));
}
for (uint i = 15; i < 120; i+=7)
{
Console.WriteLine(format(i));
}
Console.WriteLine(format(1234));
@ufcpp
ufcpp / Lambda.cs
Created November 26, 2017 06:41
静的メソッドをデリゲート化した場合の呼び出し負荷、思ったよりもでかかった
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
static class Lambda
{
// Roslyn 実装では、ラムダ式に対して匿名クラスが作られて、インスタンス メソッドが作られる。
// インスタンスを作る分重たそうに見えるけど、実はこの方が速い。というか静的メソッドが遅い。
public static Action Nop { get; } = () => { };
}
@ufcpp
ufcpp / RelativePathPdb.csproj
Created April 3, 2018 03:44
PDB 中に含まれるソースコードのパスを相対パスに変えるための csproj 設定(Rosnly リポジトリ内を漁ってたらこういう書き方だった)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<!-- このオプションの指定で、タイムスタンプとかを決定論的なハッシュ値に置き換える -->
<Deterministic>true</Deterministic>
@ufcpp
ufcpp / Program.cs
Created October 28, 2016 03:33
index付きforeach
// 動作確認用
using System;
using System.Collections;
using System.Collections.Generic;
// ここの using 切り替えで、どの Indexed が呼ばれるかを切り替え
// 手元の環境だと以下の通り(GetTotalMemory の前後差分0が理想)
// A: 0
// B: 237568
// C: 720896
@ufcpp
ufcpp / CovariantDictionary.cs
Last active November 22, 2023 12:14
IReadOnlyDictionary の TValue に out つけれないの毎度つらい
using System.Collections;
using System.Runtime.CompilerServices;
var d = new Dictionary<int, string> { { 1, "one" }, { 2, "two" } };
ICovariantDictionary<int, string> cd = d.AsCovariant();
// TValue が out なので string → object 代入可能。
ICovariantDictionary<int, object> id = cd;
foreach (var (k, v) in id)
namespace TimeProviderExtension;
public class PausableTimeProvider(TimeProvider innerProvider) : TimeProvider
{
public PausableTimeProvider() : this(System) { }
private long _pausedTimestamp = innerProvider.GetTimestamp();
private long _offsetTimestamp = 0;
private DateTimeOffset _pausedUtcNow = innerProvider.GetUtcNow();
private TimeSpan _offsetUtcNow = default;
@ufcpp
ufcpp / RefImplicitCast.cs
Created September 21, 2023 13:39
Unsafe.AsRef implicit operator
using System.Runtime.CompilerServices;
var p = new Private();
Console.WriteLine(p.Value); // 0
Accessor r = p; // implicit cast で p の参照が r に渡る。
r.Value = 1;
Console.WriteLine(p.Value); // 書き変わってる。
// PrivateProxy 触りつつ、
@ufcpp
ufcpp / PCParamegers.cs
Created September 15, 2023 08:22
プライマリコンストラクター引数のキャプチャ
class A(int x)
{
int _x = x; // これはキャプチャしない。
}
class B(int x)
{
public int X { get; } = x; // これもキャプチャしない。
}