Commit Graph

136 Commits

Author SHA1 Message Date
Hassan
1d4f220ed4 [stdlib] Replace precondition with the internal _precondition 2021-11-04 23:51:10 +02:00
Karoy Lorentey
8ed81ae063 [stdlib] Adopt availability macros 2021-10-31 15:00:58 -07:00
David Smith
30a3b6d3f5 Expose __SwiftNativeNSString for Foundation's use, as well as an initializer to create Strings from them 2021-09-10 01:26:11 -07:00
David Smith
834e82b7da Avoid an unnecessary call to CFStringGetCharactersPtr in bridging 2021-08-20 00:40:54 -07:00
Robert Widmann
0149ccd0ca Add arm64_32 support for Swift
Commit the platform definition and build script work necessary to
cross-compile for arm64_32.

arm64_32 is a variant of AARCH64 that supports an ILP32 architecture.
2021-04-20 14:59:04 -07:00
David Smith
e793e0e0e4 Always ask Cocoa for terminated char*s
(cherry picked from commit b7ce22ba1525dccd47f4095a9bf8a640b238a5fa)
2021-03-19 13:01:23 -07:00
Andrew Trick
5eafc20cdd Fix undefined behavior in SmallString.withUTF8
withUTF8 currently vends a typed UInt8 pointer to the underlying
SmallString. That pointer type differs from SmallString's
representation. It should simply vend a raw pointer, which would be
both type safe and convenient for UTF8 data. However, since this
method is already @inlinable, I added calls to bindMemory to prevent
the optimizer from reasoning about access to the typed pointer that we
vend.

rdar://67983613 (Undefinied behavior in SmallString.withUTF8 is miscompiled)

Additional commentary:

SmallString creates a situation where there are two types, the
in-memory type, (UInt64, UInt64), vs. the element type,
UInt8. `UnsafePointer<T>` specifies the in-memory type of the pointee,
because that's how C works. If you want to specify an element type,
not the in-memory type, then you need to use something other than
UnsafePointer to view the memory. A trivial `BufferView<UInt8>` would
be fine, although, frankly, I think UnsafeRawPointer is a perfectly
good type on its own for UTF8 bytes.

Unfortunately, a lot of the UTF8 helper code is ABI-exposed, so to
work around this, we need to insert calls to bindMemory at strategic
points to avoid undefined behavior. This is high-risk and can
negatively affect performance. So far, I was able to resolve the
regressions in our microbenchmarks just by tweaking the inliner.
2020-09-24 18:36:42 -07:00
Károly Lőrentey
c579759aa7 [stdlib] Use a direct initializer for typed to raw pointer conversion 2020-06-29 19:16:23 -07:00
David Smith
220f0ccde8 Untag tagged constant NSString pointers before we wrap them, so BridgeObject doesn't stomp the high bits 2020-04-22 16:49:23 -07:00
David Smith
5ad4b15af6 Avoid attempting to create SmallStrings for constant tagged CFStrings 2020-04-15 12:30:20 -07:00
Mishal Shah
e7cd5ab17f Update master to build with Xcode 11.4 2020-03-24 11:30:45 -07:00
Michael Ilseman
0ca42e9ef7 [string] Shrink storage class sizes.
* Don't allocate breadrumbs pointer if under threshold
* Increase breadrumbs threshold
* Linear 16-byte bucketing until 128 bytes, malloc_size after
* Allow cap less than _SmallString.capacity (bridging non-ASCII)

This change decreases the amount of heap usage for moderate-length
strings (< 64 UTF-8 code units in length) and increases the amount of
spare code unit capacity available (less growth needed).

Average improvements for moderate-length strings:

* 64-bit: on average, 8 bytes saved and 4 bytes of extra capacity
* 32-bit: on average, 4 bytes saved and 6 bytes of extra capacity

Additionally, on 32-bit, large-length strings also gain an average of
6 bytes of extra spare capacity.

Details:

On 64-bit, half of moderate-length allocations will save 16 bytes
while the other half get an extra 8 bytes of spare capacity.

On 32-bit, a quarter of moderate-length allocations will save 16
bytes, and the rest get an extra 4 bytes of spare
capacity. Additionally, 32-bit string's storage class now claims its
full allocation, which is its birthright. Prior to this change, we'd
have on average 1.5 bytes of spare capacity, and now we have 7.5 bytes
of spare capacity.

Breadcrumbs threshold is increased from the super-conservative 32 to
the pretty-conservative 64. Some speed improvements are incorporated
in this change, but more are in flight. Even without those eventual
improvements, this is a worthwhile change (ASCII is still fast-pathed
and irrelevant to breadcrumbing).

