Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Kozhion · Sep 26, 2015 at 05:41 PM · performance optimizationstruct

Unity structs should implement the IEquatable interface

Implementing the IEquatable interface on the unity structs (Vector3, etc) should provide a performance increase when using these structures in the generic contains no?

http://www.somasim.com/blog/2015/08/c-performance-tips-for-unity-part-2-structs-and-enums/

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ddf · Mar 14, 2016 at 10:51 AM

I've been looking at this today and while in general I agree after having read that same blog post, it would seem that the reason they don't do this is because it requires JIT compiling to work. If you call Contains on a List of structs that implement IEquatable, it will try to JIT compile a System.Collections.Generic.GenericEqualityComparer for that type. Looks like this:

 00:29:05    ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.GenericEqualityComparer`1<Point3>:.ctor
 00:29:05    ()' while running with --aot-only.
 00:29:05    
 00:29:05      at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod*,object,object[],S
 00:29:05    ystem.Exception&)
 00:29:05      at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.
 00:29:05    Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
 00:29:05    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
 00:29:05      at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.
 00:29:05    Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
 00:29:05      at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters
 00:29:05    , System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
 00:29:05      at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
 00:29:05      at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
 00:29:05      at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
 00:29:05      at System.Collections.Generic.EqualityComparer`1[Point3]..cctor () [0x00000] in <filename unknown>:0
 00:29:05    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for System.Collections.Generic.Equality
 00:29:05    Comparer`1
 00:29:05      at System.Array.IndexOf[Point3] (.Point3[] array, Point3 value, Int32 startIndex, Int32 count) [0x00000] in <filename unknown
 00:29:05    >:0
 00:29:05      at System.Collections.Generic.List`1[Point3].Contains (Point3 item) [0x00000] in <filename unknown>:0

This happens even if you write a class that sub-classes EqualityComparer. In fact, writing that class and trying to instantiate it will cause the same JIT compile (vs implementing the IEqualityComparer interface). If there is a way to force the compiler to generate the EqualityComparer ahead of time, I would love to know about it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Destroy vs Disable GameObject for Clothes Change game. 1 Answer

Because this call is not awaited, execution of the current method continues 0 Answers

Sharing one animation on multiple clones 1 Answer

Prefab lightmapping/Prefab lighting 0 Answers

Changing material of an object VS Changing the color property of the material which is more efficient ? 1 Answer


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