[MERGE] FIFOCache and FIFOSizeCache

John Arbash Meinel john at arbash-meinel.com
Tue Dec 9 22:54:19 GMT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Collins wrote:
> bb:tweak
> 
> fifo_cache and lru_cache module docstrings should reference each other I
> think.
> 
> 
> +        if key in self:
> +            # Remove the earlier reference to this key, adding it again
> bumps
> +            # it to the end of the queue
> +            del self[key]
> +        self._queue.append(key)
> 
> might be better as:
> try:
>     # Remove the earlier reference to this key, adding it again bumps
>     # it to the end of the queue
>     del self[key]
> except KeyError:
>     pass
> self._queue.append(key)
> 
> with the if key in self optimisation pushed down to __delitem__
> 
> -Rob
> 

That would optimize for the case where you are replacing items, rather
than optimizing for the case where add is called when the item is not
present.

It is hard to say what the most common case is, but I would guess that
most caches are only used for things that won't actually mutate, so I
would rather optimize for that case.

Or are you just trying to change the semantics of "del fifo[foo]" into
"fifo.discard(foo)" [which doesn't raise if foo doesn't exist]?

I'm willing to change it, but I think I have it written the correct way.

John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkk+9xsACgkQJdeBCYSNAAN0KgCeKEsp3Iksh1HJlFL+D+EorSWM
17IAoNM11/WxEJylPpswOEh+sU5mOwaB
=5PB8
-----END PGP SIGNATURE-----



More information about the bazaar mailing list