Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] consistently fast freeobject() function

Author
Message
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 3rd Jun 2008 04:49 Edited at: 3rd Jun 2008 04:51
I've seen people talk about FreeObject() functions for finding unused object numbers. The idea often seems to be to search through object numbers from 1 to whatever the first nonexistent object is. The speed of such a function is proportional to the lowest free object number, and i think quite slow for a large number of objects.

This set of functions i made i think are much more efficient. The attached code illustrates making new objects and deleting objects randomly. The downside is that you have to call functions when creating or destroying objects. Can be used alongside other objects not using this system. It uses bitshifting and XOR logic and stuff which i'm not sure DBC has.

I could write a better explanation and comments if anyone is interested. This is probably best explained with aid of a diagram, but maybe you can suss it out already. Hope this is of some use to someone.



SUPER BADASS SPACESHIP X: WEBSITE
FORUM TOPIC
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 3rd Jun 2008 23:01
well I have given it ago but its not happy with this line

inc psn2 , binary_freeobj_spacefull(psn2+psn1)

error 118 about the array not being there.

turns out the dim number need to be a bit higher.

Dark Physics makes any hot drink go cold.
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 4th Jun 2008 01:32
Hmm. That's odd. I just cut and pasted the code into a new project and it works without error. What verion of DBP are you running? I'm using 1.066.

How much higher did the array size need to be? Thanks for giving it a go anyway.

SUPER BADASS SPACESHIP X: WEBSITE
FORUM TOPIC
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Jun 2008 10:20
Why do you need all of this? To make an efficient FreeObject() function all you need to do is to record the highest object ID and an array that stores all deleted objects, if the array is of size 0 then you increment the highest object number and return that, if you delete an object you add it to the array, if the array is more than 0 in size when calling the FreeObject() function then remove an index from the array and return that, surely that's sufficient and the fastest method, or at least the basic idea behind it, you could speed it up by not resizing the array all the time by allocating it in chunks and never downsizing it etc.

And I ran your code, in the default editor I had to remove all occurrences of the default concatenation symbol '...' in the comments for it to compile and got the same error HowDo got, with the latest version of DBPro, and when making the array larger I wasn't really sure what I was supposed to be seeing.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 5th Jun 2008 03:21 Edited at: 5th Jun 2008 03:34
Your idea would work, and would be simpler to implement than mine. Also, it would probably be initially faster.

On the downside, once there are a large number of deleted objects (and the lowest thus far unused object number is high), the array of deleted and currently unused object numbers could be very big. The array i am using has 2 bits*the max number of objects, yours could use something like 16 bits (for a word)*this number. Not that much bigger i guess, especially considering the amount of memory available to a current PC. I did a quick calculation and it's at most a few hundred Kb.I guess the compicatedness of my code is fairly pointless, but i'm still pretty chuffed with it. Perhaps i can use this kind of concept for something more useful someday.

I'm very intruiged as to why it shouldn't work on other people's computers. Perhaps i'll make a version that writes a debugging file or something. Seems that some fundamental thing has changed in DB. This is why I'm scared of updating!

SUPER BADASS SPACESHIP X: WEBSITE
FORUM TOPIC
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Jun 2008 23:03
It worked fine for me on 6.7

I used the following loop to test for allocating everything:


No crash, and it allocated every number available within the limit I'd set before exiting.

Login to post a reply

Server time is: 2024-11-25 02:54:55
Your offset time is: 2024-11-25 02:54:55