Search Feedback

202
votes

Vector3 and other structs optimization of operators

Profiling & Optimization

-

-

A lot of Unity's Structs' operators can have a massive performance increase, in a very simple way.
In my test script (available here https://dropb.in/ponda.nimrod), I reduce the Vector3 multiplication cost from 18.9 ms to 2.5 ms

If we take the * operator as an example, the current implementation does this:
{
return new Vector3(a.x * d, a.y * d, a.z * d);
}
The implementation I suggest would be like this:
{
Vector3 result;
result.x = a.x * d;
result.y = a.y * d;
result.z = a.z * d;
return result;
}
This avoids calling unnecessarily the Vector3 constructor.

What I just explained for the Vector3 struct it is also true for other Unity's structs (like Vector2)

Comments (6)

  1. 3e9ed461bfbb2bb331f686df5059d3bb?d=mm

    Aka_ToolBuddy

    Aug 30, 2018 13:49

    I implemented those optimizations into this free asset: https://www.assetstore.unity3d.com/#!/content/120660?aid=1101l3N9P
    Please give it a try.

  2. 5664beb4ac89ac0acae001f097a4c29f?d=mm

    DamonJager

    Aug 28, 2017 12:21

    So the compiler asigns two times the same value if you use new. It sounds really dumb. It may have something to do with inlining... and I heard of really strange things happening with the mono and the old .NET. It's not about Unity, it's about the runtime, this retarded case should be covered somehow. Though I would suspect that this doesn't happen with microsoft's runtime.

  3. 3e9ed461bfbb2bb331f686df5059d3bb?d=mm

    Aka_ToolBuddy

    Aug 21, 2017 08:42

    For those interested, here is a forum thread about this suggestion, with a comparison of the IL instructions of an original operator and its optimized equivalent

    https://forum.unity3d.com/threads/vector3-and-other-structs-optimization-of-operators.477338/

  4. 1da96c5ef6181f2d6a46f6f60b00df3d?d=mm

    borg1980

    Aug 03, 2017 08:21

    Speedup confirmed. C# compilers are so darn stupid :/

  5. 3e9ed461bfbb2bb331f686df5059d3bb?d=mm

    Aka_ToolBuddy

    Jun 18, 2017 19:12

    I confirm that the optimization works for standalone applications in release mode ("Development Build" disabled)
    I used a heavier version of the script above, and used Fraps to get the FPS count (to rule out any potential Unity's profiler issue), and here are the results:
    - Optimized version: 53 FPS
    - Unoptimized version: 42 FPS

  6. F151c66a2cc7eeef6f209cda8195da21?d=mm

    ThomasTrenkwalder

    Jun 18, 2017 06:52

    Unitys operator is 30% slower for me, both in editor and in build on 5.6.1f1

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea

Categories

All

(11030)

2D

(290)

Ads

(63)

AI & Navigation

(83)

Analytics

(130)

Animation

(413)

Asset Store

(370)

Asset Store Publisher

(21)

Assets

(557)

Audio

(185)

Cloud Build

(154)

Collaborate

(70)

Docs & Tutorials

(251)

Editor

(2579)

Everyplay

(17)

Game Performance Reporting

(22)

General

(1003)

Graphics

(903)

GUI

(447)

Input

(173)

Licensing

(93)

Networking

(192)

Physics

(392)

Platforms

(448)

Profiling & Optimization

(84)

Runtime

(188)

Scripting

(1154)

Terrain

(177)

WebGL

(145)