Alright, so I'm reinventing the wheel with something and I want everyone's opinion. I'm writing a simple circular cache to use inside a module of mine. This cache is specific to the module and I want it to be as fast as possible.

Here's the code so far:

package GNS::Cache; use strict; ########### ## How this for strangeness ... ########### sub new { my $class=shift; my $size=shift; my $self=bless { }; $self->{size}=($size ? $size : 30); $self->{list}=[]; $self; } sub check { my $self=shift; my $id=shift; local $_; ## walk through the list foreach (@{$self->{list}}) { if ($_->[0]==$id) { return $_->[1]; } } return; } sub insert { my $self=shift; my $id=shift; my $data=shift; my $list=$self->{list}; push @$list,[$id,$data]; shift @$list if $#$list>=$self->{size}; } sub purge { my $self=shift; $self->{list}=[]; } sub dump { my $self=shift; local $_; foreach (@{$self->{list}}) { print "$_->[0] = $_->[1]\n"; } } ####### 1; __END__ #######
#1. In the operations that you see, is there anyplace where a code change could be applied that would make it faster?

#2. In the check() function, if a hit is found, I would like the item move to the top of the list (representing it should stay in the cache longer). What I need to do is remove the current item ($_ as it stands now) and tack it on to the end of the list via a push. How would you recommend doing it? I dont care about cleverness, or obsfucation, but raw number of instructions. I had though of doing an undef $_; inside the loop, but that doesn't seem to actually remove the item from the array, just undef it out (the list continues to have the same # of elements). I suspect I need to splice it, but without having the index of the element of the array, I couldn't figure out how to do it... efficiently.

#3. In my purge() function, I do a simple $self->{list}=[] to reset the list. I presume Perl's memory management is good enough to handle that style of deallocation. Correct?

There are a bazillion methods of doing this, and I've tried a few, but I'm curious as to everyone else's ideas.

Thanx! and forgive me if this is not well written, I'm just on my first cup of java.

Thanx!


In reply to Efficiency Question by mr.nick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.