Commit Graph

6693 Commits

Author SHA1 Message Date
Karoy Lorentey
2e3e88cab8 [stdlib] Optimize Set.intersection(_:)
Use a temporary bitset to speed up the `Sequence` variant by roughly a factor of ~4-6, and the set/set variant by a factor of ~1-4, depending on the ratio of overlapping elements.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
db80c78809 [stdlib] Optimize Set.filter(_:)
This works the same way as `Set.subtracting<S>(_:)`, and has similar performance benefits.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
80296bb606 [stdlib] Optimize Set.subtracting(_:)
Use a temporary bitset to avoid hashing elements more than once, and to prevent rehashings during the creation of the result set.

This leads to a speedup of about 0-4x, depending on the number of elements removed.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
8612f2f960 [stdlib] Add a fast path to Set.isDisjoint<S>(with: S)
Have the generic variant call out to the specialized overload if the argument happens to be a `Set`.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
a2540c27b7 [stdlib] Optimize Set.isStrictSuperset(of:)
- Use a temporary bitset to speed up the `Sequence` variant by roughly a factor of 4.
- Fix a logic error causing the `a == b` case for the set variant to be O(n) instead of O(1).
2021-11-01 21:38:10 -07:00
Karoy Lorentey
725ee55fbc [stdlib] Optimize Set.isSuperset<S>(of: S)
Call into the specialized overload if the argument happens to be a `Set`.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
9043c71bd7 [stdlib] Optimize Set.isStrictSubset<S>(of: S)
Use a temporary bitset to speed up the Sequence variant by roughly a factor of 3.
2021-11-01 21:38:09 -07:00
Karoy Lorentey
5e642d2b8d [stdlib] Optimize Set.isSubset<S>(of: S)
Use a temporary bitset to speed up the Sequence variant by roughly a factor of 3.
2021-11-01 21:38:09 -07:00
Karoy Lorentey
6b1f6c075c [stdlib] _SetVariant.convertedToNative: New convenience function 2021-11-01 21:38:09 -07:00
Karoy Lorentey
e2415f78d7 [stdlib] _UnsafeBitset.withTemporaryBitset: New internal function 2021-11-01 21:38:09 -07:00
Alejandro Alonso
5a0bbb9f89 [stdlib] Implement native grapheme breaking for String (#37864)
* Implement GraphemeWalker that does native grapheme breaking

* Bridged strings use native grapheme breaking for forward strides

* Implement bidirectional native grapheme breaking for native and foreign strings

* Remove ICU's grapheme breaking support

* Use UnicodeScalarView to implement GraphemeWalker

use an Iterator approach

remove Iterator conformance

* Incorporate Michael's feedback

more comments addressed

fix crlf bug

* Try bringing back some old fast paths

* Parameterize nextBoundary and previousBoundary

Parameterize nextBoundary and previousBoundary

* Implement Michael's suggestions
2021-11-01 16:52:28 -07:00
Karoy Lorentey
5d6b0bace6 [stdlib][SE-0320] Update availability of new Codable additions 2021-11-01 16:45:04 -07:00
Karoy Lorentey
6d255658ca Merge pull request #39994 from lorentey/adopt-availability-macros
[stdlib] Adopt availability macros
2021-11-01 14:45:33 -07:00
Alex Martini
29e0a31941 Merge pull request #39977 from amartini51/remove_iff_31371832
Remove "iff" from doc comments.
2021-11-01 11:26:44 -07:00
Karoy Lorentey
1dbcf23515 [stdlib] Availability macros don't work in inlinable code 😩 2021-10-31 15:00:59 -07:00
Karoy Lorentey
8ed81ae063 [stdlib] Adopt availability macros 2021-10-31 15:00:58 -07:00
Morten Bek Ditlevsen
0251957ae3 [SE-0320] Added protocol CodingKeyRepresentable (#34458) 2021-10-30 11:33:42 +01:00
Michael Gottesman
3a5049fa15 [stdlib] Mark _move as public.
Just a thinko on my part.
2021-10-29 15:37:46 -07:00
Michael Gottesman
f9122a79b7 [moveOnly] Implement a new _copy function that performs an explicit copy value.
The key thing is that the move checker will not consider the explicit copy value
to be a copy_value that can be rewritten, ensuring that any uses of the result
of the explicit copy_value (consuming or other wise) are not checked.

Similar to the _move operator I recently introduced, this is a transparent
function so we can perform one level of specialization and thus at least be
generic over all concrete types.
2021-10-29 15:37:46 -07:00
Alex Martini
f81d11492a Remove repetitive wording.
Fixes <rdar://59618453>
2021-10-29 11:05:21 -07:00
Alex Martini
1186fc7f13 Remove "iff" from doc comments.
This abbreviation for "if and only if" is confusing to those not coming
from a background in formal mathematics, and is frequently reported as a
type by developers reading the documentation.

This commit also changes doc comments in internal and private members,
which don't become part of the public documentation, because omitting
"iff" everywhere makes it much easier to check for any later changes
that reintroduce it.
2021-10-29 10:16:27 -07:00
Erik Eckstein
a5030a63b0 stdlib: add a _unsafePerformance function which disable performance diagnostics in the passed closure. 2021-10-28 18:44:44 +02:00
Michael Gottesman
114789707c [moveOnly] Implement a semi-generic _move function that can be used on non-generic, non-existential values.
This patch introduces a new stdlib function called _move:

```Swift
  @_alwaysEmitIntoClient
  @_transparent
  @_semantics("lifetimemanagement.move")
  public func _move<T>(_ value: __owned T) -> T {
  #if $ExperimentalMoveOnly
    Builtin.move(value)
  #else
    value
  #endif
  }
```

It is a first attempt at creating a "move" function for Swift, albeit a skleton
one since we do not yet perform the "no use after move" analysis. But this at
leasts gets the skeleton into place so we can built the analysis on top of it
and churn tree in a manageable way. Thus in its current incarnation, all it does
is take in an __owned +1 parameter and returns it after moving it through
Builtin.move.

Given that we want to use an OSSA based analysis for our "no use after move"
analysis and we do not have opaque values yet, we can not supporting moving
generic values since they are address only. This has stymied us in the past from
creating this function. With the implementation in this PR via a bit of
cleverness, we are now able to support this as a generic function over all
concrete types by being a little clever.

The trick is that when we transparent inline _move (to get the builtin), we
perform one level of specialization causing the inlined Builtin.move to be of a
loadable type. If after transparent inlining, we inline builtin "move" into a
context where it is still address only, we emit a diagnostic telling the user
that they applied move to a generic or existential and that this is not yet
supported.

The reason why we are taking this approach is that we wish to use this to
implement a new (as yet unwritten) diagnostic pass that verifies that _move
(even for non-trivial copyable values) ends the lifetime of the value. This will
ensure that one can write the following code to reliably end the lifetime of a
let binding in Swift:

```Swift
  let x = Klass()
  let _ = _move(x)
  // hypotheticalUse(x)
```

Without the diagnostic pass, if one were to write another hypothetical use of x
after the _move, the compiler would copy x to at least hypotheticalUse(x)
meaning the lifetime of x would not end at the _move, =><=.

So to implement this diagnostic pass, we want to use the OSSA infrastructure and
that only works on objects! So how do we square this circle: by taking advantage
of the mandatory SIL optimzier pipeline! Specifically we take advantage of the
following:

1. Mandatory Inlining and Predictable Dead Allocation Elimination run before any
   of the move only diagnostic passes that we run.

2. Mandatory Inlining is able to specialize a callee a single level when it
   inlines code. One can take advantage of this to even at -Onone to
   monomorphosize code.

and then note that _move is such a simple function that predictable dead
allocation elimination is able to without issue eliminate the extra alloc_stack
that appear in the caller after inlining without issue. So we (as the tests
show) get SIL that for concrete types looks exactly like we just had run a
move_value for that specific type as an object since we promote away the
stores/loads in favor of object operations when we eliminate the allocation.

In order to prevent any issue with this being used in a context where multiple
specializations may occur, I made the inliner emit a diagnostic if it inlines
_move into a function that applies it to an address only value. The diagnostic
is emitted at the source location where the function call occurs so it is easy
to find, e.x.:

```
func addressOnlyMove<T>(t: T) -> T {
    _move(t) // expected-error {{move() used on a generic or existential value}}
}

moveonly_builtin_generic_failure.swift:12:5: error: move() used on a generic or existential value
    _move(t)
    ^
```

To eliminate any potential ABI impact, if someone calls _move in a way that
causes it to be used in a context where the transparent inliner will not inline
it, I taught IRGen that Builtin.move is equivalent to a take from src -> dst and
marked _move as always emit into client (AEIC). I also took advantage of the
feature flag I added in the previous commit in order to prevent any cond_fails
from exposing Builtin.move in the stdlib. If one does not pass in the flag
-enable-experimental-move-only then the function just returns the value without
calling Builtin.move, so we are safe.

rdar://83957028
2021-10-27 19:36:49 -07:00
Jonathan Grynspan
31b44c8f22 Merge pull request #39925 from apple/jgrynspan/84672024-fix
Fix incorrect precondition test (gt instead of gte) in TemporaryAllocation.swift
2021-10-27 11:58:56 -04:00
Jonathan Grynspan
208b8d493a Fix incorrect precondition test (gt instead of gte) in TemporaryAllocation.swift 2021-10-26 14:19:25 -04:00
Xiaodi Wu
3927e78f64 Add to-do for Stride* implementation of _customContainsEquatableElement (#39908) 2021-10-25 21:00:06 -04:00
Jonathan Grynspan
687cee9bfa Merge pull request #37666 from grynspan/jgrynspan/temporary-buffers
[SE-0322] Temporary uninitialized buffers
2021-10-25 15:53:50 -04:00
Xiaodi Wu
fa4d08ab9f Fix _customContainsEquatableElement implementation for Stride* types (#39903)
* Fix `_customContainsEquatableElement` implementation for `Stride*` types

* Add tests for `Stride*.contains`
2021-10-25 14:07:01 -04:00
Jonathan Grynspan
f1bf7badba [SE-0322] Temporary uninitialized buffers
Adds two new IRGen-level builtins (one for allocating, the other for deallocating), a stdlib shim function for enhanced stack-promotion heuristics, and the proposed public stdlib functions.
2021-10-25 11:20:10 -04:00
Guillaume Lessard
8eea64271a Merge pull request #39824 from glessard/urbp-copycontents-optimization
[stdlib] a minor optimization
2021-10-21 11:39:43 -06:00
Guillaume Lessard
17c19fba06 [stdlib] fix an indexing inconsistency
This iteration ranged from `buffer.startIndex` to `buffer.count`, rather than to `buffer.endIndex`.
Using `buffer.indices` is a better solution in any case.
2021-10-16 12:47:16 -06:00
Guillaume Lessard
4aed3d1aac [gardening] add spaces after commas, for readability 2021-10-16 12:46:42 -06:00
Guillaume Lessard
4d05b759b2 [stdlib] a minor optimization
We have already done the hard work of unwrapping the optionals
that represent the first and last pointer, and calling `self.count` does it all over again. Use the distance between the unwrapped pointers instead.
2021-10-15 11:46:58 -06:00
Kuba (Brecka) Mracek
7538949859 Split out CommandLine enum into a separate static library, allow removing it from stdlib (#39591)
This is for the 'freestanding' build to stop assuming the platform has argc/argv.

- Introduce a new sub-library, libswiftCommandLineSupport.a
- Move stubs/CommandLine.cpp into this library
- Conditionally embed it into libswiftCore
- Conditionally embed it into libswiftPrivateLibcExtras if not in libswiftCore to support testing
- Add SWIFT_STDLIB_HAS_COMMANDLINE CMake (and build-script) flag
2021-10-13 07:02:43 -07:00
Kuba (Brecka) Mracek
1147d08ae3 Disable stable ABI on freestanding stdlib (#39674) 2021-10-12 15:25:57 -07:00
Karoy Lorentey
dd40ff2929 [stdlib][NFC] Put stored properties & primary initializers before other members in struct declarations 2021-10-05 22:01:35 -07:00
Max Desiatov
e4f71c8e11 stdlib: add support for WebAssembly in VarArgs (#39361)
When targeting `wasm32` we're relying on the alignment padding branch that's used for 32-bit ARM.
2021-10-05 10:21:31 +01:00
Alex Martini
2ccf3a29a3 Merge pull request #39403 from amartini51/identifiable_83422517
Avoid 'e.g.' per Apple Style.
2021-10-04 10:58:40 -07:00
Michael Ilseman
f8e5f28b86 Merge pull request #38922 from Azoy/native-normalization
[stdlib] Implement native normalization for String
2021-10-01 13:58:48 -06:00
eeckstein
2135f247d4 Merge pull request #39520 from eeckstein/fix-cow-checks
stdlib: add a build-script option --enable-array-cow-checks to enable compilation of COW checks.
2021-10-01 07:08:51 +02:00
Meghana Gupta
83c023446a Use -enable-ossa-modules for all of stdlib/public (#39478)
With this, even non-transparent functions will have their ownership eliminated later in the pipeline
2021-09-30 10:20:08 -07:00
Erik Eckstein
eb9621fe86 stdlib: add a build-script option --enable-array-cow-checks to enable compilation of COW checks.
And set this option in various presets for buildbots.

Don't enable the checks by default because when linking against the OS library (which does not support COW checking) it will result in unresolved symbol errors.
So far it was handled by an availability checks against 9999 (which was a hack), but this does not work anymore.

Note, all this is only relevant for assert builds of the stdlib.

rdar://83673798
2021-09-30 18:06:52 +02:00
Alejandro Alonso
014e822cb2 Address Michael's comments
fix infinite recursion bug

NFC: Remove early ccc check

remember that false is turned on
2021-09-29 14:20:22 -07:00
Alejandro Alonso
98aaa157ec Implement native normalization for String
use >/< instead of !=

fix some bugs

fix
2021-09-29 14:20:21 -07:00
Nate Cook
2daa005c5c [Docs] Note that partition(by:) is unstable (#39384) 2021-09-29 13:54:31 -05:00
Erik Eckstein
af71088d29 libswift: bootstrapping build
Adding build modes for libswift: off, hosttools, bootstrapping, bootstrapping-with-hostlibs
The two bootstrapping modes are new. For details see libswift/README.md
2021-09-28 18:51:42 +02:00
Alex Martini
9fa3121181 Avoid 'e.g.' per Apple Style. 2021-09-22 16:51:45 -07:00
Kuba (Brecka) Mracek
6766824d51 Add a SWIFT_STDLIB_HAS_STDIN flag to compile out the Swift.readLine() API from the stdlib (for the 'freestanding' preset) (#39332) 2021-09-17 10:42:14 -07:00
Karoy Lorentey
8e5ccec2c7 Merge pull request #39155 from lorentey/RawRepresentable-hashing-fix
[stdlib] Allow RawRepresentable types to customize their hashing without implementing hashValue
2021-09-17 00:50:10 -07:00
LucianoAlmeida
00cb1c5330 [stdlib] Improve set isDisjoint by iterating on smaller set 2021-09-14 17:28:45 -03:00