Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9436] Subscripting Dictionary with String keys generates 452 more bytes of TEXT (12x!) than in ObjC #51900

Open
aroben opened this issue Dec 7, 2018 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code size compiler The Swift compiler in itself standard library Area: Standard library umbrella

Comments

@aroben
Copy link

aroben commented Dec 7, 2018

Previous ID SR-9436
Radar rdar://problem/46534453
Original Reporter @aroben
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Bug, CodeSize
Assignee None
Priority Medium

md5: 7baa30d403a3b667c14bbf6d2d813714

Issue Description:

Subscripting Dictionary with String keys generates 348 more bytes of TEXT than in ObjC.

$ cat test.swift
@inline(never)
func blackHole<T>(_ x: T) {
}

@inline(never)
func testit(_ a: [String: Int], _ b: String) -> Int {
  return a[b] ?? 0
}

let a = [String: Int]()
blackHole(testit(a, "foo"))

$ cat test.m
#import <Foundation/Foundation.h>

__attribute__((noinline))
NSInteger testit(NSDictionary<NSString *, NSNumber *> *a, NSString *b) {
  return [a[b] integerValue];
}

int main(int argc, const char *argv[]) {
  NSMutableDictionary<NSString *, NSNumber *> *a = [NSMutableDictionary new];
  NSLog(@"%ld", (long)testit(a, @"foo"));
  [a release];
  return 0;
}

$ swiftc -Osize -whole-module-optimization -module-name test -o test test.swift
$ clang -framework Foundation -Os -o testobjc test.m

The testit method is 492 bytes in Swift vs. 40 bytes in ObjC.

ObjC:

