Search Feedback

112
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 (5)

  1. 5664beb4ac89ac0acae001f097a4c29f?d=mm

    DamonJager

    Aug 28, 2017 14: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.

  2. 3e9ed461bfbb2bb331f686df5059d3bb?d=mm

    ehakram

    Aug 21, 2017 10: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/

  3. 1da96c5ef6181f2d6a46f6f60b00df3d?d=mm

    borg1980

    Aug 03, 2017 10:21

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

  4. 3e9ed461bfbb2bb331f686df5059d3bb?d=mm

    ehakram

    Jun 18, 2017 21: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

  5. F151c66a2cc7eeef6f209cda8195da21?d=mm

    ThomasTrenkwalder

    Jun 18, 2017 08: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

(9031)

2D

(221)

Ads

(40)

AI & Navigation

(67)

Analytics

(92)

Animation

(304)

Asset Store

(222)

Assets

(489)

Audio

(157)

Cloud Build

(94)

Collaborate

(31)

Docs & Tutorials

(197)

Editor

(2084)

Everyplay

(13)

Game Performance Reporting

(12)

General

(810)

Graphics

(761)

GUI

(363)

Input

(145)

Licensing

(73)

Networking

(158)

Physics

(341)

Platforms

(402)

Profiling & Optimization

(69)

Runtime

(161)

Scripting

(1014)

Terrain

(152)

WebGL

(131)