(I will not comment on how efficient the general idea is (with the specific key structure and counters etc.) as this is only part of the code. I will comment on just what I see here.) I understand that you loop over ALL keys to find the non-expired keys. Then you forget about the hard-earned array of NON expired keys @keys you have constructed and proceed to conditionally call purge() which probably has to do it again (internally, it has to find the set of expired keys this time I guess), in a similar or more efficient way.

If you want to retain the data structure you are using, then you could break the loop as soon as you find even one expired key and forget about the array, just retain the fact that expired keys were found and purge() must be called (conditionally). Given that you purge if there is at least one expired key (on a 5% basis). Then you are left with finding if there is additional space (the return 1:0) but can't purge() return you the number of keys purged so that you could calculate if there is still space?

A more drastic change would be to find all expired keys yourself. Given that you are doing it already now, or with my above suggestion you will probably have to partially do it, it will be a benefit if you believe you can do it in a more efficient way than purge. But then you must pass that set of expired keys you found to purge() so that it does not have to re-construct it. Does it take hints? And does it return some info?

A different path would be to observe that purging will be done when there are expired keys ONLY 5% of the time (rand()<0.05). So, if there is another way to calculate whether there is space left (return @keys <= $self->{max_items} ? 1:0;) the expensive loop AND purge() could be avoided. You only have to find expired keys 5% of the time. But you need alternative way to find out if you are full.

bw, bliako


In reply to Re: Optimizing a CHI-based data throttler by bliako
in thread Optimizing a CHI-based data throttler by perlancar

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.