swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.57k stars 10.36k forks source link

[SR-4699] Penalty for even thin abstraction #47276

Open dabrahams opened 7 years ago

dabrahams commented 7 years ago
Previous ID SR-4699
Radar rdar://problem/31823073
Original Reporter @dabrahams
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, Performance | |Assignee | None | |Priority | Medium | md5: 8bf5b601c5998b28eb72814a42a69846

Issue Description:

$ git fetch https://github.com/dabrahams/swift refs/bugs/thin-abstraction-penalty:BUG && git checkout BUG

Note: this is very close to github/master, with no changes except in test/Prototypes/

build the compiler with the following command

/Users/dave/src/s/swift/utils/build-script --distcc --skip-build-ios-device --skip-build-tvos-device --skip-build-compiler-rt --release --no-assertions

Note: the difference described here is only a few percent

Then enter the test/Prototypes directory and execute:

set -e
swiftc -DBENCHMARK -DREVERSE -O -swift-version 4 UnicodeDecoders.swift -o /tmp/test
for i in {1..10}; do
   time nice -19 /tmp/test
done

recording the output.

Now do the same thing after checking out that commit's parent, 1ab347c66bc517c2e62aee56d8f569ebcf1a8fa7

I notice that the first run is reliably a few percent slower than the second. This really shouldn't be the case because the HEAD commit does nothing but wrap a UInt32 and a UInt8 into a struct with just about everything inlined. The properties are then re-exposed as computed and all computation proceeds exactly as before.

Other hashes

clang                              a0f8ca6108 Merge remote-tracking branch 'origin/swift-4.0-branch' into stable
cmark                              d875488 Merge pull request #​4 from llvm-beanz/generate-cmark-exports
compiler-rt                        b8fed2ead Merge remote-tracking branch 'origin/swift-4.0-branch' into stable
llbuild                            c47d2c4 [Xcode] Remove some dead project references.
lldb                               056379299 Merge branch 'stable' of ssh://github.com/apple/swift-lldb into stable
llvm                               eb15922c714 Merge remote-tracking branch 'origin/swift-4.0-branch' into stable
ninja                              586bb6d Merge pull request #​1267 from atetubou/clparser_perftest
swift                              1ab347c66b [stdlib] UnicodeDecoders: nix wrong forward/reverse relationship
swift-corelibs-foundation          e4cfd27e Implementation of XDG File specification and a basic implementation of HTTPCookieStorage (#889)
swift-corelibs-libdispatch         2df80a3 Merge pull request #​233 from amboar/master
swift-corelibs-xctest              5c702f7 Merge pull request #​188 from tkremenek/force-ver-3
swift-integration-tests            641971a Merge pull request #​21 from apple/revert-20-revert-19-revert-6a20dc2
swift-xcode-playground-support     3dfa22c Merge pull request #​13 from moiseev/new-integer-protocols
swiftpm                            ff9879d8 Merge pull request #​1098 from weissi/jw-cross-compilation
dabrahams commented 7 years ago

@swift-ci create

dabrahams commented 7 years ago

Turns out the comment below is bogus; it was avoiding work by shifting the buffer length out of its UInt8 and thus the buffer became magically empty. So feel free to delete this from the bug.

Even weirder: take that commit and add work by changing the getter/setter for _bitsInBuffer as follows:

  var _bitsInBuffer : UInt8 {
    get { return buffer._length >> 3 }
    set { buffer._length = newValue << 3 }
  }

Now I see across-the-board speedups for both test cases of approximately 17.5%:

.h3 Without Shifting

set -e
for x in FORWARD REVERSE ; do
    echo $x
    swiftc -DBENCHMARK -D$x -O -swift-version 4 UnicodeDecoders.swift -o /tmp/u3-$x 
    for i in {1..3}; do
        time nice -19 /tmp/u3-$x
    done
done
-----
FORWARD

real    0m2.643s
user    0m2.628s
sys 0m0.007s

real    0m2.643s
user    0m2.626s
sys 0m0.009s

real    0m2.640s
user    0m2.627s
sys 0m0.007s

REVERSE

real    0m2.413s
user    0m2.400s
sys 0m0.007s

real    0m2.412s
user    0m2.400s
sys 0m0.006s

real    0m2.420s
user    0m2.408s
sys 0m0.007s

.h3 With Shifting

set -e
for x in FORWARD REVERSE ; do
    echo $x
    swiftc -DBENCHMARK -D$x -O -swift-version 4 UnicodeDecoders.swift -o /tmp/u3-$x 
    for i in {1..3}; do
        time nice -19 /tmp/u3-$x
    done
done
-----
FORWARD

real    0m2.170s
user    0m2.156s
sys 0m0.006s

real    0m2.200s
user    0m2.183s
sys 0m0.008s

real    0m2.191s
user    0m2.174s
sys 0m0.009s

REVERSE

real    0m1.957s
user    0m1.943s
sys 0m0.006s

real    0m1.960s
user    0m1.949s
sys 0m0.007s

real    0m2.035s
user    0m2.008s
sys 0m0.011s