Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Considerable performance fixes

未关闭
#66 5 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp
领域
performance

调研方向

先从 src/Unity.Mathematics/int2.gen.cs 中第 616 行和第 775 行附近引用的位置开始,然后确定这两种提议的模式在生成的向量类型中适用的范围有多广。将生成的 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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Unity-Technologies/Unity.Mathematics 的其他 Issue

查看 Unity-Technologies/Unity.Mathematics 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。