Skip to content

Instantly share code, notes, and snippets.

View ufcpp's full-sized avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
@ufcpp
ufcpp / NumericInterfaces.cs
Last active June 26, 2022 14:58
generic math なインターフェイスの依存関係を mermaid グラフで出力
using System.Numerics;
//var interfaces = GetAllIntegerInterfaces();
var interfaces = GetAllFloatInterfaces();
WriteMermaidGraph(
from i in interfaces
from b in GetUniqueBaseInterfaces(i)
select (i.Name, b.Name));
@ufcpp
ufcpp / vs17_3p2.cs
Created June 15, 2022 02:08
C# 11、Merged into 17.3p2 になってるやつ
// https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md で
// Merged into 17.3p2 になってるやつ。
using System.Diagnostics.CodeAnalysis;
// DIM for Static Members
interface IA
{
// static abstract 自体これまで「RequiresPreviewFeatures」だったのであんまり「新機能」感はないけども。
// 17.3p5 から、static virtual も書けるように。
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System.Text;
var template = """
class MyClass
{
`*
var properties = new[] {
#nullable disable
#pragma warning disable IDE0004, IDE0049, IDE0052, IDE0055, IDE0060, CS1998, CS0626
using System.Runtime.CompilerServices;
using static System.Runtime.CompilerServices.Unsafe;
class A
{
Delegate _x;
public object X
@ufcpp
ufcpp / AsBenchmark.cs
Created May 18, 2022 05:04
Unsafe.As 展開
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Runtime.CompilerServices;
BenchmarkRunner.Run<AsBenchmark>();
class Util
{
public static long M1<T>(T x)
{
@ufcpp
ufcpp / ParamNull.cs
Last active May 11, 2022 03:35
C# 11 Preview in VS 17.3 Preview 1
// 【!! 終了のお知らせ】
// のはずなんだけど、 VS 17.3p1 ではまだ消えてなかった。
m(null);
static void m(string s!!) { }
@ufcpp
ufcpp / RefGenericParameter.cs
Created May 2, 2022 14:03
そんなとこで関数ポインター使う?と思った小ネタ
using System.Runtime.CompilerServices;
var r = new R();
// (.NET 7 で緩和予定だけど、今は) ref struct を型引数にできない。
//ref int r = ref Unsafe.As<R, int>(ref r); // CS0306 エラー
unsafe
{
// As<int, int> で関数ポインターを取っておいて、
@ufcpp
ufcpp / VirtualMethodPointer.cs
Created April 19, 2022 16:29
リフレクション越しにインターフェイスメソッドを呼ぶときにbox化は避けられるのかどうか
var it = typeof(IDisposable);
var t = typeof(S);
var map = t.GetInterfaceMap(typeof(IDisposable));
ulong guard1 = 0x8_0000_0009;
var s = new S { X = 1, Y = 2, Z = 3, W = 4 };
ulong guard2 = 0x6_0000_0007;
{
@ufcpp
ufcpp / SpanBenchmark.cs
Created January 30, 2022 05:23
ReadOnlySpan 最適化
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<SpanBenchmark>();
[MemoryDiagnoser]
public class SpanBenchmark
{
private const int N = 1000;
@ufcpp
ufcpp / NoEscapingEncoder.cs
Last active January 16, 2023 01:42
UnsafeRelaxedJsonEscaping でもエスケープしてしまう全角スペースとか + とかすら素通ししたい
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
var original = new A(123, "あいう abc \b/\r\n\t\"\\\'🐈");
var opt = new JsonSerializerOptions
{
//Encoder = new NoEscapingEncoder(),