From: Adam Sampson Date: Wed, 28 Jan 2009 12:20:47 +0000 (+0000) Subject: Cache the result of locale.getpreferredencoding() on startup. X-Git-Tag: v2.12rc2~2 X-Git-Url: http://git.ozo.com/?a=commitdiff_plain;h=37cd75cc21fa2ac4df87649ca59fb770819ad89f;p=rawdog%2F.git Cache the result of locale.getpreferredencoding() on startup. Profiler output suggests this is quite expensive, and we were calling it a lot because it's used by safe_ftime. --- diff --git a/NEWS b/NEWS index f3da16f..dbb839e 100644 --- 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 elements (reported by Darren Griffith). diff --git a/rawdoglib/rawdog.py b/rawdoglib/rawdog.py index 3dfd482..3dba503 100644 --- a/rawdoglib/rawdog.py +++ b/rawdoglib/rawdog.py @@ -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: