...but don't worry, it's SHA1.
Here's some background: in order to have R create graphs, I needed unique filenames so one graph wouldn't overwrite the next. I also couldn't keep count very effectively, as there can be multiple R drawing objects adding graphs at once. I ended up using a timestamp, down to the microsecond level, which worked well. R can make graphs quickly, but not that quickly, so I ended up with unique filenames.
The trouble is those graphs were strictly one-use. "Hey PHP, get me that lineplot you made at 1311025677.192238 Unix time, 'kay?" Although I admittedly refresh the page a lot more than a normal user would, I generated about 7000 images in the last day or two. Just one user. Removing the photos each day when they get too old isn't hard, but there was also the end-user time problem. Each of those page retrievals took about five (sometimes as much as eight) seconds, and about three till anything was visible.
So today I worked entirely on my R object, and implemented a filename hash. I convert the data array sent to the object into a string, hash that string, append that onto whatever type of graph I'm making, and use that as a filename. If the data array sent is the same as a graph that's already created, it creates the same filename, and I just load that picture instead. First-time creation of a page still takes five seconds, but if the files are already created, it only takes four. If the user has already been to the page and their browser has cached the pictures, it takes three. I still would like it faster, and I'll do some profiling to find out what I can trim, but it's much better. Also, the number of graphs created ends up being much less, as there is reuse even between different questions and tasks.
There is one possible trouble, however. Hashes aren't guaranteed to return unique values -- two different values can return the same hash. This is called collision. If this were to happen, I could have two different graphs with the same filename. The chances of this happening, however, are very, very, very low. In a perfectly random system, the chances of collision reach 50% when there are 2^80 ( 1,208,925,819,614,629,174,706,176) objects. We don't even have that many bits of storage space on rawr. If I clear out the pictures once in a while, the chances decrease more.
No comments:
Post a Comment