aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Westerberg <drclaw@mac.com>2020-03-01 22:44:13 +1000
committerRuben Westerberg <drclaw@mac.com>2020-03-01 22:44:13 +1000
commitfabec5ef15134eefc9840b3c6d5e3b187341cb4b (patch)
treecb880e370c95bf8ccf6be7efa788a56d65969cba
parentebdc657948493ea995af8ab98004942a182317ab (diff)
downloadperlweeklychallenge-club-fabec5ef15134eefc9840b3c6d5e3b187341cb4b.tar.gz
perlweeklychallenge-club-fabec5ef15134eefc9840b3c6d5e3b187341cb4b.tar.bz2
perlweeklychallenge-club-fabec5ef15134eefc9840b3c6d5e3b187341cb4b.zip
Added ch-2 pl and raku
-rw-r--r--challenge-049/ruben-westerberg/README8
-rwxr-xr-xchallenge-049/ruben-westerberg/perl/ch-2.pl112
-rwxr-xr-xchallenge-049/ruben-westerberg/raku/ch-2.raku108
3 files changed, 226 insertions, 2 deletions
diff --git a/challenge-049/ruben-westerberg/README b/challenge-049/ruben-westerberg/README
index 9f659c966a..1e26e2861c 100644
--- a/challenge-049/ruben-westerberg/README
+++ b/challenge-049/ruben-westerberg/README
@@ -2,8 +2,12 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.raku
===================
-Survivor program. Run to find the survivor
+Run program with a single commandline argument with the number to test. Prints out a multiple of the number only containing 1s and 0s. 55 is used as the default if not value is provided
ch-2.pl and ch-2.raku
===================
-Palindrone dates between 2000 and 2999
+Demonstration of LRU cache.
+
+A 'slow large' backing store is simulated with a delay in accessing the elements of the store.The cache loads values from the store when a cache 'miss' occours in reading. In writing, the cache only writes to the backing store when the cached element is to be deleted from the LRU list.
+
+The demonstration program randomly uses read and write calls to the cache. There are more acces operations than the original size of the backing store. It will grow and show undefined elements as 'N/A'
diff --git a/challenge-049/ruben-westerberg/perl/ch-2.pl b/challenge-049/ruben-westerberg/perl/ch-2.pl
new file mode 100755
index 0000000000..148ec30c52
--- /dev/null
+++ b/challenge-049/ruben-westerberg/perl/ch-2.pl
@@ -0,0 +1,112 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+no warnings 'experimental';
+use Time::HiRes;
+use v5.24;
+my ($GET_CMD,$SET_CMD,$SYNC_CMD,$UPDATE_CMD,$DUMP_CMD)=0..4;
+
+
+print "Inital backing store contents:\n";
+largeSlowStore($DUMP_CMD);
+
+my $counter=0;
+
+for (0..19) {
+ print "\nOperation: $counter\n";
+ fastSmallCache(int(rand($SET_CMD+1)),int(rand(20)), int(rand(100)));
+ fastSmallCache($DUMP_CMD);
+ print '=' x 20,"\n";
+ $counter++;
+}
+print "Resulting backing store before flush/sync of lru cache\n";
+largeSlowStore($DUMP_CMD);
+
+fastSmallCache($SYNC_CMD);
+print "Resulting backing store after sync \n";
+largeSlowStore($DUMP_CMD);
+
+sub fastSmallCache {
+ my ($cmd,$index,$value)=@_;
+ state $capacity=5;
+ state %index;
+ state @lru;
+ do {
+ given ($cmd) {
+ when ($GET_CMD) {
+ print "\nRead Cache: ";
+ do {given ($index{$index}) {
+ when (undef) {
+ print "-Miss\n";
+ #cache miss
+ my $val=largeSlowStore($GET_CMD,$index);
+ fastSmallCache($UPDATE_CMD,$index,$val) if defined $val;
+ }
+
+ default {
+ #cache hit
+ print "-Hit\n";
+ fastSmallCache($UPDATE_CMD,$index,$_);
+ }
+ }
+ }
+
+ }
+ when ($SET_CMD) {
+ print "\nWrite cache: \n";
+ fastSmallCache($UPDATE_CMD,$index,$value);
+ }
+ when ($SYNC_CMD) {
+ print "Syncing cache\n";
+ largeSlowStore($SET_CMD,$_, $index{$_}) for (@lru);
+ %index=();
+ @lru=();
+ }
+ when ($UPDATE_CMD) {
+ print "Updating cache: $index => $value\n";
+ my ($k)=grep {$lru[$_] == $index} 0..@lru-1;
+ my $tmp;
+ $tmp=splice @lru,$k,1 if defined $k;
+ #print "cached pre key: $tmp\n";
+ my $del=shift @lru unless @lru < $capacity;
+ if (defined $del) {
+ print "Cache overflow\n";
+ largeSlowStore($SET_CMD,$del,$index{$del});
+ delete $index{$del};
+ }
+ push @lru,$index;
+ $index{$index}=$value;
+ }
+ when ($DUMP_CMD) {
+ print "LRU keys: ", join ", ", @lru;
+ print "\n\n";
+ }
+
+ }
+ }
+}
+
+
+
+sub largeSlowStore {
+ my ($cmd,$index,$value)=@_;
+ state @data= map {int rand 10} 1..10; #initialize store
+ do {
+ given ($cmd) {
+ Time::HiRes::usleep 1000000 * rand 2;
+ when ($GET_CMD) {
+ print "Read backing store\n";
+ $data[$index];
+ };
+ when ($SET_CMD) {
+ print "Write backing store\n";
+ $data[$index]=$value;
+ }
+ when ($DUMP_CMD) {
+ print join ", ", map { defined $_? $_:"N/A"} @data;
+ print "\n";
+ }
+ }
+ }
+}
diff --git a/challenge-049/ruben-westerberg/raku/ch-2.raku b/challenge-049/ruben-westerberg/raku/ch-2.raku
new file mode 100755
index 0000000000..9f48e9db3f
--- /dev/null
+++ b/challenge-049/ruben-westerberg/raku/ch-2.raku
@@ -0,0 +1,108 @@
+#!/usr/bin/env raku
+
+my ($GET_CMD,$SET_CMD,$SYNC_CMD,$UPDATE_CMD,$DUMP_CMD)=0..4;
+
+
+put "Inital backing store contents:";
+largeSlowStore($DUMP_CMD);
+
+my $counter=0;
+
+for ^20 {
+ put "\nOperation: $counter";
+ fastSmallCache(($SET_CMD+1).rand.Int,index=>20.rand.Int, value=>100.rand.Int);
+ fastSmallCache($DUMP_CMD);
+ put '=' x 20;
+ $counter++;
+}
+
+put "Resulting backing store before flush/sync of lru cache";
+largeSlowStore($DUMP_CMD);
+
+fastSmallCache($SYNC_CMD);
+put "Resulting backing store after sync ";
+largeSlowStore($DUMP_CMD);
+
+sub fastSmallCache($cmd,:$index,:$value){
+ state $capacity=5;
+ state %index;
+ state @lru;
+ do {
+ given $cmd {
+ when $GET_CMD {
+ put "\nRead Cache: ";
+ do { given %index{$index} {
+ when Any {
+ put "-Miss";
+ #cache miss
+ my $val=largeSlowStore($GET_CMD,:$index);
+ fastSmallCache($UPDATE_CMD,:$index,value=>$val) if defined $val;
+ }
+
+ default {
+ #cache hit
+ put "-Hit";
+ fastSmallCache($UPDATE_CMD,:$index,value=>$_);
+ }
+ }
+ }
+
+ }
+
+ when ($SET_CMD) {
+ put "\nWrite cache: ";
+ fastSmallCache($UPDATE_CMD,:$index,:$value);
+ }
+
+ when ($SYNC_CMD) {
+ put "Syncing cache";
+ largeSlowStore($SET_CMD,index=>$_, value=>%index{$_}) for (@lru);
+ %index=();
+ @lru=();
+ }
+
+ when ($UPDATE_CMD) {
+ put "Updating cache: $index => $value";
+ my ($k)=(^@lru).grep({@lru[$_] == $index});
+ my $tmp;
+ $tmp=@lru.splice($k,1) when $k.defined;
+ my $del=@lru.shift unless @lru < $capacity;
+ if (defined $del) {
+ put "Cache overflow";
+ largeSlowStore($SET_CMD,index=>$del,value=>%index{$del});
+ %index{$del}:delete;
+ }
+ @lru.push: $index;
+ %index{$index}=$value;
+ }
+ when ($DUMP_CMD) {
+ put "LRU keys: ", @lru.join: ", ";
+ put "\n";
+ }
+
+ }
+ }
+}
+
+
+
+sub largeSlowStore($cmd,:$index,:$value) {
+ state @data= 10.rand.Int xx 10;
+ do {
+ given ($cmd) {
+ sleep(2.rand);
+ when ($GET_CMD) {
+ put "Read backing store";
+ @data[$index];
+ };
+ when ($SET_CMD) {
+ put "Write backing store";
+ @data[$index]=$value;
+ }
+ when ($DUMP_CMD) {
+ put @data.map({ defined($_)??$_!!"N/A"}).join: ", ";
+ put "";
+ }
+ }
+ }
+}