[Libreoffice] unicode string literals, "stack" strings etc.

Caolán McNamara caolanm at redhat.com
Fri Oct 29 01:49:06 PDT 2010


Just to throw out here some code to play with strings.

Taking unostrings.cxx there's some template cunning which generates
objects with the same layout as a OString, but without any constructors,
so that the StaticInit_OString macro generates OString-alike objects
which require effectively 0 setup time and can be shoved by the linker
into the rodata section.

16unistrings.cxx then extends this to add support for the "u" 16bit
string literals as found in c++0x (g++ --std=c++0x) and taking advantage
that wchar_t being 16bits in msvc the (though untested) "L" identifier
there. This would have the downside that the string literal storage in
the binary is now double the size it is now, but the upside that we
completely elide ::createFromAscii and/or the OUString constructor and
elide their destructors too of course.

Catch is though, that because OStrings and OUStrings do shallow copies,
copying one of these magic-strings about the place leaves it very
possible that a shallow-copy of the string will outlive the "stack
string" and die horribly. So to be safe the OString/OUString copy
methods would have to be tweaked to do a deep copy when one of these
shows up. But if the stack strings were only passed around by reference,
then they wouldn't need to get copied. A big plus I see here is the
potential to remove the very large number of string constructors which
are run during startup.

As an aside, an easy hack for small optimizations already is to replace
stuff like

rtl::OUString aFoo(rtl::OUString::createFromAscii("foo")); 
and
rtl::OUString aFoo = rtl::OUString::createFromAscii("foo");
with
rtl::OUString aFoo(RTL_CONSTASCII_USTRINGPARAM("foo"));

seeing as the final example is a titchy optimization over the other
examples, Added this as an easy hack to 
http://wiki.documentfoundation.org/Development/Easy_Hacks#Use_RTL_CONSTASCII_USTRINGPARAM_macro

C.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unostrings.cxx
Type: text/x-c++src
Size: 1627 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/libreoffice/attachments/20101029/2888f658/attachment.cxx>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 16unostrings.cxx
Type: text/x-c++src
Size: 2521 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/libreoffice/attachments/20101029/2888f658/attachment-0001.cxx>


More information about the LibreOffice mailing list