[fpc-pascal] ShortString still relevant today?

Nikolay Nikolov nickysn at gmail.com
Tue Jul 4 05:11:11 CEST 2023


On 7/4/23 04:19, Hairy Pixels via fpc-pascal wrote:
> I've been exploring the string types and I'm curious now, does the classic Pascal "ShortString" even make sense anymore on modern computers? I'm running tests and I can't seem to find a way in which AnsiString overall performs worse than ShortString.
>
> Are there any examples where AnsiString is worse? I think if you passed strings around lots that would trigger the ref counting and InterlockedExchange (I saw this in my own code before and it unnerved me) but that's been hard to test.

ShortString is mainly for compatibility with Turbo Pascal, not for 
performance, IMHO. Although the FPC compiler itself still uses 
ShortString for performance reasons (I think the main advantage is the 
avoidance of the implicit try..finally blocks, needed for ansistrings). 
It might be interesting to benchmark the compiler with AnsiStrings 
instead of ShortStrings and see if there's a performance difference. But 
even if there is, a compiler is an extreme example. For 99% of the 
programs, performance impact of AnsiString is not an issue. I put {$H+} 
in almost all my new programs. I'd say that in 99% of the legit use 
cases, ShortString is used and needed for compatibility with legacy 
code, not for performance. Switching legacy code to {$H+} doesn't always 
work and may need additional fixes. Old code does things like S[0] := x 
instead of SetLength(S, x), etc. It also does uglier things, like 
FillChar() or Move() directly to/from string memory, or saves 
ShortStrings to files, as a part of a record, etc.

Nikolay



More information about the fpc-pascal mailing list