contact luis

luis is a co-founder and social software architect at Infinite.ly. he likes building small web toys a whole lot. More ...

quick links to the good stuff

  • 25 First Dates 25 May 2009
  • True Crime: Confessions of a Criminal Mastermind 17 Feb 2009
  • Finding Your Soul Mate: A Statistical Analysis 27 Jan 2009
  • Sex and Schrodinger's Cat 07 January 2009
  • An Extended Rant on Heroes 26 September 2008
  • Zero Barrier 05 May 2008
  • Sweatshop Blogging Economics 08 April 2008
  • The Doomsday Singularity 25 February 2008
  • Piracy and Its Impact on Philippine Music 21 January 2008
  • The Manila Pen-etration by the Hotelier Antonio Trillanes 29 November 2007
  • Journey of a Thousand Heroes 17 December 2006
  • Shake, Rattle & LOL 30 December 2005

@helloluis on Twitter

    elsewhere online

    • LinkedIn
    • Facebook
    • Last.FM
    • Del.icio.us
    • Flickr
    • Plurk
    • Multiply
    • Stumbleupon

    guttervomit

    • 0

      Optimization Time

      29 Jan 2006

      [ WARNING: Extremely geeky technical content ahead. Commbadges should be firmly affixed and tricorders should be at the ready. ]

      One of the Gibbity features I’m most excited about is the ability to explore the games listing by tags. I got the idea from browsing Emusic’s very nice directories last week and decided to implement something similar in Gibbity.

      In a nutshell: users choose one of the top 20 tags with which to begin their search. So assuming you chose "pc," you’d then be presented with a page containing all of the games tagged "pc." On the sidebar you’d also find a list of related tags, which you can use to further refine your search, e.g., "strategy," "rpg," "fps," etc. Clicking "fps" gets you a page with games matching "pc" and "fps," with its own sidebar of related tags. Theoretically you could refine your search an infinite number of times (although in practice you’d probably run out of matching games after the third or fourth tag choice).

      I was pretty proud of this infinitely-nesting search when I put it together a few days back, but there were some major doubts in my mind whether I could write it in such a way as to be resource-friendly (i.e., it wouldn’t bring the server to its knees every time a few dozen people try to use it).

      True enough, the search was a friggin’ hog. I ran a very simple script timer to see how long it took the PHP engine to process the instructions of my sample pc+fps search, and got an average of about 0.3 seconds on each run.

      Now, 0.3 seconds may not seem like much, but I can tell you that that is a lifetime for a multi-user web-based application. Most scripts shouldn’t take longer than a hundredth of a second to run, because you are assuming an environment where the server is getting hammered by hundreds of requests per second. (By comparison, Gibbity’s homepage only takes 0.02 seconds to render from top to bottom.)

      After trying various things to optimize the matching code, I simply could not get the execution time any lower than a quarter of a second. Finally, I decided that the only way I was going to shave a few milliseconds of this operation was to cheat, and cache the code.

      Code-caching is basically what it sounds like: instead of actually searching for games tagged "pc" and "fps," the engine cheats and serves up stored results from previous searches. The idea is that you become very efficient when serving popular searches like the aforementioned pc+fps, although you take a small hit when running obscure searches like say, pc+fps+balloons.

      There are a bunch of different ways to implement caching. The (very basic) method I chose was to save the actual PHP array of search results in to a text file, i.e., all games matching "pc" are in a file called tags_pc.cache, and all games matching "fps" are in a file called tags_fps.cache. So when anybody asks for pc+fps, all the engine has to do is look for the two *.cache files, and intersect their values. The beauty of this method is that even the obscure searches (pc+fps+balloons) benefit from the caching, because only "balloons" will actually have to be run.

      Now that I had gotten the caching part working, my next problem was figuring out how to keep the cache fresh. Since the cache is totally disconnected from the present state of the site, new games could be added at any time that wouldn’t be reflected in the cache.

      My first idea was to destroy and rebuild specific caches every time a new game and/or tag was introduced by a user. So for example, if someone added that new game "Hell Gate: London" and tagged it as "pc," "fps," and "mmorpg," I would destroy and rewrite those 3 tags_*.cache files. That would mean 100% accurate caches at all times, which is great. The problem though, is that popular tags like "pc" have pretty big lists of games ("pc" has 668 games already, and I haven’t even debuted the site yet), so it’d be an equally large resource hog to write and rewrite those caches every time a user makes an entry.

      My second idea, and the one I eventually went with, was to have a simple expiration on each cache. So if we run our search and we find that one of the caches are older than an hour, we destroy it, rebuild it and display its contents. That means that one person every hour or so, will get a slow search (in the region of 0.3-0.4 seconds) but everyone else who runs a similar search within that time period gets a free ride. By "free ride," I mean 0.015 second execution times, which are the results I’ve been getting from my testing (Savings of over 90% = good).

      The performance increase was so dramatic that I almost forgot that there were still many, many parts of the site that needed optimization, but at least I’m off to a good start :) 

      Leave a Reply

     

    categories

    • Home
    • Business (42)
      • Acquisitions (15)
      • Goin' Legit (61)
    • Media (53)
      • Artwork (13)
      • Books (22)
      • Comics (9)
      • Movies (142)
      • Music (102)
      • Photography (33)
      • Poker (10)
      • TV (30)
    • Randomness (301)
    • Site News (8)
    • Technology (69)
      • Games (14)
      • Hardware (113)
      • Social Software (45)
      • Software (131)
    • Tutorials (16)

    archives

    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • August 2010
    • May 2010
    • April 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • July 2005
    • June 2005
    • May 2005
    • April 2005
    • March 2005
    • February 2005
    • January 2005
    • December 2004
    • November 2004
    • October 2004
    • September 2004
    • August 2004
    • July 2004
    • June 2004
    • May 2004
    • April 2004
    • March 2004
    • February 2004
    • January 2004
    • December 2003
    • November 2003
    • October 2003
    • September 2003
    • August 2003
    • July 2003
    • June 2003
    • May 2003
    • April 2003
    • March 2003
    • February 2003
    • January 2003
    • December 2002
    • November 2002
    • October 2002
    • September 2002
    • July 2002
    • May 2002
    • April 2002
    • February 2002
    • January 2002
    • December 2001
    • November 2001
    • October 2001

    friends

    • Dementia
    • Gabby
    • Gail
    • Gibbs
    • Helga
    • Ia
    • Ina
    • Jason
    • Kaye
    • Lauren
    • Lizz
    • Luna
    • Mae
    • Migs
    • Mike
    • Ryan
    • Sacha
    • Vicky
    • Vida
    • Yuga

    search

    notes

    Guttervomit v3 went online in January, 2008. It uses Wordpress for publishing, and was built largely with Adobe Illustrator and Textmate. Logotype and navigation is set with Interstate.