[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]cscc::optimisation::literals

From: Rhys Weatherley
Subject: Re: [DotGNU]cscc::optimisation::literals
Date: Fri, 22 Mar 2002 13:04:21 +1000

S11001001 wrote:

> Change all string literals to static class members. Why? Because if you are
> going to call the function multiple times and it instantiates the String every
> time, that's a waste. If you only call it once, then it's no big deal.

Err ... this isn't necessary.  String literals of the same
value all end up being hashed to the same object by
the runtime regardless.

The first time you call the function, and while it is
being converted to CVM bytecode, it will hash the
literal and find the object.  This object reference is
then embedded into the code.  Every subsequent
call to the method will use the object directly,
with no extra overhead.

> Just an idea based on my growing habit of declaring
> string literals as private static const members.

Which won't actually compile, because "static" cannot
be used with "const".  The correct way to declare a
private constant is just "private const".

Another approach is "private static readonly", which
creates a real field that must be dereferenced every
time.  This should be used where the value of a public
constant may change between program versions, and
you want to ensure that other classes pick up the
right one.  In the case of private constants, there is
no need to do this, and the compiler/runtime will run
better if you try not to out-fox them.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]