For a complex real-world workload, this amounts to around a 5%
improvement to transient heap usage due to all strings and a 4%
improvement to peak heap usage due to all strings. For moderate-length
strings specifically, this gives around 11% improvement to both.
2020-03-05 16:10:23 -08:00
Alexis Laferrière
4055c94d6d [stdlib] Update one String initializer and client to use @_spi 2020-02-19 16:38:51 -08:00
Max Desiatov
67297904ac [WebAssembly] Add ifdefs for the WASI target 2020-02-08 07:37:10 +00:00
Luciano Almeida
392baefc47 [stdlib][Qol] SR-11295 Removing stdlib unnecessary coercions (#27165)
* Removing unnecessary casts from stdlib

* Minor adjustments

* [stdlib][qol] Clean up error variable assign NSError

* Removing unnecessary coercions after removing DeclRefExpr restriction.
2019-12-11 07:30:25 -08:00
Michael Ilseman
8463e53df3 [stdlib] [gardening] Refactor bridging from StringStorage.swift
Add StringStorageBridge.swift to separate out ObjC interop
functionality.

NFC
2019-11-01 17:25:08 -07:00
David Smith
90037745ad Add C calling convention shims for creating arrays and strings from buffers 2019-09-24 13:06:54 -07:00
David Smith
1efa946caf Remove stdlib and runtime dependencies on Foundation and CF 2019-09-12 18:03:14 -07:00
Arnold Schwaighofer
fe69a86929 Revert "Remove stdlib and runtime dependencies on Foundation and CF"
It is causing bots to fail.

* Revert "The __has_include(<os/system_version.h>) branch here wasn't quite right, we'll just use the dlsym one for now"

This reverts commit f824922456.

* Revert "Remove stdlib and runtime dependencies on Foundation and CF"

This reverts commit 3fe46e3f16.

rdar://54709269
2019-08-26 13:00:08 -07:00
David Smith
3fe46e3f16 Remove stdlib and runtime dependencies on Foundation and CF 2019-08-23 14:10:23 -07:00
David Smith
f559a94f1b Redo CFStringCreateWithBytes shim 2019-08-12 15:15:10 -07:00
David Smith
d0ed51925b Review comments and subsequent build fix on https://github.com/apple/swift/pull/26422 2019-08-07 13:42:51 -07:00
David Smith
0887299d9e Fix sub-scalar index distances in foreign UTF8 views 2019-08-02 16:45:42 -07:00
David Smith
c5fc715746 Reimplement the CF stub system using ObjC. The primary effect of this is to break the link-time dependency on the CF symbols, but it also improves performance a bit.
One additional tweak (setting the scalar-aligned bit on foreign indices) had to be made to avoid a performance regression for long non-ASCII foreign strings.
2019-08-01 19:56:45 -07:00
Paul Hudson
06f82a53b5 Replaced the majority of ' : ' with ': '. 2019-07-18 20:46:07 +01:00
David Smith
27359bdec6 Bridge non-ASCII SmallStrings as native Swift Strings rather than by creating CFStrings. This gets consistent behavior with non-smol Strings when the String contains a BOM 2019-07-02 16:34:22 -07:00
Michael Ilseman
4cd1e812b7 [String] Scalar-alignment bug fixes.
Fixes a general category (pun intended) of scalar-alignment bugs
surrounding exchanging non-scalar-aligned indices between views and
for slicing.

SE-0180 unifies the Index type of String and all its views and allows
non-scalar-aligned indices to be used across views. In order to
guarantee behavior, we often have to check and perform scalar
alignment. To speed up these checks, we allocate a bit denoting
known-to-be-aligned, so that the alignment check can skip the
load. The below shows what views need to check for alignment before
they can operate, and whether the indices they produce are aligned.

┌───────────────╥────────────────────┬──────────────────────────┐
│ View          ║ Requires Alignment │ Produces Aligned Indices │
╞═══════════════╬════════════════════╪══════════════════════════╡
│ Native UTF8   ║ no                 │ no                       │
├───────────────╫────────────────────┼──────────────────────────┤
│ Native UTF16  ║ yes                │ no                       │
╞═══════════════╬════════════════════╪══════════════════════════╡
│ Foreign UTF8  ║ yes                │ no                       │
├───────────────╫────────────────────┼──────────────────────────┤
│ Foreign UTF16 ║ no                 │ no                       │
╞═══════════════╬════════════════════╪══════════════════════════╡
│ UnicodeScalar ║ yes                │ yes                      │
├───────────────╫────────────────────┼──────────────────────────┤
│ Character     ║ yes                │ yes                      │
└───────────────╨────────────────────┴──────────────────────────┘

The "requires alignment" applies to any operation taking a
String.Index that's not defined entirely in terms of other operations
taking a String.Index. These include:

* index(after:)
* index(before:)
* subscript
* distance(from:to:) (since `to` is compared against directly)
* UTF16View._nativeGetOffset(for:)
2019-06-26 16:42:58 -07:00
Michael Ilseman
93d65fc9c3 [gardening] Remove trailing whitespace 2019-06-26 09:22:17 -07:00
David Smith
803227a46b Avoid O(n) character accesses in String.UTF8View._foreignCount 2019-05-21 13:22:42 -07:00
David Smith
b6d0362204 Merge pull request #24289 from Catfish-Man/bulk-discount
SR-10555 foreignCopyUTF8 should do bulk access
2019-04-29 22:17:29 -07:00
David Smith
fd0d4d858e SR-10555 foreignCopyUTF8 should do bulk access 2019-04-29 16:23:55 -07:00
swift-ci
c34c3e373e Merge pull request #23732 from Catfish-Man/pointernt 2019-04-23 14:58:34 -07:00
David Smith
96a93261d0 Use the Swift runtime's (faster) class check in the stdlib instead of shimming object_getClass() 2019-04-23 13:05:25 -07:00
David Smith
584fbfc245 Avoid the overhead of looking up the current CFAllocator in String bridging 2019-04-15 15:28:00 -07:00
Mike Ash
fa5888fb3f [Stdlib][Overlays] Rename various classes to avoid conflicting ObjC names.
Old Swift and new Swift runtimes and overlays need to coexist in the same process. This means there must not be any classes which have the same ObjC runtime name in old and new, because the ObjC runtime doesn't like name collisions.

When possible without breaking source compatibility, classes were renamed in Swift, which results in a different ObjC name.

Public classes were renamed only on the ObjC side using the @_objcRuntimeName attribute.

This is similar to the work done in pull request #19295. That only renamed @objc classes. This renames all of the others, since even pure Swift classes still get an ObjC name.

rdar://problem/46646438
2019-01-15 12:21:20 -05:00
Michael Ilseman
5a6d2dfa59 [String] Switch ABI to only use 4 discriminator bits.
In anticipation of potential future HW features, e.g. armv8.5 memory
tagging, only use the high 4 bytes as discriminator bits in
_BridgeObject rather than the top 8 bits. Utilize two perf flags to
cover this instead. This requires shifting around a fair amount of
internal complexity.
2018-12-19 13:54:50 -08:00
Xiaodi Wu
f9e8b6363e [gardening] Make edits for stdlib style [NFC] 2018-12-02 15:26:35 -05:00
David Smith
8b57921905 Assorted bridging changes:
• Convert _AbstractStringStorage to a protocol, and the free functions used to deduplicate implementations to extensions on that protocol.
• Move 'start' into the abstract type and use that to simplify some code
• Move the ASCII fast path for length into UTF16View.
• Add a weirder but faster way to check which (if any) of our NSString subclasses a given object is, and adopt it
2018-11-28 16:04:34 -08:00
Slava Pestov
86b5411a5a stdlib: __SwiftNativeNSString does not need to be public 2018-11-27 00:16:34 -05:00
Ben Cohen
1673c12d78 [stdlib] Replace "sanityCheck" with "internalInvariant" (#20616)
* Replace "sanityCheck" with "internalInvariant"
2018-11-15 20:50:22 -08:00
David Smith
8bb6b78e36 Use super trampolines to let us override several more NSString methods (-UTF8String, -cStringUsingEncoding:, and -getCString:maxLength:encoding:) for performance 2018-11-15 17:10:51 -08:00
David Smith
96691208e9 Bridged Strings should have some different/additional overrides for performance 2018-11-08 11:08:03 -08:00
Michael Ilseman
62549c81e8 [String] Add _CocoaString typealias to all build configurations.
Conditionally compiling out _CocoaString from 32-bit string will be
way too unweidly. Fixes Android bots.
2018-11-05 16:48:05 -08:00
Michael Ilseman
948655e850 [String] Cleanups, comments, documentation
After rebasing on master and incorporating more 32-bit support,
perform a bunch of cleanup, documentation updates, comments, move code
back to String declaration, etc.
2018-11-04 10:42:42 -08:00
Karoy Lorentey
40aae6b235 [String] 32-bit platform support
Add support for 32-bit platforms for UTF-8 backed String.
2018-11-04 10:42:41 -08:00
Michael Ilseman
cb0fbc6fc7 [String] 5X Faster getCharacters implementation
Rather than bounce through the UTF-16 view, implement custom
transcoding for getCharacters. This speeds it up by around 5X. Adds
tests.
2018-11-04 10:42:41 -08:00
Michael Ilseman
e2c2e479bb [test] Test the breadcrumbing String<->Cocoa interface 2018-11-04 10:42:41 -08:00
Michael Ilseman
d5da6fdbfd [String] More comparison speedups and cleanup 2018-11-04 10:42:41 -08:00
Michael Ilseman
a37d110adf [String] Constant-fold small strings from literals.
Tweak and adjust code so that the SIL optimizer can constant-fold
small strings from literals. Also some cleanup.
2018-11-04 10:42:41 -08:00
Michael Ilseman
8851bac1be [String] Inlining, NFC fast paths, and more.
Add inlinability annotations to restore performance parity with 4.2 String.

Take advantage of known NFC as a fast-path for comparison, and
overhaul comparison dispatch.

RRC improvements and optmizations.
2018-11-04 10:42:41 -08:00