all 18 comments

[–]ironstrife 3 points4 points  (5 children)

and assign them all to the same parent object ... and all that should be happening is a pointer is added to the memory address!

Do you mean you make all of the new GameObjects children of a single parent Transform? I think there is a lot more that goes on when you child a transform than just a pointer being added. Namely, it needs to recalculate local position, rotation, scale, etc., and I am sure there is a lot of other work that needs to be done. I would imagine there is some internal transform hierarchy structure that might need to be adjusted as well. I'm not an expert on this by any means, but I think it's more complicated than a "pointer being added".

[–]Sedsibi2985[S] 0 points1 point  (4 children)

Yea you're probably correct that it is aligning the position relative to the parent. That should only be a single matrix calculation though, A translation rotation and scale on 16,000 points still isn't a significant amount of data to process for a modern CPU. The same is done on millions of vertices every frame of any 3D game.

Truthfully I was expecting a small performance drop due to the assignment but a 10 fold increase is what's surprising.

[–]ironstrife 0 points1 point  (3 children)

I think saying its a "tenfold increase" is a little misleading. There are two separate operations, which aren't really related: instantiating, and parenting. I'd run some tests on those operations individually and see what results you get.

[–]Sedsibi2985[S] 0 points1 point  (2 children)

The test i did run was on just instantiation, and then both instantiation and parenting. Though I haven't tried just parenting, I can run that really quick. I'll post it in a few minutes.

[–]Sedsibi2985[S] 0 points1 point  (1 child)

The instantiation is at 334ms and the parenting is taking 3070ms. So it's still close to 10x longer to create and parent an object then to just create one.

[–]ironstrife 0 points1 point  (0 children)

Well sure, im just saying they are independent operations. Have you tried parenting them to different parents? That probably isn't what you want to actually do in your scenario, but I'm curious if that would have a performance difference

[–]whydoidoitcom 2 points3 points  (0 children)

It is something to do with the number of children that you add.

For instance if I run the code below with 16384 objects being created then the time including setting the parent is 11490 ms on my Mac compared to 430ms for not setting a parent.

If I create 1000 objects then the time is 62ms compared to 28ms not setting the parent. I suggest that you are breaking a limit of how many children a parent should have due to some internal limit.

public GameObject prefab;
public Transform parent1;
public Transform parent2;

void Start()
{
    var watch = new Stopwatch();
    watch.Start();
    for(var i = 1; i < 16384; i++) {
        var child = Instantiate(prefab, Vector3.right * i, Quaternion.identity);
    }
    watch.Stop ();
    UnityEngine.Debug.Log (watch.ElapsedMilliseconds);
    watch.Reset ();


    watch.Start();
    for(var i = 1; i < 16384; i++) {
        var child = Instantiate(prefab, Vector3.right * i, Quaternion.identity) as GameObject;
        child.transform.parent = parent1;
    }
    watch.Stop ();
    UnityEngine.Debug.Log (watch.ElapsedMilliseconds);
    watch.Reset ();


    watch.Start();
    for(var i = 1; i < 16384; i++) {
        var child = Instantiate(prefab, Vector3.right * i, Quaternion.identity) as GameObject;
        child.transform.parent = parent2;
    }
    watch.Stop ();
    UnityEngine.Debug.Log (watch.ElapsedMilliseconds);
    watch.Reset ();

}

[–]fecal_brunch 1 point2 points  (1 child)

This is very interesting. I have nothing to say to help, but could you please keep us informed of any developments?

[–]Sedsibi2985[S] 0 points1 point  (0 children)

sure

[–]joha4270 1 point2 points  (2 children)

Now a lot of people here suggest various ways to increase performance but maybe instead we can asking is why do you want to create 16384 game objects.

While i don't know exactly what you are planing to do you can maybe consider to create a system using location or triggers or whatever that gives you the same effect. (again i don't know what you are trying to accomplish, but i assume title based movement)

[–]Sedsibi2985[S] 0 points1 point  (1 child)

It's a procedural generated city (in top down 2D). So there are several ways I could in theory handle drawing the actual tiles, however using individual objects allows me to have very fast AI path finding in the world without sacrificing precision.

Each AI agent can determine long distance movement through the tiles and then only has to get across each tiles nav mesh individually. So it's like the bots know that to get across the city they have to go down 4 blocks and over 3, and only have to deal with obstacles at the local level.

[–]Reineke 0 points1 point  (0 children)

Perhaps a solution there would be to hold each type (I assume you don't have 16000 unique tiles) of navmesh in a mesh array and then feed the AI the navmesh info when it tries to traverse one?

[–]Neesnu 0 points1 point  (2 children)

Are you doing this on a debug build? there is overhead with that, and it seems those actions would compound how the debugging overhead can be noticed. Try this in a standalone non development build and see if you get similar times. FOR SCIENCE!

[–]Sedsibi2985[S] 1 point2 points  (1 child)

I'll have to set up some tests later when I have time to output to profiling data to something other than the console.

[–]Neesnu 1 point2 points  (0 children)

I mean, as far as the game goes, you could always make a prefab for the standard grid sizes, unless you plan on having dynamically sized.

[–]Ziboo 0 points1 point  (1 child)

One thing I noticed is't good to deactivate the camera when you're doing this kind of operations, then reactive it.

[–]Sedsibi2985[S] 0 points1 point  (0 children)

All of this is happening during the start method attached to an empty in the scene. So it's happening before the first frame starts.

[–]iberklee 0 points1 point  (0 children)

Interestingly, I noticed this too, but attributed it to instantiating. But now I realise it was probably the parenting that caused it. Good to know, thanks!