set_used(0); vs clear(); - Which is better to use? (array)

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

set_used(0); vs clear(); - Which is better to use? (array)

Post by MasterGod »

I understand both methods clears the array so which should I use?

Thanks.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: set_used(0); vs clear(); - Which is better to use? (arra

Post by rogerborg »

MasterGod wrote:I understand both methods clears the array so which should I use?
You may want to revisit your understanding.

Code: Select all

	void set_used(u32 usedNow)
	{
		if (allocated < usedNow)
			reallocate(usedNow);

		used = usedNow;
	}
set_used(0) does not destroy any objects or free any memory.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Re: set_used(0); vs clear(); - Which is better to use? (arra

Post by MasterGod »

rogerborg wrote:
MasterGod wrote:I understand both methods clears the array so which should I use?
You may want to revisit your understanding.

Code: Select all

	void set_used(u32 usedNow)
	{
		if (allocated < usedNow)
			reallocate(usedNow);

		used = usedNow;
	}
set_used(0) does not destroy any objects or free any memory.
So what can it be used for? (I've seen it in the smgr->drawAll() method..) - What's the purpose of it..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Re: set_used(0); vs clear(); - Which is better to use? (arra

Post by Halifax »

MasterGod wrote:
rogerborg wrote:
MasterGod wrote:I understand both methods clears the array so which should I use?
You may want to revisit your understanding.

Code: Select all

	void set_used(u32 usedNow)
	{
		if (allocated < usedNow)
			reallocate(usedNow);

		used = usedNow;
	}
set_used(0) does not destroy any objects or free any memory.
So what can it be used for? (I've seen it in the smgr->drawAll() method..) - What's the purpose of it..
If I recall, that was answered in your last topic where you specifically asked about what set_used(0) was used for.

And if you feel too lazy to search for it, I did it myself:
vitek wrote: OnRegisterSceneNode() is called for every scene node in the scene graph. That function will be called regardless of what the contents of the other render queues look like.

That is just the way it is written. Scene nodes register themselves every render in OnRegisterSceneNode(). If they didn't do it that way, then they would need to register themselves for rendering when they are added to the scene graph, and they would have to be removed from the render queues when they were removed from the scene graph.

If you don't clear the array before/after each render, but you continue to call OnRegisterSceneNode(), the render queue will have many pointers to the same nodes.

Travis
TheQuestion = 2B || !2B
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

reallocating memory is an expensive job, so it is better to mark the render queues as unused instead of clearing them each frame. iirc it used to do this in Irrlicht 1.0 but we changed it
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

bitplane wrote:reallocating memory is an expensive job, so it is better to mark the render queues as unused instead of clearing them each frame. iirc it used to do this in Irrlicht 1.0 but we changed it
So when do they actually get cleared then?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

It's cleared in CSceneManager's destructor.

Try this code yourself, try different values for size, put some real timers around them if it runs too fast for you to be able to see anything.

Code: Select all

core::array<s32> Numbers1;
core::array<s32> Numbers2;
s32 i, j, size = 5000;

cout << "Test 1 start\n";

// test 1- 
for (j=0; j < size; ++j)
{
    Numbers1.clear();
    for (i=0; i < size; ++i)
        Numbers1.push_back(i);
}

cout << "Test 2 start\n";

// test 2- 
for (j=0; j < size; ++j)
{
    Numbers2.set_used(0);
    for (i=0; i < size; ++i)
        Numbers2.push_back(i);
}

cout << "Test 2 end";
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

bitplane wrote:reallocating memory is an expensive job...
With that example your words has much greater effect :shock:
It's much more faster with the set_used, wow.. - Thanks for showing me that.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply