Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vladibo · Jul 01, 2014 at 11:26 PM · structref

Structs passed by ref = better performance?

In .NET passing to a function large struct by ref has a performance gain because they don't have to be copied but instead a reference to the location on the stack is passed. Is it the same for Unity on mobile? I don't see many examples like (ref Vector3 struct) in Unity API

Thanks in advance

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vladibo · Jul 01, 2014 at 11:43 PM -1
Share

When I say large struct I mean for example Vector3 = 4*3 bytes = 12 bytes If passed by ref should take only 4 bytes for the ref

I cannot make Vector3 already to be class. What would be better performance-wise:

 1.) Some$$anonymous$$ehtod(Vector3 v)
 or
 2.) Some$$anonymous$$ehtod(ref Vector3 v)

In .NET for example XNA, clearly 2.) is faster event though 1.) is prerettier

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by rutter · Jul 01, 2014 at 11:29 PM

Pass-by-ref does tend to run faster, but isn't always worth the trouble.

Why not use a class, instead? A "large" struct sounds like a possible design problem.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Jul 01, 2014 at 11:34 PM 0
Share

As I understand it the primary difference between a class and a struct is a struct is always passed by value, while a class is always passed by reference.

$$anonymous$$akes sense to me to use a class if you want default pass by reference.

avatar image Huacanacha · Jul 01, 2014 at 11:46 PM 0
Share

$$anonymous$$atrix4x4 is what I would consider a large struct (16 floats). I can't think of any other common structs in Unity that would be worth passing by reference for performance reasons. Vectors, Color and Quaternions top out at 4 floats which certainly isn't worth the trouble (including losing immutability) for essentially no gain.

avatar image FIFTYTWO · Sep 13, 2020 at 01:11 PM 0
Share

If the code is performance critical it makes sense to use struct to optimise memory allocation because they don't need separate heap allocation for every instance and may be allocated in call stack.

avatar image
1

Answer by Huacanacha · Jul 01, 2014 at 11:42 PM

A Vector3 is just three floats so it's pretty much pointless, for performance reasons, to pass by ref which requires creating the reference wrapper and dereferencing when used.

You also lose the immutability of the struct which could lead to unexpected consequences. For example grabbing the position of an object then modifying will move the object without explicity setting the transform.position. In many (maybe most) cases this won't be the intent.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Where to use Structs and classes? 5 Answers

Putting user defined struct in Unity Editor 4 Answers

How to declare a struct in C# 1 Answer

Structs in C# Question 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges