Speeding up i18n

Thiago Macieira thiago at kde.org
Tue Feb 3 16:33:35 GMT 2009


Em Terça-feira 03 Fevereiro 2009, às 11:42:11, Chusslove Illich escreveu:
> > [: John Tapsell :]
> > Has anyone looked into speeding up i18n calls by generating the string
> > hash at compile time rather than at runtime.
>
> Do you have a real-life situation where they are too slow?
>
> For an average message (~40 chars, no placeholders) i18n can now deliver
> ~50 kmsg/s (kmsg/s = 1000 messages per second) on my 2 GHz AMD Athlon XP
> machine (average machine in 2004).
>
> If that is not good enough, then it's certainly not hashing that should be
> improved, as raw Gettext call (which does the hashing) delivers >1000
> kmsg/s for the same message/machine (no, no extra zero here).

Remember that gettext "cheats":

The gettext function is actually a macro that, when expanded, checks if the 
string is constant (builtin_constant) and, if so, stores the result in a 
static variable. As long as the gettext generation (an integer indicating 
whether languages or catalogue configurations have changed) is the same, it 
will deliver the same message at all times, without going through the hash-
lookup-fetch phase.

However, we don't return const char*. We return QString, which means it can't 
be stored in a static variable. We could try something like:

inline QString i18n(const char *source)
{
#ifdef Q_CC_GNU
	return reali18n(source, builtin_constant(source));
#else
	return reali18n(source, false);
#endif
}

and when reali18n receives a "true" for the second parameter, it stores the 
result in a global, mutex-protected:
	QHash<const char *, QString> knownTranslations;

The hashing of a pointer is much faster than the hashing of a string (on 32-
bit archs, it's just "return ptr"), so only the QHash bucket-search will 
appear as slowdown factor.

Another trick would be:

#define i18n(source)	\
	({ static char generation = 0; \
	static QString cache = reali18n(source, &generation); \
	if (generation != KLocale::currentGeneration) \
		cache = reali18n(source, &generation); \
	return cache; })

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Software
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20090203/05cee2e8/attachment.sig>


More information about the kde-core-devel mailing list