Cache the result of locale.getpreferredencoding() on startup.
authorAdam Sampson <ats@offog.org>
Wed, 28 Jan 2009 12:20:47 +0000 (12:20 +0000)
committerAdam Sampson <ats@offog.org>
Wed, 28 Jan 2009 12:20:47 +0000 (12:20 +0000)
Profiler output suggests this is quite expensive, and we were calling it
a lot because it's used by safe_ftime.

NEWS
rawdoglib/rawdog.py

diff --git a/NEWS b/NEWS
index f3da16f34f583fb2c6e87a053ffb6c3d6fd1b731..dbb839e5c0666efe343b1c8196e0370ecdb45eab 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,8 @@
 - rawdog 2.12
 
+Cache the result of locale.getpreferredencoding(). This significantly
+speeds up writing output files.
+
 Update feedparser to revision 291, which fixes the handling of
 <media:title> elements (reported by Darren Griffith).
 
index 3dfd4821d3c3e847fdddefbef09f5841d7463acb..3dba503bec37f28b8a28fe843986487d139553fd 100644 (file)
@@ -40,13 +40,10 @@ def set_socket_timeout(n):
                import timeoutsocket
                timeoutsocket.setDefaultSocketTimeout(n)
 
+system_encoding = None
 def get_system_encoding():
        """Get the system encoding."""
-       try:
-               # This doesn't exist on Python 2.2.
-               return locale.getpreferredencoding()
-       except:
-               return "UTF-8"
+       return system_encoding
 
 def safe_ftime(format, t):
        """Format a time value into a string in the current locale (as
@@ -1586,6 +1583,15 @@ def main(argv):
 
        locale.setlocale(locale.LC_ALL, "")
 
+       global system_encoding
+       try:
+               # This doesn't exist on Python 2.2.
+               # It's also quite expensive, which is why we do it on startup
+               # and cache the result.
+               system_encoding = locale.getpreferredencoding()
+       except:
+               system_encoding = "UTF-8"
+
        try:
                (optlist, args) = getopt.getopt(argv, "ulwf:c:tTd:va:r:NW", ["update", "list", "write", "update-feed=", "help", "config=", "show-template", "dir=", "show-itemtemplate", "verbose", "upgrade", "add=", "remove=", "no-locking", "no-lock-wait"])
        except getopt.GetoptError, s: