[antlr-interest] C runtime Memory Usage
Gavin Lambert
antlr at mirality.co.nz
Sat Jan 24 13:35:57 PST 2009
At 06:47 25/01/2009, Jim Idle wrote:
>It is a convenience function that creates (and tracks) a memory
>structure that copies the string from the input every time you
>call getText(). It is this way because (and I think this is in
>the docs) sometimes you want a new copy of the input string
>and sometimes you want to modify the string you get back but
>not modify the input.
I don't think that's a good argument. Strings in Java and C# are
immutable; in C/C++ they're not, but they should be treated as if
they were (reading a string is a far more common operation than
modifying one). So the getText() function should return a const
string and should only construct it once. (For performance
reasons, it should probably keep the existing behaviour of not
constructing the string until first requested.)
For another argument: it's trivial to take a read-only string and
convert it into a writable one (without affecting the
original). It's non-trivial to extract text from the token
stream. So the latter function should be implemented by the
runtime in such a way that the former can be applied afterwards in
the unlikely event that it's needed.
More information about the antlr-interest
mailing list