optimizations

Karl Chen <[email protected]>
Newsgroups gmane.comp.gnu.aspell.devel
Message-ID <[email protected]>
>>>>> "Kevin" == Kevin Atkinson <[email protected]> writes:
    >> Gcc 3.3 requires constants (empty_str) to be initialized.
    >> (BTW, you could probably make it static const or just write
    >> the value instead of empty_str)
    Kevin> 
    Kevin> Its a minor memory optimization.  The idea is to avoid
    Kevin> having to dereference a distant memory location just to
    Kevin> discover that it is the empty string.

Are you sure this is really worth it?  (I'm asking, not being rude.)

It seems like a very premature optimization (trying to make sure
that the string contents are in L1 cache).  For example, if you
wanted to trade simplicity for acute optimizations you could use
NULL instead of "" to indicate empty strings to avoid the
memory-load altogether when reading the value.

Since gcc pools equal string constants together, there's only one
memory location to read; other string constants are likely to be
nearby also.  So if you dereference the value of "" a lot then
it's likely to already be in L1 cache anyway.

For writing the value your optimization is actually worse;
consider this code:

extern void bar(char const*);

struct C {
    const char* s;
    const char z[1];
    C() : z() {}

    int foo1();
    int foo2();

    int quux1();
    int quux2();
};

int C::foo1() { bar(""); return 1;}
int C::foo2() { bar(z); return 1;}

int C::quux1() { s = ""; return 1;}
int C::quux2() { s = z; return 1;}


Compiled with: g++-3.3 -March=i686 -S -O2 a.cc


bar(""):
	movl	$.LC0, (%esp)
	call	_Z3barPKc

bar(z):
	movl	8(%ebp), %eax
	addl	$4, %eax
	movl	%eax, (%esp)
	call	_Z3barPKc

s = "":
	movl	8(%ebp), %eax
	movl	$.LC0, (%eax)

s = z:
	movl	8(%ebp), %edx
	leal	4(%edx), %eax
	movl	%eax, (%edx)
	movl	$1, %eax


P.S. I think either way the effort is wasted doing this kind of
optimization in a spell-checker.  Since it sounds from the web
page you're strapped for time/energy, I think it's more important
to work on featureset, bugs, etc. than rewriting/optimizing STL.


-- 
Karl 2004-04-24 13:10
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.