empty ansistring check with length() is more complicated
Original Reporter info from Mantis: kays @KaiBurghardt
-
Reporter name: Kai Burghardt
Original Reporter info from Mantis: kays @KaiBurghardt
- Reporter name: Kai Burghardt
Description:
I wondered, whether someString = '' or length(someString) = 0 is “better”. Turns out, only the former produces a cmp-vs.-zero instruction.
Steps to reproduce:
program emptyStringCheck(input, output, stdErr);
{$longStrings on}
var
someString: string;
begin
someString := '';
if length(someString) = 0 then
begin
writeLn('Great. Now I don’t know what to say.');
end;
// just for comparison:
if someString = '' then
begin
end;
end.
Additional information:
The length check becomes (on a x86 target):
# [7] if length(someString) = 0 then
movq U_$P$EMPTYSTRINGCHECK_$$_SOMESTRING,%rax
testq %rax,%rax
je .Lj13
movq -8(%rax),%rax
.Lj13:
testq %rax,%rax
First, we ensure someString isn’t a null pointer. OK.
But then it already becomes strange: We check again for zero (the testq after je .Lj13)?
I mean, I understand -8(%rax) dereferences the non-nil-pointer and retrieves the length field in front of the first character (which an ansistring variable points to).
But why is that so complicated?
Mantis conversion info:
- Mantis ID: 36060
- OS: GNU/Linux
- OS Build: 4.2.0
- Build: 3.0.4+dfsg-11 [2017/12/30]
- Platform: x86_64
- Version: 3.0.4
- Fixed in version: 3.3.1
- Fixed in revision: 43272 (#e80eee5d)
- Monitored by: » Vincent (Vincent Snijders)