| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package LinkedList::Single::Example::LRU::Lazy v2.0.0; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use Carp qw( croak ); | ||
| 8 | |||
| 9 | ######################################################################## | ||
| 10 | # the array programmer's best version: nothing is ever searched for or | ||
| 11 | # spliced out. | ||
| 12 | # | ||
| 13 | # a hash holds key => [ value, stamp ]. every use pushes [ key, stamp ] | ||
| 14 | # at the back of a queue with a fresh stamp, and leaves the older queue | ||
| 15 | # entry for that key behind as a husk: it is stale because the hash now | ||
| 16 | # has a newer stamp. eviction shifts entries from the front, skipping | ||
| 17 | # the husks. when the queue holds too many husks it is compacted, which | ||
| 18 | # is O(n) but happens once per O(limit) operations. | ||
| 19 | # | ||
| 20 | # the price: a queue longer than the cache (up to 3x), husks to | ||
| 21 | # recognise, a stamp in every entry, and a compaction rule to get right. | ||
| 22 | ######################################################################## | ||
| 23 | |||
| 24 | sub new( $class, %arg ) | ||
| 25 | { | ||
| 26 | my $limit = $arg{ limit } | ||
| 27 | // croak 'Botched new: missing limit'; | ||
| 28 | |||
| 29 | $limit > 0 | ||
| 30 | or croak "Botched new: limit '$limit' is not positive"; | ||
| 31 | |||
| 32 | bless | ||
| 33 | { | ||
| 34 | limit => $limit | ||
| 35 | , on_evict => $arg{ on_evict } // sub {} | ||
| 36 | , entry => {} | ||
| 37 | , queue => [] | ||
| 38 | , stamp => 0 | ||
| 39 | } | ||
| 40 | => $class | ||
| 41 | } | ||
| 42 | |||
| 43 | # a queue entry is current if the hash still has its stamp. | ||
| 44 | |||
| 45 | sub _current( $self, $queued ) | ||
| 46 | { | ||
| 47 | my $entry = $self->{ entry }{ $queued->[ 0 ] } | ||
| 48 | or return 0; | ||
| 49 | |||
| 50 | $entry->[ 1 ] == $queued->[ 1 ] | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _touch( $self, $key ) | ||
| 54 | { | ||
| 55 | my $stamp = ++$self->{ stamp }; | ||
| 56 | my $queue = $self->{ queue }; | ||
| 57 | |||
| 58 | $self->{ entry }{ $key }[ 1 ] = $stamp; | ||
| 59 | |||
| 60 | push $queue->@*, [ $key, $stamp ]; | ||
| 61 | |||
| 62 | # compaction: once husks outnumber the live entries 2:1. | ||
| 63 | |||
| 64 | if( @$queue > 3 * $self->{ limit } + 16 ) | ||
| 65 | { | ||
| 66 | $queue->@* = grep { $self->_current( $_ ) } $queue->@*; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub get( $self, $key ) | ||
| 71 | { | ||
| 72 | my $entry = $self->{ entry }{ $key } | ||
| 73 | or return undef; | ||
| 74 | |||
| 75 | $self->_touch( $key ); | ||
| 76 | |||
| 77 | $entry->[ 0 ] | ||
| 78 | } | ||
| 79 | |||
| 80 | sub put( $self, $key, $value ) | ||
| 81 | { | ||
| 82 | my $entry = $self->{ entry }{ $key } //= []; | ||
| 83 | |||
| 84 | $entry->[ 0 ] = $value; | ||
| 85 | |||
| 86 | $self->_touch( $key ); | ||
| 87 | |||
| 88 | while( keys $self->{ entry }->%* > $self->{ limit } ) | ||
| 89 | { | ||
| 90 | my $queued = shift $self->{ queue }->@*; | ||
| 91 | |||
| 92 | $self->_current( $queued ) | ||
| 93 | or next; | ||
| 94 | |||
| 95 | my $old = delete $self->{ entry }{ $queued->[ 0 ] }; | ||
| 96 | |||
| 97 | $self->{ on_evict }->( $queued->[ 0 ], $old->[ 0 ] ); | ||
| 98 | } | ||
| 99 | |||
| 100 | $self | ||
| 101 | } | ||
| 102 | |||
| 103 | sub exists( $self, $key ) | ||
| 104 | { | ||
| 105 | exists $self->{ entry }{ $key } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub size( $self ) | ||
| 109 | { | ||
| 110 | scalar keys $self->{ entry }->%* | ||
| 111 | } | ||
| 112 | |||
| 113 | sub limit( $self ) | ||
| 114 | { | ||
| 115 | $self->{ limit } | ||
| 116 | } | ||
| 117 | |||
| 118 | # newest first: the current queue entries, from the back. | ||
| 119 | |||
| 120 | sub contents( $self ) | ||
| 121 | { | ||
| 122 | [ | ||
| 123 | map { [ $_->[ 0 ], $self->{ entry }{ $_->[ 0 ] }[ 0 ] ] } | ||
| 124 | grep { $self->_current( $_ ) } | ||
| 125 | reverse $self->{ queue }->@* | ||
| 126 | ] | ||
| 127 | } | ||
| 128 | |||
| 129 | 1 | ||
| 130 | __END__ |