_testit:
0000000100007e9c    stp x29, x30, [sp, #-0x10]!
0000000100007ea0    mov x29, sp
0000000100007ea4    mov x2, x1
0000000100007ea8    nop
0000000100007eac    ldr x1, #&#8203;0x1bc ; Objc selector ref: objectForKeyedSubscript:
0000000100007eb0    bl  0x100007f38 ; Objc message: -[x0 objectForKeyedSubscript:]
0000000100007eb4    nop
0000000100007eb8    ldr x1, #&#8203;0x1b8 ; Objc selector ref: integerValue
0000000100007ebc    ldp x29, x30, [sp], #&#8203;0x10
0000000100007ec0    b   0x100007f38 ; Objc message: -[x0 integerValue]

Swift:

test.testit([Swift.String : Swift.Int], Swift.String) -> Swift.Int:
0000000100007cf8    sub sp, sp, #&#8203;0xa0
0000000100007cfc    stp x26, x25, [sp, #&#8203;0x50]
0000000100007d00    stp x24, x23, [sp, #&#8203;0x60]
0000000100007d04    stp x22, x21, [sp, #&#8203;0x70]
0000000100007d08    stp x20, x19, [sp, #&#8203;0x80]
0000000100007d0c    stp x29, x30, [sp, #&#8203;0x90]
0000000100007d10    add x29, sp, #&#8203;0x90
0000000100007d14    ldr x8, [x0, #&#8203;0x10]
0000000100007d18    cbz x8, 0x100007ea4
0000000100007d1c    mov x21, x2
0000000100007d20    mov x22, x1
0000000100007d24    mov x19, x0
0000000100007d28    ldr x0, [x0, #&#8203;0x28]
0000000100007d2c    add x8, sp, #&#8203;0x8
0000000100007d30    bl  0x100007efc ; symbol stub for: Swift.Hasher.init(_seed: Swift.Int) -> Swift.Hasher
0000000100007d34    mov x0, x21
0000000100007d38    bl  0x100007f20 ; symbol stub for: _swift_bridgeObjectRetain
0000000100007d3c    mov x0, x19
0000000100007d40    bl  0x100007f20 ; symbol stub for: _swift_bridgeObjectRetain
0000000100007d44    add x0, sp, #&#8203;0x8
0000000100007d48    mov x1, x22
0000000100007d4c    mov x2, x21
0000000100007d50    bl  0x100007ee4 ; symbol stub for: Swift.String.hash(into: inout Swift.Hasher) -> ()
0000000100007d54    add x20, sp, #&#8203;0x8
0000000100007d58    bl  0x100007f08 ; symbol stub for: Swift.Hasher._finalize() -> Swift.Int
0000000100007d5c    add x20, x19, #&#8203;0x40
0000000100007d60    ldrb    w8, [x19, #&#8203;0x20]
0000000100007d64    mov x9, #-0x1
0000000100007d68    lsl x25, x9, x8
0000000100007d6c    bic x23, x0, x25
0000000100007d70    lsr x8, x23, #&#8203;3
0000000100007d74    and x8, x8, #&#8203;0xfffffffffffff8
0000000100007d78    ldr x8, [x20, x8]
0000000100007d7c    orr w9, wzr, #&#8203;0x1
0000000100007d80    lsl x9, x9, x23
0000000100007d84    tst x9, x8
0000000100007d88    b.eq    0x100007e94
0000000100007d8c    ldr x24, [x19, #&#8203;0x30]
0000000100007d90    add x8, x24, x23, lsl #&#8203;4
0000000100007d94    ldp x0, x1, [x8]
0000000100007d98    orr w4, wzr, #&#8203;0x1
0000000100007d9c    mov x2, x22
0000000100007da0    mov x3, x21
0000000100007da4    bl  0x100007ef0 ; symbol stub for: Swift._stringCompare(_: Swift._StringGuts, _: Swift._StringGuts, expecting: Swift._StringComparisonResult) -> Swift.Bool
0000000100007da8    tbnz    w0, #&#8203;0x0, 0x100007eac
0000000100007dac    mvn x25, x25
0000000100007db0    add x8, x23, #&#8203;0x1
0000000100007db4    and x23, x8, x25
0000000100007db8    lsr x8, x23, #&#8203;3
0000000100007dbc    and x8, x8, #&#8203;0xfffffffffffff8
0000000100007dc0    ldr x8, [x20, x8]
0000000100007dc4    orr w9, wzr, #&#8203;0x1
0000000100007dc8    lsl x9, x9, x23
0000000100007dcc    tst x8, x9
0000000100007dd0    b.eq    0x100007e94
0000000100007dd4    add x8, x24, x23, lsl #&#8203;4
0000000100007dd8    ldp x0, x1, [x8]
0000000100007ddc    orr w4, wzr, #&#8203;0x1
0000000100007de0    mov x2, x22
0000000100007de4    mov x3, x21
0000000100007de8    bl  0x100007ef0 ; symbol stub for: Swift._stringCompare(_: Swift._StringGuts, _: Swift._StringGuts, expecting: Swift._StringComparisonResult) -> Swift.Bool
0000000100007dec    tbnz    w0, #&#8203;0x0, 0x100007eac
0000000100007df0    add x8, x23, #&#8203;0x1
0000000100007df4    and x23, x8, x25
0000000100007df8    lsr x8, x23, #&#8203;3
0000000100007dfc    and x8, x8, #&#8203;0xfffffffffffff8
0000000100007e00    ldr x8, [x20, x8]
0000000100007e04    orr w9, wzr, #&#8203;0x1
0000000100007e08    lsl x9, x9, x23
0000000100007e0c    tst x8, x9
0000000100007e10    b.eq    0x100007e94
0000000100007e14    add x8, x24, x23, lsl #&#8203;4
0000000100007e18    ldp x0, x1, [x8]
0000000100007e1c    orr w4, wzr, #&#8203;0x1
0000000100007e20    mov x2, x22
0000000100007e24    mov x3, x21
0000000100007e28    bl  0x100007ef0 ; symbol stub for: Swift._stringCompare(_: Swift._StringGuts, _: Swift._StringGuts, expecting: Swift._StringComparisonResult) -> Swift.Bool
0000000100007e2c    tbnz    w0, #&#8203;0x0, 0x100007eac
0000000100007e30    add x8, x23, #&#8203;0x1
0000000100007e34    and x23, x8, x25
0000000100007e38    lsr x8, x23, #&#8203;3
0000000100007e3c    and x8, x8, #&#8203;0xfffffffffffff8
0000000100007e40    ldr x8, [x20, x8]
0000000100007e44    orr w9, wzr, #&#8203;0x1
0000000100007e48    lsl x9, x9, x23
0000000100007e4c    tst x8, x9
0000000100007e50    b.eq    0x100007e94
0000000100007e54    orr w26, wzr, #&#8203;0x1
0000000100007e58    add x8, x24, x23, lsl #&#8203;4
0000000100007e5c    ldp x0, x1, [x8]
0000000100007e60    orr w4, wzr, #&#8203;0x1
0000000100007e64    mov x2, x22
0000000100007e68    mov x3, x21
0000000100007e6c    bl  0x100007ef0 ; symbol stub for: Swift._stringCompare(_: Swift._StringGuts, _: Swift._StringGuts, expecting: Swift._StringComparisonResult) -> Swift.Bool
0000000100007e70    tbnz    w0, #&#8203;0x0, 0x100007eac
0000000100007e74    add x8, x23, #&#8203;0x1
0000000100007e78    and x23, x8, x25
0000000100007e7c    lsr x8, x23, #&#8203;3
0000000100007e80    and x8, x8, #&#8203;0xfffffffffffff8
0000000100007e84    ldr x8, [x20, x8]
0000000100007e88    lsl x9, x26, x23
0000000100007e8c    tst x8, x9
0000000100007e90    b.ne    0x100007e58
0000000100007e94    mov x0, x21
0000000100007e98    bl  0x100007f14 ; symbol stub for: _swift_bridgeObjectRelease
0000000100007e9c    mov x0, x19
0000000100007ea0    bl  0x100007f14 ; symbol stub for: _swift_bridgeObjectRelease
0000000100007ea4    mov x20, #&#8203;0x0
0000000100007ea8    b   0x100007ec4
0000000100007eac    ldr x8, [x19, #&#8203;0x38]
0000000100007eb0    ldr x20, [x8, x23, lsl #&#8203;3]
0000000100007eb4    mov x0, x21
0000000100007eb8    bl  0x100007f14 ; symbol stub for: _swift_bridgeObjectRelease
0000000100007ebc    mov x0, x19
0000000100007ec0    bl  0x100007f14 ; symbol stub for: _swift_bridgeObjectRelease
0000000100007ec4    mov x0, x20
0000000100007ec8    ldp x29, x30, [sp, #&#8203;0x90]
0000000100007ecc    ldp x20, x19, [sp, #&#8203;0x80]
0000000100007ed0    ldp x22, x21, [sp, #&#8203;0x70]
0000000100007ed4    ldp x24, x23, [sp, #&#8203;0x60]
0000000100007ed8    ldp x26, x25, [sp, #&#8203;0x50]
0000000100007edc    add sp, sp, #&#8203;0xa0
0000000100007ee0    ret

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code size compiler The Swift compiler in itself standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant