Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the 256 key limit only for efficiency? #2

Open
dylanevl opened this issue Jun 26, 2011 · 3 comments
Open

Is the 256 key limit only for efficiency? #2

dylanevl opened this issue Jun 26, 2011 · 3 comments

Comments

@dylanevl
Copy link

Looking at the code, I can't see any reasons why the 256 key limit is needed.

Am I missing something or is it simply there to improve the overall performance of the pseudo-database?

@creationix
Copy link
Owner

It's a requirement of the db storage format. Each cell in the grid is
an 8-bit number. You can't hold more than 256 items in 8 bits.

You could change the algorithm to be 16 bits per tile if you wanted
more indexes. Then you could have a lot more possibilities with only
twice the storage and memory usage.

On Sun, Jun 26, 2011 at 11:12 AM, dylanevl
reply@reply.github.com
wrote:

Looking at the code, I can't see any reasons why the 256 key limit is needed.

Am I missing something or is it simply there to improve the overall performance of the pseudo-database?

Reply to this email directly or view it on GitHub:
#2

@dylanevl
Copy link
Author

I commented and deleted this before because I thought I was wrong, but after reviewing the code some more, I think i could be right. There were kudos for the Adventure code in there in case github didn't automatically send it to you. I've enjoyed picking it apart, it's been a nice introduction to node.

Back to increasing the number of keys-- is it just a matter of changing the file encoding to 'base64' and then adjusting the tileSize variable? Right now it's set to 1024 or (2^8)_4, so I assume I'd need to change it to (2^16)_4 for 16-bit keys?

@creationix
Copy link
Owner

There shouldn't be any encoding (utf8 or otherwise) since it's not a string, but a buffer. What I meant was to double the size of the buffer and then every time the buffer is accessed you'd need to first double the memory offset and split the number into it's higher and lower 8 bits and set both bytes manually. Then to read out of the buffer, you'd double the offset and read the two halves and add them together.

This is untested code, but it should give you an idea of how to read and write 16 bit numbers out of a buffer. I don't remember the exact function to map x and y to the buffer. Also you'll still need to convert between the index and the actual value.

function read16(buffer, x, y) {
  var offset = (x + y * 1024) * 2;
  return (buffer[offset] << 8)  + buffer[offset + 1];
}

function write16(buffer, x, y, index) {
  var offset = (x + y * 1024) * 2;
  buffer[offset] = index >> 8;
  buffer[offset + 1] = index & 0xff;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants