Nicolas Cannasse wrote:
Here is another implementation which makes fib2_3 twice as fast for
me (~32s vs. ~14s). Unless I missed something, that actually puts it
as fast as (or maybe slightly faster than) Ruby 1.8.7.
//No changes to Object
Integer = $new(null)
Integer.cache = $hnew(10) //keep a cache of integer
[...]
The issue here is that you will waste a lot of memory.
You'd certainly need some limit or LRU policy for the cache, but it does
speed things up in the tests I've used. There's definitely some
trade-off there, though. It could save memory by having a single copy of
each Integer used, and checking the cache seems to be faster than
allocating a new object. On the other hand, there is some penalty for
checking the cache, and those Integers will hang around forever and not
be garbage collected, thus wasting memory. A more sophisticated cache
management policy might not be worth the overhead, I don't know.
What about my proposal, which was :
IntegerProto = $new(null);
IntegerProto.value = null; // allocate object slot
$objsetproto(IntegerProto,Integer);
Integer.new = function(num) {
var o = $new(IntegerProto);
o.value = num;
o;
}
What is the advantage of having the intermediate IntegerProto object?
Does allocating the value slot on it instead of the new object make a
difference somehow? I'm curious.
-Justin
--
Neko : One VM to run them all
(http://nekovm.org)