Considerable performance fixes
まだ誰も着手していません。
評価
- 難易度
- 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:

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

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:

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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Unity-Technologies/Unity.Mathematics のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 55/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
Unity-Technologies/Unity.Mathematics#245 · コメント 3 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 42/100
Unity-Technologies/Unity.Mathematics#243 · リアクション 6 件 ·
Unity-Technologies/Unity.Mathematics の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
メンテナーはふだん 1 日以内に返信
-
:watch: Not Triaged aspnet-core/svc fundamentals/subsvc Source - Docs.ms
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
dotnet/AspNetCore.Docs#37785 ·
メンテナーはふだん 1 日以内に返信
-
needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
Azure/azure-sdk-tools#17204 ·
メンテナーはふだん 1 日以内に返信
-
Проблема с Dotnet RUオープンarea-tutorials needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
dotnet/website-feedback#1779 ·