MakeID - Unique ID generator
Tuesday, December 31, 2013 | Permalink
It's been a while since I posted anything here; between family life and a computer that was broken for a month or two there hasn't been much opportunity to post anything, but now at last I can offer an update. Yay!
So
here's a piece of code that I have probably spent a little too much time tweaking considering the relatively narrow scope. It's a simple header-file only library for generating unique IDs at runtime. The reason I conjured up this from the beginning was sort of a problem I encountered at work. Basically our sort-IDs that we use in the rendering system are 64bit. Typically we would throw in a pointer to a "RenderBlock" as they are called in our system, which more or less corresponds to the data for a draw-call. In 32bit this was fine, as we still had 32bits for encoding other stuff, like shader permutation, state flags etc. 32bits for distinguishing things that are basically in the order of tens of thousands is a bit of a waste. And now with 64bit pointers, it's eating the entire sort-ID, except the bottom 4 bits because of 16byte alignment. So I wanted to create unique IDs for these that are much smaller, say 16bits or so. So I made a lib to manage a pool of IDs and always returns the smallest free ID. So if you allocate 5 handles, you'll get 0,1,2,3,4, and then if you free 3 and later ask for a new ID, you'll get the 3 again.
Beyond this particular use case I can also see some other uses. For instance as replacement for hashes, handles or resource pointers at runtime. It can also be used for managing a pool of shared resources, where the ID can be used as an index into an array. The underlying storage can be reallocated without a need to update IDs handed out as handles elsewhere. And all IDs bunched up at the bottom of the array is of course also great for memory and cache behavior.
There may be other uses as well that I haven't thought of. In any case, the code is released in public domain, so feel free to use any way you want. Any feedback is of course also welcome.
Update 2014-01-23:
There's now a 1.01 version available. No dramatic changes, mostly minor code review fixes.
[
1 comments |
Last comment by Denis Gladkiy (2014-01-08 18:32:19) ]