Commit Graph

162 Commits

Author SHA1 Message Date
David Smith 51b4c9b7d0 Don't attempt to create tagged pointer strings for >11 characters (#88810)
Fixes rdar://176209576
2026-05-09 20:25:21 -07:00
Allan Shortlidge 9211f0aa46 stdlib: Remove superfluous unsafe markers.
Resolves warnings introduced by https://github.com/swiftlang/swift/pull/87271.
2026-05-08 16:14:04 -07:00
David Smith 5039cccf80 Add a cross-encoding version of strcmp, use it in bridged String equality, and expose it for Foundation's use (#87271)
There's a longstanding problem in implementing `-isEqualToString:`,
where if you don't know how to get fast access to the other NSString's
contents, you have to pick between doing it character by character (very
slow), or calling [other isEqualToString: self], which risks infinite
recursion if the other string does the same.

This cuts the gordian knot by adding a new method
`isEqualToBytes:encoding:count:`, so you can get the contents out of
`self`, and hand it to the other string, confident that it will not need
to (nor, in fact be able to) ask you anything that might recurse.
2026-05-07 02:10:44 -07:00
Doug Gregor 453277eb74 Mark the various with* functions as @safe
Functions like withUnsafeBufferPointer are, by themselves, safe to
call. It's only the operations on the unsafe pointers passed into the
closure that are the safety issue.

This was the intent spelled out in SE-0458 but was not fully realized
in the library.

Fixes rdar://174519372.
2026-04-16 22:37:54 -07:00
David Smith def9ee7464 Introduce a "single breadcrumb mode" for Strings decoded from UTF16. (#83987)
This allows us to quickly answer .utf16.count without requiring
additional allocations

Fixes rdar://160656317
2026-03-26 18:09:18 -07:00
Allan Shortlidge 0c7d87257e stdlib: Remove an unnecessary 'unsafe' expression in StringBridge.swift.
NFC.
2025-10-24 19:47:15 -07:00
David Smith 11adbdd707 Don't grow smol Strings to 16 characters when bridging out if tagged pointer formation fails (#84068)
Fixes rdar://159771498
2025-09-04 12:34:19 -07:00
David Smith 0038b92e2b Speed up -isEqual:/isEqualToString: (#83894)
Fixes rdar://159058877
2025-08-25 18:03:54 -07:00
Gabor Horvath 402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00
David Smith 1a56da655f Native implementation of -lengthOfBytesUsingEncoding, plus handling ASCII-subset MacRoman in a few places (#81791)
Fixes rdar://154341146
2025-06-27 13:48:10 -07:00
Doug Gregor 050a514588 [Strict memory safety] Update standard library for unsafe treated as a call effect 2025-04-25 21:54:23 -07:00
Allan Shortlidge d32310fb76 stdlib: Address new #StrictMemorySafety warnings. 2025-04-03 10:18:39 -07:00
David Smith f800fdf3d2 Adopt indirect tagged strings (#80425)
Fixes rdar://142991821
2025-04-01 12:53:29 -07:00
Doug Gregor 0ec13f9a90 Build compiler and runtimes without Swift
Remove dependency on macros from compiler and stdlib build when
bootstrapping the compiler without Swift available.
2025-03-06 13:32:52 -08:00
Doug Gregor 22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
David Smith 600dcd0828 Expose immortal String initialization for Foundation (#78661) 2025-01-19 07:43:13 -08:00
David Smith bed89567d4 Disable calling _swift_stdlib_dyld_is_objc_constant_string, it's unnecessary currently (#77064) 2024-10-17 19:01:08 -07:00
David Smith 3589044213 A new way to bridge constant NSStrings (#74881) 2024-07-03 20:38:33 -07:00
Kuba Mracek 7ae20b7039 [embedded] Port Swift.String to embedded Swift 2024-05-08 11:11:37 -07:00
Alejandro Alonso f9f640b141 Sendablize the standard library
oops dont add this flag

no more nonisolated
2024-03-05 15:02:09 -08:00
Karoy Lorentey b82ce9c3be [stdlib] Adopt _pointerBitWidth conditional 2023-04-27 13:33:24 -07:00
Karoy Lorentey 7d89d62dce [stdlib] Remove @_specialize attributes obsoleted by explicit type checks 2022-12-29 13:18:10 -08:00
Karoy Lorentey d00f8ed44b [stdlib] Optimize StringProtocol._toUTF16Indices/_toUTF16Offsets
Speed up conversion between UTF-16 offset ranges
and string index ranges, by carefully switching
between absolute and relative index calculations,
depending on the distance we need to go.

It is a surprisingly tricky puzzle to do this
correctly while avoiding redundant calculations.
Offset ranges within substrings add the additional
complication of having to bias offset values with
the absolute offset of the substring’s start index.
2022-12-28 20:08:05 -08:00
Karoy Lorentey 2423b8ba3c [stdlib] StringProtocol._toUTF16Indices: Speed up conversion of short ranges
Instead of calling `_toUTF16Index` twice, call it once and then use
`index(_:offsetBy:)` to potentially avoid another breadcrumbs lookup.
2022-12-27 20:22:38 -08:00
David Smith 3292f4e97e Separate out ASCII and UTF8 in NSString accessors, and use the ASCII one where we really need ASCII (#61086)
Separate out ASCII and UTF8 in NSString accessors, and use the ASCII one where we really need ASCII
2022-09-23 12:31:45 -07:00
David Smith e0dd80fe3b Handle long tagged NSStrings 2022-08-05 19:02:37 -07:00
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