On Sunday 25 November 2007 07:15:01 Patrick R. Michaud wrote: > What's the difference between CONST_STRING and string_from_literal? > In particular, which one should be used when? In some code I see > things like: > > # src/pmc/exporter.pmc > STRING *s_hash = CONST_STRING(interp, "hash"); > > and in others we have > > # src/pmc/codestring.pmc > STRING *percent = string_from_literal(INTERP, "%");
CONST_STRING() is 100% more awesome, because it's completely constant. The compiler creates a STRING at compile time and stuffs it in the constant data segment of the shared library. The runtime effect of that statement is dereferencing a pointer to get at that constant STRING. string_from_literal() has its literal string stored in the constant data segment, but it creates a new STRING structure at runtime each time it executes. As you might expect, this creates many many more GCable data structures than the other approach. The one drawback of CONST_STRING() is that it requires a little bit of source code preprocessing to create a .str file for each source code file which contains the macro, and I don't understand how or when that happens so I can't make it work everywhere. For what it's worth, I saw a nice performance improvement in a hot path in the PDD 15 branch after replacing string_from_literal() with CONST_STRING(). We should do this wherever possible. -- c