Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Considerable performance fixes

オープン
#66 コメント 5 件 リアクション 4 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
30/100
issue の種類
リファクタリング
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp
領域
performance

調査の方向性

まず、参照されている src/Unity.Mathematics/int2.gen.cs の616行目と775行目付近の箇所を起点にし、次に、提案された2つのパターンが生成されたベクトル型全体にどの程度広く適用できるかを特定します。結果として生成された assembly を issue の例と比較し、影響を受けるインデクサーと例外パスが意図した動作を維持していることを確認します。

索引モデルが issue の本文から書いたものです。

説明

Hi there!
I noticed two problems that reduce performance and can easily be fixed 😄

1. Not using throw helpers

The jit will never inline a method if it throws an exception, even if you add the AgressiveInlining attribute.

Example: int2.gen.cs line:775

The common fix for this is very simple, just use a "throw helper", like this:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int select_shuffle_component(int2 a, int2 b, ShuffleComponent component)
{
    switch(component)
    {
        case ShuffleComponent.LeftX:
            return a.x;
        case ShuffleComponent.LeftY:
            return a.y;
        case ShuffleComponent.RightX:
            return b.x;
        case ShuffleComponent.RightY:
            return b.y;
        default:
            ThrowInvalidArgument();
    }
}

static void ThrowInvalidArgument()
{
    throw new System.ArgumentException("Invalid shuffle component: " + component);
}

2. Using the "fixed" statement instead of a reinterpret cast

The code used for indexers can be improved a lot!
Example: int2.gen.cs line:616

The fixed statement is horrible because just like throwing exceptions, it prevents a ton of optimizations (it is implemented that way on all runtimes I tested!).

Take a look at the asm it generates:

Code:
example code

(z = test1[6] is there on purpose, not a typo)

Assembly:
asm old

This should instead be done using a reinterpret cast.

Keep in mind that even though the syntax of Unsafe can sometimes (fortunately not here) become really unwieldy, it pretty much always compiles down to very simple instructions. (In fact methods like Unsafe.As<TFrom, TTo>(ref source) are literally a nop because they only trick the C# compiler into accepting the type change).
It's really just the C# name for reinterpret_cast.

By simply casting ref this to a little helper struct all overhead is gone.

Btw Unsafe is defined in System.Runtime.CompilerServices.Unsafe. The As() method I'm using here has an extremely simple definition (in this case it does literally nothing as you can see). All other functions in there are just as trivial, so they should be really easy for Unity to add 👍

Here is what I did instead:


struct int2
{
	public int x;
	public int y;

	public unsafe int this[int index]
	{
		get
		{
			return Unsafe.As<int2, ArrayUnion>(ref this).ints[index];
		}
		set
		{
			Unsafe.As<int2, ArrayUnion>(ref this).ints[index] = value;
		}
	}
}

[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ArrayUnion
{
	// The fixed array sizes are assuming a float4(x,y,z,w) as maximum
	// but their size doesn't even actually matter because we're only using
	// it to reinterpret the bits anyway!
	[FieldOffset(0)]
	public fixed int ints[4];
	[FieldOffset(0)]
	public fixed uint uints[4];
	[FieldOffset(0)]
	public fixed float floats[4];
	[FieldOffset(0)]
	public fixed short shorts[2 * 4]; // 2 shorts per field, 4 times
	[FieldOffset(0)]
	public fixed bool bools[4 * 4]; // 4 bools per field, 4 times
	// ...
}

And with the exact same C# code, this time the disassembly looks like this:

asm improved

This time the jit was able to absolutely decimate the code! Nice! Let me know what you think 😄

主要言語
C#
スター
1.4k
フォーク
159
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Unity-Technologies/Unity.Mathematics のほかの issue

Unity-Technologies/Unity.Mathematics の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。