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
1
Question by JLJac · Jun 06, 2018 at 04:59 PM · performanceprofilerperformance optimizationthreadsthreading

Lowered general performance with Threads

I am trying to get threading working in Unity. I have a heavy task which has all the data it needs ready early in the frame and doesn't need to be done until the end of the frame, so it would make sense to have it just run in parallel while the rest of the game logic is resolving.

I have tried the new Jobs system, which works, but putting and withdrawing all the data from the nativeArray ends up taking 3x as long as the actual operation, so it's not a solution. Instead I moved on to raw C# threading, and came across some unusual behavior.

When putting the heavy task on another thread, main thread slows down to about half speed.

https://imgwiz.com/images/2018/06/06/threadingProblem.png

On the left you see the Heavy Task, which as expected drops down to 0ms when threading is turned on. On the right you see another random task. Pretty much all scripts have a 100-150% increase when the threading is toggled on, which drops back to normal immediately when it's toggled off again. The increase doesn't seem to be concentrated anywhere in particular but trickles down the entire hierarchy.

It happens whether I use a continuous while(true) thread or a thread pool where I start a thread each frame. I have tried throwing out safety and running the thing without any locks at all (in case the locks were halting the main thread), to the same result.

Another thing to note, with threading the little notches on the Unity worker threads appear... sparser? Could be a clue, or the causality is the other way around and it's just an artifact of the main thread being slower and making requests less often.

alt text

Please please please, if you have any idea give me a nudge in the right direction.

Thank you.

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
0
Best Answer

Answer by JLJac · Jun 07, 2018 at 05:39 PM

After much research I might have figured it out. It seems that any repeated method calls in the new thread (I had a bunch of Mathf.Lerp - beware, even simple things like Clamp or Max etc seem to cause it!) will cause the weird slowdown on the main thread. I suppose this is because the method calls somehow need to check back between threads or are referring to some common object or similar. Maybe there is a lock on Mathf?

If the code inside the thread doesn't employ any method calls, it seems to be able to spin away on its own without getting in the way of the main thread.

On a slightly ironic note the exclusion of method calls made my code so much faster that I don't even need to thread it any more. But, knowledge gained ~

Comment
Add comment · Show 2 · 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 JLJac · Jun 08, 2018 at 08:45 AM 1
Share

I can't believe how stupid I was - it's the Deep Profiler. It's completely natural - each time a method is called anywhere, the Deep Profiler must be notified, and some kind of lock is utilized. $$anonymous$$aking it so that if 2 threads are running in parallel, they have to wait for each other every time they reach a method call, making it take approximately as long as it would have to just run them one after each other. With the Deep Profiler disabled, I do get the expected speedups from parallelism.

avatar image Harinezumi JLJac · Jun 08, 2018 at 09:05 AM 0
Share

Nice find (points awarded ;) )! This is useful for future when profiling multi-threaded code. And it's good to remember that profiling is not without its performance overhead!

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

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

Help with Optimizing Voxel Code? 1 Answer

Reading Profiler Results 1 Answer

Question regarding TileMap Performance 0 Answers

Problem with profiler spikes caused by Semaphore.WaitForSignal 0 Answers

performance issue on android devices unity 2018.1.1f1 0 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