From 8df71f820a64780e156febef216600c181123961 Mon Sep 17 00:00:00 2001 From: Daniel Worku Date: Fri, 4 Dec 2015 01:25:36 -0600 Subject: [PATCH 1/7] Replace let/nil-check patterns with `guard let` The let/nil-check pattern is more expensive and less elegant than `guard let`. --- stdlib/public/core/ArrayBuffer.swift | 41 +++++++++++++--------------- stdlib/public/core/Collection.swift | 12 ++++---- stdlib/public/core/Map.swift | 7 ++--- stdlib/public/core/Runtime.swift.gyb | 9 +++--- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/stdlib/public/core/ArrayBuffer.swift b/stdlib/public/core/ArrayBuffer.swift index 1f7426cf1878e..ebe52c70930eb 100644 --- a/stdlib/public/core/ArrayBuffer.swift +++ b/stdlib/public/core/ArrayBuffer.swift @@ -247,29 +247,26 @@ extension _ArrayBuffer { // Look for contiguous storage in the NSArray let nonNative = self._nonNative let cocoa = _CocoaArrayWrapper(nonNative) - let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) - - if cocoaStorageBaseAddress != nil { - return _SliceBuffer( - owner: nonNative, - subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), - indices: subRange, - hasNativeBuffer: false) + guard let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) else { + // No contiguous storage found; we must allocate + let subRangeCount = subRange.count + let result = _ContiguousArrayBuffer( + count: subRangeCount, minimumCapacity: 0) + + // Tell Cocoa to copy the objects into our storage + cocoa.buffer.getObjects( + UnsafeMutablePointer(result.firstElementAddress), + range: _SwiftNSRange( + location: subRange.startIndex, + length: subRangeCount)) + + return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex) } - - // No contiguous storage found; we must allocate - let subRangeCount = subRange.count - let result = _ContiguousArrayBuffer( - count: subRangeCount, minimumCapacity: 0) - - // Tell Cocoa to copy the objects into our storage - cocoa.buffer.getObjects( - UnsafeMutablePointer(result.firstElementAddress), - range: _SwiftNSRange( - location: subRange.startIndex, - length: subRangeCount)) - - return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex) + return _SliceBuffer( + owner: nonNative, + subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), + indices: subRange, + hasNativeBuffer: false) } set { fatalError("not implemented") diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index ecee5d30fe368..4cd9731890753 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -584,19 +584,17 @@ extension SequenceType // A fast implementation for when you are backed by a contiguous array. public func _initializeTo(ptr: UnsafeMutablePointer) -> UnsafeMutablePointer { - let s = self._baseAddressIfContiguous - if s != nil { - let count = self.count - ptr.initializeFrom(s, count: count) - _fixLifetime(self._owner) - return ptr + count - } else { + guard let s = self._baseAddressIfContiguous else { var p = ptr for x in self { p++.initialize(x) } return p } + let count = self.count + ptr.initializeFrom(s, count: count) + _fixLifetime(self._owner) + return ptr + count } } diff --git a/stdlib/public/core/Map.swift b/stdlib/public/core/Map.swift index db885411e7f1a..2ade3f080e4e6 100644 --- a/stdlib/public/core/Map.swift +++ b/stdlib/public/core/Map.swift @@ -26,11 +26,10 @@ public struct LazyMapGenerator< /// since the copy was made, and no preceding call to `self.next()` /// has returned `nil`. public mutating func next() -> Element? { - let x = _base.next() - if x != nil { - return _transform(x!) + guard let x = _base.next() else { + return nil } - return nil + return _transform(x) } public var base: Base { return _base } diff --git a/stdlib/public/core/Runtime.swift.gyb b/stdlib/public/core/Runtime.swift.gyb index 8674560244cfa..89ac1d56a0d28 100644 --- a/stdlib/public/core/Runtime.swift.gyb +++ b/stdlib/public/core/Runtime.swift.gyb @@ -247,12 +247,11 @@ public // @testable func _stdlib_atomicLoadARCRef( object target: UnsafeMutablePointer ) -> AnyObject? { - let result = _swift_stdlib_atomicLoadPtrImpl( - object: UnsafeMutablePointer(target)) - if result != nil { - return Unmanaged.fromOpaque(result).takeUnretainedValue() + guard let result = _swift_stdlib_atomicLoadPtrImpl( + object: UnsafeMutablePointer(target)) { + return nil } - return nil + return Unmanaged.fromOpaque(result).takeUnretainedValue() } % for operation in [ 'Add', 'And', 'Or', 'Xor' ]: From 4b4c4dcc2c1e23fc0d396c5b86e2973b432fb705 Mon Sep 17 00:00:00 2001 From: dbworku Date: Fri, 4 Dec 2015 10:48:02 -0600 Subject: [PATCH 2/7] Use `if let` for non-trivial branches --- stdlib/public/core/ArrayBuffer.swift | 16 +++++++++------- stdlib/public/core/Collection.swift | 21 +++++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/stdlib/public/core/ArrayBuffer.swift b/stdlib/public/core/ArrayBuffer.swift index ebe52c70930eb..9ad723f948180 100644 --- a/stdlib/public/core/ArrayBuffer.swift +++ b/stdlib/public/core/ArrayBuffer.swift @@ -244,10 +244,17 @@ extension _ArrayBuffer { return _native[subRange] } - // Look for contiguous storage in the NSArray let nonNative = self._nonNative let cocoa = _CocoaArrayWrapper(nonNative) - guard let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) else { + + // Look for contiguous storage in the NSArray + if let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) { + return _SliceBuffer( + owner: nonNative, + subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), + indices: subRange, + hasNativeBuffer: false) + } else { // No contiguous storage found; we must allocate let subRangeCount = subRange.count let result = _ContiguousArrayBuffer( @@ -262,11 +269,6 @@ extension _ArrayBuffer { return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex) } - return _SliceBuffer( - owner: nonNative, - subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), - indices: subRange, - hasNativeBuffer: false) } set { fatalError("not implemented") diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 4cd9731890753..6d342debdf1bd 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -584,17 +584,18 @@ extension SequenceType // A fast implementation for when you are backed by a contiguous array. public func _initializeTo(ptr: UnsafeMutablePointer) -> UnsafeMutablePointer { - guard let s = self._baseAddressIfContiguous else { - var p = ptr - for x in self { - p++.initialize(x) + if let s = self._baseAddressIfContiguous { + let count = self.count + ptr.initializeFrom(s, count: count) + _fixLifetime(self._owner) + return ptr + count + } else { + var p = ptr + for x in self { + p++.initialize(x) + } + return p } - return p - } - let count = self.count - ptr.initializeFrom(s, count: count) - _fixLifetime(self._owner) - return ptr + count } } From e02ca762755e6fd22e8c642982948c725a2c5e38 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Fri, 4 Dec 2015 14:06:18 +0100 Subject: [PATCH 3/7] Don't rely on cmark internals The cmark_node struct and the node.h header are private. --- lib/Markup/Markup.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/Markup/Markup.cpp b/lib/Markup/Markup.cpp index 985e526116191..b49e2a5b98ad1 100644 --- a/lib/Markup/Markup.cpp +++ b/lib/Markup/Markup.cpp @@ -16,7 +16,6 @@ #include "swift/Markup/LineList.h" #include "swift/Markup/Markup.h" #include "cmark.h" -#include "node.h" using namespace llvm; using namespace markup; @@ -60,15 +59,7 @@ StringRef getLiteralContent(MarkupContext &MC, LineList &LL, cmark_node *Node) { // its parent. auto Literal = cmark_node_get_literal(Node); assert(Literal != nullptr); - size_t Length = 0; - switch (cmark_node_get_type(Node)) { - case CMARK_NODE_CODE_BLOCK: - Length = Node->as.code.literal.len; - break; - default: - Length = Node->as.literal.len; - } - return MC.allocateCopy(StringRef(Literal, Length)); + return MC.allocateCopy(StringRef(Literal)); } ParseResult From 40584f830abf06f1ffd542a8d755663619c389f9 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Fri, 4 Dec 2015 18:36:04 -0800 Subject: [PATCH 4/7] [test] Add a Radar to the tests I disabled in f869e9e6. I filed this under Radar rather than bugs.swift.org because it's /only/ our CI system that's failing, and external contributors have no insight into that anyway. Hopefully I'll be able to get to the bottom of this soon. --- test/Driver/environment.swift | 1 + test/Driver/options-interpreter.swift | 1 + 2 files changed, 2 insertions(+) diff --git a/test/Driver/environment.swift b/test/Driver/environment.swift index 04938cf1a5226..b891e1a6a07da 100644 --- a/test/Driver/environment.swift +++ b/test/Driver/environment.swift @@ -1,4 +1,5 @@ // FIXME: This is failing on some of Apple's internal CI. +// FIXME: Fix test/Driver/{environment.swift,options-interpreter.swift} // REQUIRES: disabled // RUN: %swift_driver -target x86_64-apple-macosx10.9 -L/foo/ -driver-use-frontend-path %S/Inputs/print-var.sh %s DYLD_LIBRARY_PATH | FileCheck %s diff --git a/test/Driver/options-interpreter.swift b/test/Driver/options-interpreter.swift index 690c7e8f3cde6..d7cd351b40b50 100644 --- a/test/Driver/options-interpreter.swift +++ b/test/Driver/options-interpreter.swift @@ -1,4 +1,5 @@ // FIXME: This is failing on some of Apple's internal CI. +// FIXME: Fix test/Driver/{environment.swift,options-interpreter.swift} // REQUIRES: disabled // RUN: not %swift_driver -deprecated-integrated-repl -emit-module 2>&1 | FileCheck -check-prefix=IMMEDIATE_NO_MODULE %s From 76eb96e29ea7bff09c5348eebfc7b82d0ca1f151 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 4 Dec 2015 18:40:46 -0800 Subject: [PATCH 5/7] Runtime: Reinstate _stdlib_demangleName hook. This is needed for Xcode support, even though it appeared dead within the Swift repo itself. --- stdlib/public/core/Misc.swift | 24 ++++++++++++++++++++++++ stdlib/public/runtime/Reflection.mm | 14 ++++++++++++++ test/1_stdlib/Runtime.swift | 10 ++++++++++ 3 files changed, 48 insertions(+) diff --git a/stdlib/public/core/Misc.swift b/stdlib/public/core/Misc.swift index 51ad4203b7064..6a2eb92828522 100644 --- a/stdlib/public/core/Misc.swift +++ b/stdlib/public/core/Misc.swift @@ -83,6 +83,30 @@ func _typeName(type: Any.Type, qualified: Bool = true) -> String { input: UnsafeBufferPointer(start: stringPtr, count: count)) } +@warn_unused_result +@_silgen_name("swift_stdlib_demangleName") +func _stdlib_demangleNameImpl( + mangledName: UnsafePointer, + _ mangledNameLength: UInt, + _ demangledName: UnsafeMutablePointer) + +// NB: This function is not used directly in the Swift codebase, but is +// exported for Xcode support. Please coordinate before changing. +@warn_unused_result +public // @testable +func _stdlib_demangleName(mangledName: String) -> String { + let mangledNameUTF8 = Array(mangledName.utf8) + return mangledNameUTF8.withUnsafeBufferPointer { + (mangledNameUTF8) in + let (_, demangledName) = _withUninitializedString { + _stdlib_demangleNameImpl( + mangledNameUTF8.baseAddress, UInt(mangledNameUTF8.endIndex), + $0) + } + return demangledName + } +} + /// Returns `floor(log(x))`. This equals to the position of the most /// significant non-zero bit, or 63 - number-of-zeros before it. /// diff --git a/stdlib/public/runtime/Reflection.mm b/stdlib/public/runtime/Reflection.mm index 245a0938cf2f4..dcc722a6f4bb6 100644 --- a/stdlib/public/runtime/Reflection.mm +++ b/stdlib/public/runtime/Reflection.mm @@ -1264,3 +1264,17 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup, T->vw_destroy(value); return MirrorReturn(result); } + +// NB: This function is not used directly in the Swift codebase, but is +// exported for Xcode support. Please coordinate before changing. +extern "C" void swift_stdlib_demangleName(const char *mangledName, + size_t mangledNameLength, + String *demangledName) { + auto options = Demangle::DemangleOptions(); + options.DisplayDebuggerGeneratedModule = false; + auto result = + Demangle::demangleSymbolAsString(mangledName, + mangledNameLength, + options); + new (demangledName) String(result.data(), result.size()); +} diff --git a/test/1_stdlib/Runtime.swift b/test/1_stdlib/Runtime.swift index 3c4dfd450ce8d..2c78916f78581 100644 --- a/test/1_stdlib/Runtime.swift +++ b/test/1_stdlib/Runtime.swift @@ -665,6 +665,16 @@ Runtime.test("typeName") { expectEqual("protocol<>.Protocol", _typeName(a.dynamicType)) } +Runtime.test("demangleName") { + expectEqual("", _stdlib_demangleName("")) + expectEqual("abc", _stdlib_demangleName("abc")) + expectEqual("\0", _stdlib_demangleName("\0")) + expectEqual("Swift.Double", _stdlib_demangleName("_TtSd")) + expectEqual("x.a : x.Foo, x.Foo>, x.Foo, x.Foo>>", + _stdlib_demangleName("_Tv1x1aGCS_3FooGS0_GS0_SiSi_GS0_SiSi__GS0_GS0_SiSi_GS0_SiSi___")) + expectEqual("Foobar", _stdlib_demangleName("_TtC13__lldb_expr_46Foobar")) +} + Runtime.test("_stdlib_atomicCompareExchangeStrongPtr") { typealias IntPtr = UnsafeMutablePointer var origP1 = IntPtr(bitPattern: 0x10101010) From ffc60a2899806d32e7763296fdd3d72a3fe54f47 Mon Sep 17 00:00:00 2001 From: Daniel Worku Date: Fri, 4 Dec 2015 01:25:36 -0600 Subject: [PATCH 6/7] Replace let/nil-check patterns with `guard let` The let/nil-check pattern is more expensive and less elegant than `guard let`. --- stdlib/public/core/ArrayBuffer.swift | 41 +++++++++++++--------------- stdlib/public/core/Collection.swift | 12 ++++---- stdlib/public/core/Runtime.swift.gyb | 9 +++--- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/stdlib/public/core/ArrayBuffer.swift b/stdlib/public/core/ArrayBuffer.swift index 1f7426cf1878e..ebe52c70930eb 100644 --- a/stdlib/public/core/ArrayBuffer.swift +++ b/stdlib/public/core/ArrayBuffer.swift @@ -247,29 +247,26 @@ extension _ArrayBuffer { // Look for contiguous storage in the NSArray let nonNative = self._nonNative let cocoa = _CocoaArrayWrapper(nonNative) - let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) - - if cocoaStorageBaseAddress != nil { - return _SliceBuffer( - owner: nonNative, - subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), - indices: subRange, - hasNativeBuffer: false) + guard let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) else { + // No contiguous storage found; we must allocate + let subRangeCount = subRange.count + let result = _ContiguousArrayBuffer( + count: subRangeCount, minimumCapacity: 0) + + // Tell Cocoa to copy the objects into our storage + cocoa.buffer.getObjects( + UnsafeMutablePointer(result.firstElementAddress), + range: _SwiftNSRange( + location: subRange.startIndex, + length: subRangeCount)) + + return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex) } - - // No contiguous storage found; we must allocate - let subRangeCount = subRange.count - let result = _ContiguousArrayBuffer( - count: subRangeCount, minimumCapacity: 0) - - // Tell Cocoa to copy the objects into our storage - cocoa.buffer.getObjects( - UnsafeMutablePointer(result.firstElementAddress), - range: _SwiftNSRange( - location: subRange.startIndex, - length: subRangeCount)) - - return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex) + return _SliceBuffer( + owner: nonNative, + subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), + indices: subRange, + hasNativeBuffer: false) } set { fatalError("not implemented") diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index ecee5d30fe368..4cd9731890753 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -584,19 +584,17 @@ extension SequenceType // A fast implementation for when you are backed by a contiguous array. public func _initializeTo(ptr: UnsafeMutablePointer) -> UnsafeMutablePointer { - let s = self._baseAddressIfContiguous - if s != nil { - let count = self.count - ptr.initializeFrom(s, count: count) - _fixLifetime(self._owner) - return ptr + count - } else { + guard let s = self._baseAddressIfContiguous else { var p = ptr for x in self { p++.initialize(x) } return p } + let count = self.count + ptr.initializeFrom(s, count: count) + _fixLifetime(self._owner) + return ptr + count } } diff --git a/stdlib/public/core/Runtime.swift.gyb b/stdlib/public/core/Runtime.swift.gyb index 8674560244cfa..89ac1d56a0d28 100644 --- a/stdlib/public/core/Runtime.swift.gyb +++ b/stdlib/public/core/Runtime.swift.gyb @@ -247,12 +247,11 @@ public // @testable func _stdlib_atomicLoadARCRef( object target: UnsafeMutablePointer ) -> AnyObject? { - let result = _swift_stdlib_atomicLoadPtrImpl( - object: UnsafeMutablePointer(target)) - if result != nil { - return Unmanaged.fromOpaque(result).takeUnretainedValue() + guard let result = _swift_stdlib_atomicLoadPtrImpl( + object: UnsafeMutablePointer(target)) { + return nil } - return nil + return Unmanaged.fromOpaque(result).takeUnretainedValue() } % for operation in [ 'Add', 'And', 'Or', 'Xor' ]: From ecc94f28d0b3a7b52f8635c5ab28a93f726a0035 Mon Sep 17 00:00:00 2001 From: dbworku Date: Fri, 4 Dec 2015 10:48:02 -0600 Subject: [PATCH 7/7] Use `if let` for non-trivial branches --- stdlib/public/core/ArrayBuffer.swift | 16 +++++++++------- stdlib/public/core/Collection.swift | 21 +++++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/stdlib/public/core/ArrayBuffer.swift b/stdlib/public/core/ArrayBuffer.swift index ebe52c70930eb..9ad723f948180 100644 --- a/stdlib/public/core/ArrayBuffer.swift +++ b/stdlib/public/core/ArrayBuffer.swift @@ -244,10 +244,17 @@ extension _ArrayBuffer { return _native[subRange] } - // Look for contiguous storage in the NSArray let nonNative = self._nonNative let cocoa = _CocoaArrayWrapper(nonNative) - guard let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) else { + + // Look for contiguous storage in the NSArray + if let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) { + return _SliceBuffer( + owner: nonNative, + subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), + indices: subRange, + hasNativeBuffer: false) + } else { // No contiguous storage found; we must allocate let subRangeCount = subRange.count let result = _ContiguousArrayBuffer( @@ -262,11 +269,6 @@ extension _ArrayBuffer { return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex) } - return _SliceBuffer( - owner: nonNative, - subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress), - indices: subRange, - hasNativeBuffer: false) } set { fatalError("not implemented") diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 4cd9731890753..6d342debdf1bd 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -584,17 +584,18 @@ extension SequenceType // A fast implementation for when you are backed by a contiguous array. public func _initializeTo(ptr: UnsafeMutablePointer) -> UnsafeMutablePointer { - guard let s = self._baseAddressIfContiguous else { - var p = ptr - for x in self { - p++.initialize(x) + if let s = self._baseAddressIfContiguous { + let count = self.count + ptr.initializeFrom(s, count: count) + _fixLifetime(self._owner) + return ptr + count + } else { + var p = ptr + for x in self { + p++.initialize(x) + } + return p } - return p - } - let count = self.count - ptr.initializeFrom(s, count: count) - _fixLifetime(self._owner) - return ptr + count } }