为什么使用 isEmpty 优于与空字符串文字的比较是 Swift?

Why is using isEmpty preferable over a comparison with an empty string literal is Swift?

String类型是Swift有一个属性名字叫isEmpty表示一个字符串是否没有字符

我想知道使用 isEmpty 和检查与空字符串文字的相等性是否有任何区别。换句话说,myString.isEmptymyString == "" 好吗?

我做了一些研究,发现了以下建议:

  1. String struct reference at Apple developer documentation (as well as the Swift Language Guide) 建议使用 isEmpty 而不是检查字符串的长度:

To check whether a string is empty, use its isEmpty property instead of comparing the length of one of the views to 0. Unlike with isEmpty, calculating a view’s count property requires iterating through the elements of the string.

  1. 2015 年 Rob Napier 在 Whosebug 上的陈述如下:

    The empty string is the only empty string, so there should be no cases where string.isEmpty() does not return the same value as string == "". They may do so in different amounts of time and memory, of course. Whether they use different amounts of time and memory is an implementation detail not described, but isEmpty is the preferred way to check in Swift (as documented).

  2. 一些博文还建议使用 isEmpty,尤其是不要检查字符串的长度是否为 0。

然而,

None 这些来源反对与空文字进行比较。

由于明显的性能和可读性缺陷,避免像 myString.count == 0 这样的结构似乎是完全合理的。我还了解到 myString.isEmptymyString == "".

更具可读性

不过,我很好奇与空字符串文字的比较是否不好。它真的对内存或性能有影响吗?也许 Swift 编译器现在非常聪明,它会为 myString.isEmptymyString == "" 生成相同的二进制代码?我的直觉是差异应该可以忽略不计,甚至不存在,但我没有证据。

我意识到这并不是一个真正的实际问题,但是如果有人可以分享一些见解这两种替代方案如何在较低级别上工作以及是否存在任何差异,我将不胜感激。提前谢谢大家。

请注意,isEmpty 是检查集合是否为空的 preferred/recommended 方法,因为所有 Collection 类型都保证 isEmpty returns在 O(1) 中(或者至少这适用于标准库集合)。相等运算符不做这样的保证,因此如果您只对包含或不包含元素的集合感兴趣(例如启动处理操作),那么 isEmpty 绝对是可行的方法。

现在,要了解使用 isEmpty 与比较空字符串时的幕后情况,我们可以使用生成的程序集。

func testEmpty(_ str: String) -> Bool { str.isEmpty }

产生以下汇编代码:

                     _$s3CL29testEmptyySbSSF:
0000000100002c70         push       rbp
0000000100002c71         mov        rbp, rsp
0000000100002c74         mov        rax, rsi
0000000100002c77         shr        rax, 0x38
0000000100002c7b         and        eax, 0xf
0000000100002c7e         movabs     rcx, 0xffffffffffff
0000000100002c88         and        rcx, rdi
0000000100002c8b         bt         rsi, 0x3d
0000000100002c90         cmovae     rax, rcx
0000000100002c94         test       rax, rax
0000000100002c97         sete       al
0000000100002c9a         pop        rbp
0000000100002c9b         ret        

func testEqual(_ str: String) -> Bool { str == "" }

生成这个:

                     _$s3CL29testEqualySbSSF:
0000000100002cd0         push       rbp
0000000100002cd1         mov        rbp, rsp
0000000100002cd4         movabs     rcx, 0xe000000000000000
0000000100002cde         test       rdi, rdi
0000000100002ce1         jne        0x100002cec

0000000100002ce3         cmp        rsi, rcx
0000000100002ce6         jne        0x100002cec

0000000100002ce8         mov        al, 0x1
0000000100002cea         pop        rbp
0000000100002ceb         ret        

0000000100002cec         xor        edx, edx                                    ; XREF=_$s3CL29testEqualySbSSF+17, _$s3CL29testEqualySbSSF+22
0000000100002cee         xor        r8d, r8d
0000000100002cf1         pop        rbp
0000000100002cf2         jmp        imp___stubs__$ss27_stringCompareWithSmolCheck__9expectingSbs11_StringGutsV_ADs01_G16ComparisonResultOtF
                        ; endp

两个程序集都是在发布模式下生成的,并启用了所有优化。似乎对于 isEmpty 调用,编译器能够采取一些快捷方式,因为它知道内部 String 结构。

但我们可以通过使我们的函数通用化来消除它:

func testEmpty<S: StringProtocol>(_ str: S) -> Bool { str.isEmpty }

产生

                     _$s3CL29testEmptyySbxSyRzlF:
0000000100002bd0         push       rbp
0000000100002bd1         mov        rbp, rsp
0000000100002bd4         push       r13
0000000100002bd6         push       rax
0000000100002bd7         mov        rax, rsi
0000000100002bda         mov        rcx, qword [ds:rdx+8]
0000000100002bde         mov        rsi, qword [ds:rcx+8]
0000000100002be2         mov        r13, rdi
0000000100002be5         mov        rdi, rax
0000000100002be8         call       imp___stubs__$sSl7isEmptySbvgTj
0000000100002bed         add        rsp, 0x8
0000000100002bf1         pop        r13
0000000100002bf3         pop        rbp
0000000100002bf4         ret        
                        ; endp

,而

func testEqual<S: StringProtocol>(_ str: S) -> Bool { str == "" }

产生

                     _$s3CL29testEqualySbxSyRzlF:
0000000100002c00         push       rbp
0000000100002c01         mov        rbp, rsp
0000000100002c04         push       r14
0000000100002c06         push       r13
0000000100002c08         push       rbx
0000000100002c09         sub        rsp, 0x18
0000000100002c0d         mov        r14, rdx
0000000100002c10         mov        r13, rsi
0000000100002c13         mov        rbx, rdi
0000000100002c16         mov        qword [ss:rbp+var_28], 0x0
0000000100002c1e         movabs     rax, 0xe000000000000000
0000000100002c28         mov        qword [ss:rbp+var_20], rax
0000000100002c2c         call       _$sS2SSysWl
0000000100002c31         mov        rcx, qword [ds:imp___got__$sSSN]
0000000100002c38         lea        rsi, qword [ss:rbp+var_28]
0000000100002c3c         mov        rdi, rbx
0000000100002c3f         mov        rdx, r13
0000000100002c42         mov        r8, r14
0000000100002c45         mov        r9, rax
0000000100002c48         call       imp___stubs__$sSysE2eeoiySbx_qd__tSyRd__lFZ
0000000100002c4d         add        rsp, 0x18
0000000100002c51         pop        rbx
0000000100002c52         pop        r13
0000000100002c54         pop        r14
0000000100002c56         pop        rbp
0000000100002c57         ret        
                        ; endp

类似的结果,isEmpty代码结果是汇编代码更少,速度更快。