[ python-Bugs-1573394 ] struct module doesn't use weakref for cache

SourceForge.net noreply at sourceforge.net
Thu Oct 12 19:54:58 CEST 2006


Bugs item #1573394, was opened at 2006-10-08 19:37
Message generated for change (Comment added) made by etrepum
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1573394&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Mark Flacy (markaflacy)
Assigned to: Bob Ippolito (etrepum)
Summary: struct module doesn't use weakref for cache

Initial Comment:
At the moment, when you attempt to add your 101st
different Struct object to the cache, all the other 100
entries are tossed into the garbage.  That seems a
trifle odd.

The Struct cache would appear to be a perfect use for a
weakref dictionary.

----------------------------------------------------------------------

>Comment By: Bob Ippolito (etrepum)
Date: 2006-10-12 13:54

Message:
Logged In: YES 
user_id=139309

Yes, that code is different. You haven't shown that it's
better though. Using popitem probably doesn't have very good
cache effects.

----------------------------------------------------------------------

Comment By: Žiga Seilnacht (zseil)
Date: 2006-10-12 13:36

Message:
Logged In: YES 
user_id=1326842

I was thinking abot something like this:

Index: Lib/struct.py
===================================================================
--- Lib/struct.py	(revision 52316)
+++ Lib/struct.py	(working copy)
@@ -35,7 +35,7 @@
 def _compile(fmt):
     # Internal: compile struct pattern
     if len(_cache) >= _MAXCACHE:
-        _cache.clear()
+        _cache.popitem()
     s = Struct(fmt)
     _cache[fmt] = s
     return s

(sorry, I don't have the rights to attach a file)


----------------------------------------------------------------------

Comment By: Bob Ippolito (etrepum)
Date: 2006-10-12 12:56

Message:
Logged In: YES 
user_id=139309

Weakrefs would slow it down.. probably to the point where
the cache wouldn't be an improvement at all.

The re module does the same thing with the regular
expression cache. This isn't a bug until someone presents a
patch that proves another strategy is better for performance.

----------------------------------------------------------------------

Comment By: Žiga Seilnacht (zseil)
Date: 2006-10-12 11:52

Message:
Logged In: YES 
user_id=1326842

WeakValueDictionary would be useless here; the cache is
the only thing that holds references to Struct objects.
Replacing the _cache.clear() call with _cache.popitem()
seems like a better solution.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1573394&group_id=5470


More information about the Python-bugs-list mailing list