This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [PATCH] 100x optimization for ngettext
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, georg.brandl, michaelurman, piman
Priority: normal Keywords: patch

Created on 2005-11-06 00:18 by piman, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ngettext-opt.diff piman, 2005-11-06 00:18 Patch against Python 2.4's gettext.py
Messages (8)
msg48971 - (view) Author: Joe Wreschnig (piman) Date: 2005-11-06 00:18
Currently gettext.ngettext scans the filesystem
whenever you call it. This makes it ridiculously slow.
But if one uses gettext.install to initialize gettext,
gettext.ngettext is the cleanest way to do plural
translation.

This patch makes gettext.install install both "_" and
"ngettext" into the builtin namespace, which means
using "ngettext" won't have to do the filesystem
lookup. My benchmarks show this is about 100 times faster.
msg48972 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-11-06 19:21
Logged In: YES 
user_id=1188172

The problem you diagnosed is valid, but I don't think the
patch is a proper way to salvage it (by cluttering the
builtin namespace).

I'd rather have the installed name "_" point to a wrapper
object that gives one access to the other translation functions.
msg48973 - (view) Author: Joe Wreschnig (piman) Date: 2005-11-06 20:59
Logged In: YES 
user_id=796

It seems to me that the time to complain about namespace
clutter was when gettext.install was written. Besides, it's
only "clutter" if you're not using it, and if you're not
using it, just don't call install.

Making _ dispatch on the number of arguments would break all
the existing string extraction tools. It's also unintuitive
for anyone coming from gettext in other languages. It's also
messy semantic overloading.
msg48974 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-11-07 20:30
Logged In: YES 
user_id=1188172

I didn't mean to overload the calling of "_", but I can
think of "_.gettext()", "_.ngettext()" etc. Of course with
"_()" staying  the same.

Other than that, if you need ngettext in builtins, why don't
you write your own install function?
msg48975 - (view) Author: Joe Wreschnig (piman) Date: 2005-11-07 21:41
Logged In: YES 
user_id=796

Well, I did write my own install function; that's the patch
you see below. I figured I'd submit it in the interest of
other users (that's the point of a standard library, right?)
but if the Pythonic thing to do is choose between ugly or
slow, I guess I'll just keep using my own.
msg48976 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-01-24 19:09
Logged In: YES 
user_id=1188172

Are there opinions about my suggestion?
msg48977 - (view) Author: Michael Urman (michaelurman) Date: 2006-01-25 15:12
Logged In: YES 
user_id=1404983

To be overly blunt I think it's ludicrous. I doubt the
extraction tools would scan for _.ngettext() like they do
for ngettext() (I haven't tested). Also while the builtin
namespace is somewhat precious, those using gettext.install
need the performance and visual compatibility much more than
to avoid setting a builtin that's a fast version of the
global/local they would otherwise use.
msg48978 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-02-19 13:27
Logged In: YES 
user_id=1188172

I now changed the install() method to accept an optional
"names" parameter which can be used to install ngettext() too.

Revision 42492.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42557
2005-11-06 00:18:03pimancreate