如何使用Unity的Burst编译器优化c++风格的C#代码? (HPC#实践)

Burst编译器仅支持HPC#子集的C#代码,拒绝foreach遍历非并行集合、托管堆分配、虚方法、接口、委托、LINQ、反射、异常、未标注的NativeArray参数及非blittable类型;需用NativeArray、for循环、栈分配和显式内存控制。

Unity 的 Burst 编译器不支持 C++ 代码,也不能“优化 C++ 风格的 C# 代码”——它只接受符合 HPC# 子集规范的 C# 代码,并在编译时将其转换为高度优化的原生机器码。所谓“C++ 风格”如果指手动内存管理、指针操作或无 GC 行为,那只是表象;真正起作用的是你是否遵守了 Burst 的约束边界。

哪些 C# 语法会被 Burst 拒绝?

Burst 在编译时会做静态验证,任何违反 HPC# 规则的代码都会报错,常见触发点包括:

  • foreach 遍历非 IJobParallelFor 支持的集合(如 ListDictionary
  • 调用任何托管堆分配函数:如 newstring.FormatDebug.LogDebug.Assert
  • 使用虚方法、接口、委托、lambda、LINQ、反射、异常(try/catch)、属性访问器(get/set
  • 未标记 [ReadOnly][WriteOnly]NativeArray 参数,或未加 [NoAlias] 的指针参数
  • 使用非 blittable 类型(如含引用字段的 struct)、未加 [StructLayout(LayoutKind.Sequential)]

如何写出 Burst 可接受的 HPC# 代码?

核心是「数据驱动 + 零抽象开销」。所有逻辑必须落在值类型、栈分配、确定性内存访问上:

  • NativeArray 替代 List,且必须显式传入长度,不能调用 .Length 以外的成员
  • 循环全部写成 for (int i = 0; i ,避免 foreach 和索引器重载
  • 数学计算优先用 math 命名空间(math.sqrtmath.mul),而非 System.Math
  • 自定义 struct 必须加 [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)],且所有字段为 blittable(intfloatbool、其他 struct 等)
  • 函数参数中所有 NativeArray 必须标注读写意图:[ReadOnly] NativeArray input[WriteOnly] NativeArray output
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;

[BurstCompile] public struct SumJob : IJob { [ReadOnly] public NativeArray input; [WriteOnly] public NativeArray output; public float bias;

public void Execute()
{
    float sum = 0f;
    for (int i = 0; i < input.Length; ++i)
        sum += input[i];
    output[0] = sum + bias;
}

}

Burst 编译失败时怎么快速定位问题?

错误信息通常很直接,但容易被忽略的关键线索藏在堆栈末尾或警告级别:

  • 看到 BurstCompilerException: Failed to compile job,先看下一行的 reason: —— 大多是 “call to managed method ‘xxx’ not allowed”
  • 启用详细日志:Player Settings → Other Settings → Scripting Runtime Version → .NET Standard 2.1 + 勾选 Enable Burs

    t Compilation
    ,再在 Console 窗口点击右上角设置图标 → Open Editor Log,搜索 Burst 关键字
  • 临时关闭 Burst 验证:给 job 加 [BurstCompile(DisableSafetyChecks = true)](仅调试用),可绕过部分安全检查,暴露底层 IL 错误
  • BurstInspector(Window → Analysis → Burst Inspector)查看已编译 job 的汇编输出,确认是否真生成了向量化指令(如 vaddps

Burst 的真正门槛不在语法转换,而在于重构整个数据流:你得把“对象行为”彻底拆解成“内存块 + 纯函数”,并且接受没有调试器断点、没有字符串拼接、没有隐式类型转换的事实。一旦跨过这道线,性能提升是实打实的,但每行代码都得经得起静态分析的拷问。