英文原文:https://www.jacksondunstan.com/articles/5211
Unity 2019.1 于上周发布,Burst 编译器现已退出预览版。它通过生成比 IL2CPP 更优化的代码来保证卓越的性能。让我们尝试一下,看看性能是否如宣传的那样!
要使用 Burst 编译器,我们需要做三件主要的事情。首先,我们必须将 C# 的使用限制在“High-Performance C#”子集中。这意味着我们不使用任何托管对象,例如class和string。其次,我们必须将所有代码放入 C# Job中,例如实现 IJob 或 IJobParallelFor 的代码。第三,我们必须向Job添加 [BurstCompile] 属性。如果我们已经完成了前两个步骤,那么添加 [BurstCompile] 属性就足够微不足道,就实施成本而言可以被视为“免费”。
因此,让我们编写一个简单的作业,并在使用和不使用 [BurstCompile] 属性的情况下测试其性能。这项工作所做的就是对两个向量数组执行点积。我们将使用 NativeArray<T> 类型来保存数组,并使用新发布的 Unity.Mathematics 包中的 float4 类型来保存向量。 Burst 能够识别 float4 类型,并且当我们使用它时可以生成更优化的代码。这是测试:
using System.Diagnostics;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
class TestScript : MonoBehaviour
{
struct RegularJob : IJob
{
public NativeArray<float4> A;
public NativeArray<float4> B;
public NativeArray<float4> C;
public void Execute()
{
for (int i = 0; i < A.Length; ++i)
{
C[i] = math.dot(A[i], B[i]);
}
}
}
[BurstCompile]
struct BurstJob : IJob
{
public NativeArray<float4> A;
public NativeArray<float4> B;
public NativeArray<float4> C;
public void Execute()
{
for (int i = 0; i < A.Length; ++i)
{
C[i] = math.dot(A[i], B[i]);
}
}
}
void Start()
{
const int size = 1000000;
NativeArray<float4> a = new NativeArray<float4>(size, Allocator.TempJob);
NativeArray<float4> b = new NativeArray<float4>(size, Allocator.TempJob);
NativeArray<float4> c = new NativeArray<float4>(size, Allocator.TempJob);
for (int i = 0; i < size; ++i)
{
a[i] = new float4(1, 1, 1, 1);
b[i] = new float4(2, 2, 2, 2);
}
RegularJob regularJob = new RegularJob { A = a, B = b, C = c };
BurstJob burstJob = new BurstJob { A = a, B = b, C = c };
// 第一次运行是预热。第二次运行生成报告。
long regularTime = 0;
long burstTime = 0;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 2; ++i)
{
sw.Restart();
regularJob.Schedule().Complete();
regularTime = sw.ElapsedTicks;
sw.Restart();
burstJob.Schedule().Complete();
burstTime = sw.ElapsedTicks;
}
print(
"Job,Time\n" +
"Regular," + regularTime + "\n" +
"Burst," + burstTime);
a.Dispose();
b.Dispose();
c.Dispose();
}
}
现在让我们运行这段代码来看看每个作业编译器的执行情况。我使用这个环境运行:
- 2.7 Ghz Intel Core i7-6820HQ
- macOS 10.14.4
- Unity 2019.1.0f2
- macOS Standalone
- .NET 4.x scripting runtime version and API compatibility level
- IL2CPP
- Non-development
- 640x480, Fastest, Windowed
这是我得到的结果:
Job | Time |
---|---|
常规的 | 43940 |
burst | 28590 |
通过添加 [BurstCompile] 属性,我们就获得了显着的加速! Burst 编译的代码的运行时间比 IL2CPP 编译的代码少 35%。为了找出原因,让我们使用 Burst Inspector 查看它生成的代码:
- Job > Burst > Open Inspector…
- 点击左侧的TestScript.BurstJob
- 勾选右侧的增强反汇编
- 取消选中右侧的安全检查
- 点击右侧的刷新反汇编
这是 C[i] = math.dot(A[i], B[i]) 的部分:
movups xmm0, xmmword ptr [rcx + rdi]
movups xmm1, xmmword ptr [rdx + rdi]
mulps xmm1, xmm0
movshdup xmm0, xmm1
addps xmm1, xmm0
movhlps xmm0, xmm1
addps xmm0, xmm1
shufps xmm0, xmm0, 0
movups xmmword ptr [rsi + rdi], xmm0
这些是 SIMD 指令,告诉 CPU 同时对许多变量执行相同的操作。在这种情况下,float4 的所有四个分量同时进行加法、乘法等操作。
虽然 Burst 需要改变编程风格,主要是不使用class,但它为合规的代码提供了主要的性能优势。如果代码已经以这种风格编写,Burst 只需将 [BurstCompile] 属性添加到作业类型即可提供“免费”性能!