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
0
Question by Salmjak · Dec 08, 2015 at 03:36 PM · c#vector3allocationequal

Vector3.Equals without memory allocation?

In my A* pathfinding project I use a hashset for my closed-list (a list of nodes that have been checked already). When running the profiler I noticed something funny, running 20 instances created 3KB GC allocation (per frame) from the Hashset.Contains() function. After googleing I found out that Vector3.Equals() uses a copy of the vector3 and thus create garbage. I tried using a Hashset(float[]) instead, didn't work. Found out that arrays dont create a hashcode that reflects the elements inside of it.

Anyone have an idea how I can create a hashset (I would like to have Contains() with O(1)) for a vector3 without memory allocation? I also read that there is a GetHashCode-function for vector3, but the default comparer is Equals(). How do I change this (the default comparer) for the hashset? Will it solve my problem?

Comment
Add comment · Show 5
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 Bonfire-Boy · Dec 08, 2015 at 04:01 PM 2
Share

I'm curious as to where you found that info about Vector3.Equals. I would expect it to use copies, but surely they're local and hence on the stack and not subject to garbage collection? Or did you mean something else by "garbage" here?

As for your last question, you can set a different comparer by using a alternative constructor when you declare the HashSet. See $$anonymous$$SDN https://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx

avatar image Owen-Reynolds Bonfire-Boy · Dec 08, 2015 at 04:48 PM 0
Share

V3.equals takes an 'object'? Does that mean it has to use boxing?

avatar image Bonfire-Boy Owen-Reynolds · Dec 08, 2015 at 05:02 PM 0
Share

Ah yes, it does. Good point, that'll be the garbage.

And that makes me think that @Salmjak is probably right to think in terms of a bespoke equality comparer.

avatar image Salmjak Bonfire-Boy · Dec 08, 2015 at 05:07 PM 0
Share

I first read about it in the comments here "3. Unity Vector3 equals allocates!"

I also used a deep-profiling and all the GC Allocation from Hashset.Contains() was tracked to Vector3.Equals() --> Single.Equals(). So it might not have to do with the vector3-class itself, but rather the comparer-method. I can't find the thread saying it uses a copy, maybe I just misunderstood something.

avatar image Bonfire-Boy Salmjak · Dec 08, 2015 at 05:23 PM 0
Share

No, you're right. As Owen and wibble say, Vector3.Equals uses boxing to pass the Vector as an object. That's why the heap comes into play.

Thanks for the link.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by wibble82 · Dec 08, 2015 at 04:56 PM

I haven't checked, but I am guessing Vector3.Equals is being passed an object, or casts to an object internally for some reason. If so, then it would allocate memory, as the input parameter has to be 'boxed' to be passed as an object.

The hash set generic can take a generic equality comparer. Inside your comparer you can write any comparison you like - such as directly comparing the 3 floating point components, thus avoiding the problem:

https://msdn.microsoft.com/en-us/library/bb359100(v=vs.110).aspx

That said, if you're doing an A* search algorithm, might be better off working on an integer grid anyway? Then you can encode an x/y(/z?) position into a single ulong, which makes it super quick to look up and, far less memory intensive.

-Chris

Comment
Add comment · Show 1 · 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 Salmjak · Dec 08, 2015 at 08:53 PM 0
Share

Thanks! I implemented your second suggestion!

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

37 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I want to create an interactive right analog MouseLook? 0 Answers

Smooth movement for Gameobjects switching position 2 Answers

How to check if a vector 3 is in a vector 3 array c#. 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