in reply to Re^2: Schizophrenic var
in thread Schizophrenic var
I used "dualvar" also, in MCE::Shared::Cache. When max_age is specified during construction of the cache, the dualvar is used internally to store the expiration time with the key name in the keys array.
# update expiration $keys->[ $off ] = ( $exptime >= 0 ) ? dualvar( time + $exptime, $_[1] ) : dualvar( -1, $_[1] );
Basically, the "dualvar" capability in Perl allows the "max_age" feature without increasing the memory consumption of the cache or impacting performance.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Schizophrenic var (semipredicate problem)
by eyepopslikeamosquito (Archbishop) on Jul 01, 2023 at 09:43 UTC | |
Interesting!
Vaguely related is C++-17's std::variant and Haskell's Type Inference system, with types like Maybe Real and Either String Char. So perhaps we can say that Perl's dualvar helps solve the Semipredicate problem ... though admittedly, without dualvar, ordinary Perl scalars can use undef to signal invalid input. I'm not an expert on any of this, just coincidentally happened to be looking at this topic while updating my notes on exception handling vs error returns in functions. :) Ideas/corrections and other cool references on this topic welcome! References Added Later
| [reply] [d/l] [select] |
by marioroy (Prior) on Jul 01, 2023 at 14:58 UTC | |
> Save it for very special situations, like impressing people at conferences and cocktail parties. The use of "dualvar" in MCE::Shared::Cache is not due to schizophrenic behavior or trying to impress people. Nothing like that. There was a dated article on the web (I misplaced the link) where someone compared several pure-Perl caching modules. In addition to performance, the author of the article compared memory consumption. The thing that I remember is the extra memory consumption using a hash to store the expiration time. For me, "dualvar" solved a problem. I was able to add the "max_age" feature to MCE::Shared::Cache without greatly impacting performance or spike in memory consumption. | [reply] |
by marioroy (Prior) on Jul 27, 2023 at 14:19 UTC | |
The dated "perl-benchmark-cache-with-expires-and-max-size" article no longer exists on the web. Fortunately, Celogeek's GitHub repository exists, containing the article scripts. I made two test scripts locally, at the time, when developing MCE::Shared::Cache. What I recall from the article is the author captured memory consumption. Thus, the priority for me was ensuring low memory consumption first, then performance. test-cache-mce.pl
test-cache-mce-with-expires.pl
Non-shared results:
Non-shared results with expires: Perl's dualvar capability is the reason why MCE::Shared::Cache is able to offer max_age capability without increasing memory consumption or greatly impacting performance.
Shared cache with expiration: The MCE::Shared::Cache documentation provides a parallel demonstration, using a shared cache. Here, I modified the code to match the Redis variant (i.e. set key-value pair with expiration).
Finally, the diff output for the Redis example.
Results:
| [reply] [d/l] [select] |
by marioroy (Prior) on Jul 27, 2023 at 17:29 UTC |