• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By ThunderTwonk
      Hello everyone, I am working on a game idea and since I am still in the process of learning C# and the features available in unity I was hoping some of you might be able to offer me a little insight on things in general for getting started.
      I guess the basic components of what I'm wanting to create would be a Multi-levels management/city builder/rpg.
      The goal is to provide a framework for players to interact with, build in and affect the world both from a 3rd person action RPG as well as a zoomed out 4x style view (This would be something unlocked through gameplay)
       
      As for my questions go I was wondering if anyone had resources that could help me learn.  I've been on youtube as well as enrolled in an online course for basic unity and C# and will continue those but if anyone has any words of advice, a place that has good information and tutorials etc.
       
      Thanks for your time.
    • By Cahit Karahan

       
      Hi, I'm new in this forum. It is honorable to see such communities exist. I would like to share my new game. I did for android with unity. I know the game is a little awkward , but you have to know that this game is from the time when Unity's name is Unity3D  I have made my first game when I was 12. Now I am 22.  I have taken a lot of experience in this process and I can make better games nowadays. I have published this game nowadays but actually this game is very old but also it is very special for me :))
      I have just wanted to retouch and share this game, because it has a very important place for me.
       
      DESCRIPTION FROM GOOGLE PLAY STORE

      It's a special free 3D horror adventure action game for the halloween. Fun with scary sound effects and musics, 3D realistic graphics, you will feel the horror in the deep of your heart. Use your reflex. Totally free adventure. Totally scary horror game. 

      Tamarra, she is a beast from our world. She needs to consume souls from innocent people to stay alive. Story begins, the old Elaris tribe had lost their everything because of this beast who lived in the well. Araknas was the most powerful warrior of the tribe. One day, Araknas's mother was killed by the servant beasts of Tamarra. That's how Araknas's journey to the well begins. Tamara's well is guarded by horrible beasts. Araknas has to pass all servant beasts until he reaches Tamarra.

      Even death at the end is worth the revenge. 
      Are you brave enough to jump into Tamarra's well?

      Survive from witch attacks, clown attacks and many scary creature.

      - Realistic 3D graphics.
      - Scary sounds.
      - Scary musics.
      - Best experience with headphones.
      - A demon cage where you can imprison all the demons one by one
      - The witches do not like help, but they love blood stone. Witch store where you can develop your abilities and get new abilities.
      - Countless beasts.
      - At the end of the well there is a hidden surprise for you.

      *We do not recommend this game to people with clown phobia, spider phobia, or panic attacks.*

      **!!!**Note : This game is an early-access game, we are upgrading new features every day, new beasts, new improvements, as an example online 1vs1 fall on the list, so stay on connect and follow Halloween : Horror Well on Google Play.**!!!**

    • By INFRA
      SCAN. DRILL. SURVIVE.   ISOLATED Release in May 1st 2018   https://store.steampowered.com/app/805950/Isolated/   A game by Jérémie Bertrand Music & Sound Design by Pierrick Querolle *** Our solar system has been invaded by strangers. For the purpose of a possible negotiation, a team of astronauts is sent to the moon. Alas, they are shot before even arriving on the scene. Only one astronaut survives the crash and his only goal will be to go home...   GAMEPLAY   Shoot enemy ships to avoid being invaded. Be precise in your movements, because it's better to lose a bit of life at the top than to lose it all at the bottom. Take out your drill to destroy the stones in your path. Validate your identity to cross the different laboratories. Reach the flag before losing your three lives.   And all that... at the same time! Will you be able to go home? If the answer is yes, how long will it take?
    • By BigJiggly
      Hello! So, I've been the leader of BJP for a while now. I'm a bit bored of taking the role I always take, leader. I was hoping someone out there is looking to forge a team maybe and needs a programmer. 
      I have experience mainly in the Unity engine(C# intermediate) and I have a very small amount of knowledge on Shaders, as well as experience on developing games(usually end up stuck in dev hell) and leading experience from my last team which at one point reached 11 people. I personally love the Unity engine and prefer to use it as it's the development environment I'm comfortable with. 
      I have used Unity for over a year and a few months, I'd consider myself an intermediate at the Engine, but to this day Unity still surprises me. 
      I live in the United Kingdom, I find it a bit strange to work with other programmers as the ones I've worked with tend to leave their code heavily unoptimised and I'm a on the go optimise kind of guy, I also like to get things done quickly.
       
      If you're a new team and need a programmer that has high levels of ambition and strives to maintain the motivation throughout the team, then I'm your guy. I don't care if you're just beginning because I'm all for helping people learn!
       
      To finish this off: I like to get things done and I like to get them done right the first time, if I fail I will do it again and again, etc, until I loose all motivation. So if you're a modeller or an artist, please don't leave me to do all the modelling/art as well as the programming and sound. I do have experience in all those areas but my main power is in programming and I'd prefer to keep it that way.
       
      [If this was posted in the wrong forum, sorry, I don't really know the layout of this website yet]
    • By Alexia
      Hi all,
      A small team are currently undertaking a fairly large game project involving multi-playing, taming and other complex game mechanisms.
      We are looking for someone who is experienced in programming a wide range of mechanisms, more information is on our discord server. It is also a learning experience and we wouldn't expect you to know how to do everything we wanted, but just ask that you would be willing to learn how to.
      If you are interested in joining the (rather long term project) just message me and i'll add you on discord.
      Thanks for your time,
      Alexia 
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity 'External Code' taking up most of my CPU?

This topic is 740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been implementing CPU based voxel ray casting on a BSP tree, in the end a node consists of about 30 voxels, however, the performance is terrible, I ran the program through the Visual Studio Community Edition CPU profiler and got the following output:

http://i.imgur.com/SbZIJCn.png

Since it just says that '[External Code]' is taking most of the CPU time, I'm not sure how to proceed. I can't identify many serious performance issues, and I find it hard to believe that the recursion could be the problem. My Ray-AABB intersection tests don't appear to be too slow either.

My code:
BSP Tree code (yes, I know the filename says Octree):
VoxelOctree.cs

GI_IntersectionTests.cs

GIWorld.cs

Any suggestions on how I should approach figuring out this issue would be appreciated.
The ray casting and tree traversal code is in the RayCast function in VoxelOctree.cs Edited by HimanshuGoel97

Share this post


Link to post
Share on other sites
Advertisement

Do you make a system call or call to a library function in there?

 

Edit:

Nvm, guess I can look.

 

EditEdit:

Or not. Where is VoxelTree? Oh, you linked it wrong, but I can just navigate to it. : p

 

Gonna guess that it's this:

BoundingBox b2 = new BoundingBox(Voxels.Values.ElementAt(i).Position - Vector3.One * vSide * 0.5f, Voxels.Values.ElementAt(i).Position + Vector3.One * vSide * 0.5f);

Can you break that out into a separate function and call it there, then check the profile again?

Edited by Khatharr

Share this post


Link to post
Share on other sites
Sorry about that, just noticed and fixed the link.
EDIT: Ah, a minute too late.

EDIT: The boundingbox class doesn't actually do any calculations on the data, but I'll give your suggestion a try in the morning.

Share this post


Link to post
Share on other sites

I'm wondering if it's the allocation is why I singled that out. I don't see any other external calls in that function, but I'm kind of sleepy as well.

Share this post


Link to post
Share on other sites

BoundingBox is your code, so I would expect it to show up in your call stack if it were the problem. And it's a struct, so it shouldn't be causing any heap allocations.

 

I would focus maybe on the dictionary values enumeration. Isn't there a setting that will show you symbol information for system calls? I know there is for viewing the callstack in the debugger, I don't know if there is for the performance stuff...

 

You're calling ElementAt four times, it seems you should be able to just call it once and store the result.

 

I don't see anything obvious in that function that would be causing any heap allocations (and possible performance issues), but .net can be sneaky that way. 

Share this post


Link to post
Share on other sites

I'm tempted to guess the allocation as well.  Calling "new" in C# is faster than C++'s global heap, but still not exactly speedy.

 

It looks like the allocator is being called a lot.  Every time you have Voxels !=null, you loop through Voxels.Values.Count calls to the allocator to make a new one.  How many voxel values do you have?  How many times do you call RayCast?  Multiply the two, that's about how many times you do the allocation.

 

My hunch is that you are hammering the allocator and constructor with many millions of calls for what looks like about 10 seconds. 

Share this post


Link to post
Share on other sites

Ok, it looks like (from my own tests) calling ElementAt on the Dictionary's Values collection is really slow. I'm guessing it is an order-n operation that needs to iterate through items one by one, since the larger the index the longer it takes.

 

You probably want to use foreach instead (and hopefully the ValueCollection's iterator is a value type, to avoid unnecessary heap allocation)

 

In the little test I did, for a Dictionary of 10,000 items, this:

 

            for (int i = 0; i < blobs.Values.Count; i++)
            {
                value += blobs.Values.ElementAt(i).a;
            }
 
was more than a 1000x slower than this:
 
            foreach (Blob blob in blobs.Values)
            {
                value += blob.a;
            }
 
Edited by phil_t

Share this post


Link to post
Share on other sites

BoundingBox is your code, so I would expect it to show up in your call stack if it were the problem. And it's a struct, so it shouldn't be causing any heap allocations.

 
There is a common misconception that structs must live on the stack rather than the heap.
 
C# makes a distinction between "reference types" and "value types", but that does not mean doesn't affect memory allocations or heap allocations.  C# works very hard to avoid specifying exactly where things live. The data stack and the heap are both implementation details for the language.  The specification is vague about the heap, allowing for there to be many heaps, and for heaps to be garbage collecting or not, 

 

The C# specification notes that "structs are value types and do not require heap allocation", but that does not mean they require stack allocation.

  
Structs are created on the heap all the time in C#. Structs can be placed on the stack, but they also can be placed on the heap. Structs that are boxed, structs that are a field of a class, structs not closed in a lambda, structs in an iterator or foreach block, these are all probably on the heap. A raw variable may be on the stack or the heap. 
 
The runtime has even more options. A struct's members can be pulled out into registers during JIT or ngen compilation.  Unlike C++, these details don't matter as much in C#.  The distinction between "reference types" and "value types" is generally sufficient.

Share this post


Link to post
Share on other sites

Agreed that struct doesn't necessarily mean it's allocated on the stack, but in the OP's usage scenario it will. 

 

You basically have:

    public struct BoundingBox : IEquatable<BoundingBox>
    {
        public Vector3 Min;
        public Vector3 Max;

        public BoundingBox(Vector3 min, Vector3 max)
        {
            this.Min = min;
            this.Max = max;
        }
    }

and then in the function:

    BoundingBox b2 = new BoundingBox(Voxels.Values.ElementAt(i).Position - Vector3.One * vSide * 0.5f, Voxels.Values.ElementAt(i).Position + Vector3.One * vSide * 0.5f);

In this case, the new is not allocating on the heap. I looked to see somewhere that b2 might be boxed (say, by an implicit cast to IEquatable<BoundingBox> ), and couldn't find any.

Edited by phil_t

Share this post


Link to post
Share on other sites

 

Ok, it looks like (from my own tests) calling ElementAt on the Dictionary's Values collection is really slow. I'm guessing it is an order-n operation that needs to iterate through items one by one, since the larger the index the longer it takes.

 

You probably want to use foreach instead (and hopefully the ValueCollection's iterator is a value type, to avoid unnecessary heap allocation)

 

In the little test I did, for a Dictionary of 10,000 items, this:

 

            for (int i = 0; i < blobs.Values.Count; i++)
            {
                value += blobs.Values.ElementAt(i).a;
            }
 
was more than a 1000x slower than this:
 
            foreach (Blob blob in blobs.Values)
            {
                value += blob.a;
            }
 

 

 

Just a FYI:

 

Dictionary, List, possibly a few others (except for Collection<T>) all have struct enumerators and explicitly implement the GetEnumerator() so the GetEnumerator that actually will be called in that foreach will return the struct enumerator and not do any boxing. 

 

With that said though, ElementAt is an extension method for IEnumerable<T> so the value collection is going to be treated as an IEnumerable<T> and the explicitly implemented GetEnumerator will be called, which means the struct enumerator will be boxed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement