- I decided to skip further analysis and go straight to coding.
- I made the changes in mod_megaphone to send back an HTTP response.
- I discovered an issue with an array growing without bounds
I thought about creating a HashMap with all the goodies --- chaining and all that stuff, but then I realized something: Scarlett Johansson looks very good in spandex. OK, I realized something more practical: for this situation the hash value for the key was not set. That is, I could choose any value I wanted for the key.
This means that I could be in the situation where the connection ID gets chosen in such a way that it does not collide with any of the other entries in the table, and that way avoid having to create chaining algorithms. What's more, I could use the connection ID as a direct index into the table.
Now this may sound sort of like "the tail wagging the dog." This is because I'm that lazy.
So I create a largish array --- say 32,000,000 entries or thereabouts, and then, to get a connection ID, just randomly choose a number that's in the range of the table. Check the array to see if something is there. If not, then I have my connection ID. If the entry is already being used, then just start incrementing the index and checking until you find a free entry.
The performance of this approach would depend greatly on the quality of the random number generator and the degree to which the table is occupied. Since I'm lazy I'm choosing a relatively "sparse" array --- 32 million entries should roughly 32 times the number of entries that I need. I can fine tune the table later.
Note that this is perhaps the only time I have ever had the luxury of being able to choose the hash value for an object and thereby being able to use this simple minded approach. I suppose I could do things to improve the performance by keeping track of the last connection ID used and just increment the value. By the time the value wraps again, anything still using the old value is almost certainly timed out and can be discarded.
Nah.
Anyhow I will code this up and it will just plain work. The first time. Yeah. Sure it will. Maybe I need to drink before I do this...
No comments:
Post a Comment