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.

DarkBASIC Professional Discussion / Different approach to "Unused IDs"

Author
Message
RedFlames
18
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 24th Jul 2010 01:58 Edited at: 27th Jul 2010 22:41
Ok so I was wondering how to keep track of used IDs (objects, files, ...) and the 2 main approaches seem to be:
1. Use a Counter that increases for every object in use
2. Find Random unused ID

So my first thought was to keep track of those IDs represented by the bits in a variable:
For example you have a BYTE, then you request three unused IDs, and it will store this in the BYTE as %00000111. And if you then free the second ID it would be %00000101 and so on.
As i was writing my functions to manage this I noticed: Variables are passed by Value and not by Address, so you can't actually alter the BYTE from within the function unless it is global and you know the distinc variable-name.
So I had to Allocate the Byte(s) in memory, pass the address, and Peek & Poke the BYTE to alter it.

Here is my example code: [ !!! NEEDS IANs MATRIX1UTILs !!! ]

Right now it Allocates 8 bytes and thus Peeks/Pokes a DWORD.
The Time to set/clear all Bits 10000 times return ~130ms here.
Seems acceptable.

So, now i want to hear your opinion; is this still too complex (post easier system?).
Or maybe one could use a Memblock / Memory Bank?

~RedFlames
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 24th Jul 2010 02:54
I like to compartmentalize. I have variables for the starting number of each type of object/texture/whatever.

And each time I want to reference a specific one, I do something like this:

Just as an example.... I would normally try to keep all the scenery meshes together and item meshes seperately. It doesn't seem very efficient, but I'm new to DBP and this makes sense to me.

The fastest code is the code never written.
thenerd
17
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 24th Jul 2010 03:16 Edited at: 24th Jul 2010 03:19
this is the module we are using in OpenFPS for resource management:

you use the grabResource(type) function to get a number for the specified type (all the types are constants), and the module automatically manages counters for the resources. This code won't actually compile without the other source files in the project, but you can reference it to see a system that has worked well.


IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Jul 2010 04:32
Well, TBH, I'd use the resource tracking built into my plug-ins. :p

As well as all of the standard FIND FREE XXXX functions (eg FIND FREE OBJECT etc), I recently added user-defined resources too:


Now for the technique I used for this...
The older functions pick a random number as a start point, then increment the id, wrapping back to 1 when they reach a certain limit and continuing until they reach the start point. If they find an unused ID along the way, then they return that number and stop.

The newer functions use a far faster technique. Each resource keeps a set of 2 integers denoting a range of unused IDs, for example objects have a single pair of 1 to 262144 (256k). When an object is allocated, the ranges are checked to find the relevant one. If the id is the first in the range, then the start is adjusted up by 1, if it's the last in the range, then the end is adjusted down by 1, and if the number is in the middle of the range, the range is split into 2 ranges.

Here's an example:
Start with a range of 1-1000
Allocate id 10 -> 1-9,11-1000
Allocate id 11 -> 1-9,12-1000
Allocate id 9 -> 1-8,12-1000
Allocate id 20 -> 1-8,12-19,21-1000

When an id is freed then a similar search is carried out. If the id is 1 less than the start of a range, the start is reduced by 1. If the id is 1 greater then the end of a range, then 1 is added to the end. Once this is carried out, a quick check is made to see if 2 ranges can be joined into a single range. If no match is found, then a new range is created with the start and end set to the same id.

Free id 9 -> 1-9,12-19,21-1000
Free id 11 -> 1-9,11-19,21-1000
Free id 20 -> 1-9,11-20,21-1000 -> Now have two ranges that can be joined -> 1-9,11-1000
Free id 10 -> 1-10,11-1000 -> Now have two ranges that can be joined -> 1-1000

The implementation I used doesn't use an array or linked list, but that's basically it.

Although I haven't implemented it, you can also use the same process for determining ids that are in use too, simply by looping from 1 to the start of the first range, then from the end of the first range to the start of the second range etc.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 24th Jul 2010 04:50 Edited at: 27th Jul 2010 08:25
Direct Link removed. Array Allocation Management Examples can be found on our forums.

GIDustin
18
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 24th Jul 2010 06:29
Quote: "Array Allocation Management Examples"

Your link doesnt work for me.

I have always used IanM's "find free ____()" functions just because I didnt really want to write my own. Turns out, they work fast and they work well. I havent found the need to write my own resource manager module and now after IanM explained how they work I think I made the right choice.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 24th Jul 2010 08:01
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Jul 2010 20:51
@GIDustin,
I'm slowly converting all of my functions to use the fast range-based method, but it takes time to do - it takes anywhere from an hour, up to a day to ensure that each resource I implement it for is correctly dealt with.

Sprites and Images (in the next release) each took 2-3 hours each.

GIDustin
18
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 25th Jul 2010 00:29
Quote: "'m slowly converting all of my functions to use the fast range-based method, but it takes time to do "


Updating your Matrix plugins, working on the D3D_func plugin, doing bug fixes for DBPro commands.... And you somehow have time to post on the forums!?

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 25th Jul 2010 06:34
Quote: "And you somehow have time to post on the forums!? "
IMPATIENT! I hope that was in jest.

The fastest code is the code never written.
GIDustin
18
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 25th Jul 2010 08:14
Quote: "IMPATIENT! I hope that was in jest."


Yeah, with all the work he does around here I just find it odd that he has time to socialize. It is a good thing though!

RedFlames
18
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 25th Jul 2010 23:24
Okay thanks to everyone who posted, I guess I'll have to wait for Ian's new awesome functions.
And I'll have a look at the "freelist" commands, seems like a good solution.

Login to post a reply

Server time is: 2026-07-25 05:32:48
Your offset time is: 2026-07-25 05:32:48