Skip to content

Commit

Permalink
Replace slow usage of gettext by faster one
Browse files Browse the repository at this point in the history
The Python docs suggest against the current usage.
In profiles it has shown that this is very slow as for every translation
request the translation file is searched and an exception thrown as none
is found.
The change caches the translation which would be (temporarily) created by _gettext (on every call) and reuses it.
If none is found a fallback (NullTranslation) is automatically used.
  • Loading branch information
Flamefire committed Feb 3, 2021
1 parent a123684 commit ee89195
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion easybuild/base/generaloption.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,30 @@
from functools import reduce
from optparse import Option, OptionGroup, OptionParser, OptionValueError, Values
from optparse import SUPPRESS_HELP as nohelp # supported in optparse of python v2.4
from optparse import gettext as _gettext # this is gettext.gettext normally

from easybuild.base.fancylogger import getLogger, setroot, setLogLevel, getDetailsLogLevels
from easybuild.base.optcomplete import autocomplete, CompleterOption
from easybuild.tools.py2vs3 import StringIO, configparser, string_type
from easybuild.tools.utilities import mk_rst_table, nub, shell_quote

try:
import gettext
eb_translation = None

def get_translation():
global eb_translation
if not eb_translation:
# Finding a translation is expensive, so do only once
domain = gettext.textdomain()
eb_translation = gettext.translation(domain, gettext.bindtextdomain(domain), fallback=True)
return eb_translation

def _gettext(message):
return get_translation().gettext(message)
except ImportError:
def _gettext(message):
return message


HELP_OUTPUT_FORMATS = ['', 'rst', 'short', 'config']

Expand Down

0 comments on commit ee89195

Please sign in to comment.