Skip to content

[SR-9425][Sema] Use derived conformance as witness when possible for == operator of raw representable enums #36752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 21, 2021

Conversation

LucianoPAlmeida
Copy link
Contributor

@LucianoPAlmeida LucianoPAlmeida commented Apr 5, 2021

There are two overloads involved in this case

  • <Self where Self : Equatable> (Self.Type) -> (Self, Self) -> Bool
  • <T where T : RawRepresentable, T.RawValue : Equatable> (T, T) -> Bool

Solver is finding the first one as best because the ranking rule of non-generic is better than generic.
However in CSApply https://github.com/apple/swift/blob/22506f9bdb009c0c82410406d825c87c08af7f29/lib/Sema/CSApply.cpp#L621
currently it is finding the <T where T : RawRepresentable, T.RawValue : Equatable> (T, T) -> Bool as the Equatable (WitnessChecker::findBestWitness find only this one)witness which ends up applied. What given the implementation of it:

@inlinable // trivial-implementation
public func == <T: RawRepresentable>(lhs: T, rhs: T) -> Bool
  where T.RawValue: Equatable {
  return lhs.rawValue == rhs.rawValue
}

When raw value is a String the codegen ends up with a string compare.

This PR adjusts ConformanceChecker::resolveWitnessViaLookup to not consider this witness as best match for swift raw representable enums when derive Equatable is possible(favor derived). Therefore avoiding a str compare for those cases.

Note
More details on the JIRA issue
Also, this PR although fixes the issue and makes sense to me, I'm not familiar enough with this to know if it is really the best fix.

cc @eeckstein @xedin

Resolves SR-9425, rdar://46555205

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please smoke test

@LucianoPAlmeida LucianoPAlmeida force-pushed the SR-9425-enum-eq branch 2 times, most recently from d9e0698 to c4df9b6 Compare April 7, 2021 01:41
@LucianoPAlmeida LucianoPAlmeida changed the title [DNM][SR-9425][Sema] Use derived conformance as witness when possible for == operator of raw representable enums [SR-9425][Sema] Use derived conformance as witness when possible for == operator of raw representable enums Apr 7, 2021
@LucianoPAlmeida LucianoPAlmeida marked this pull request as ready for review April 7, 2021 01:48
@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please smoke test

@LucianoPAlmeida
Copy link
Contributor Author

There is a benchmark for this https://github.com/apple/swift/blob/main/benchmark/single-source/OpenClose.swift
Just curious about the impact :)

@swift-ci Please smoke benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
StringFromLongWholeSubstring 4 5 +25.0% 0.80x
 
Improvement OLD NEW DELTA RATIO
OpenClose 108 45 -58.3% 2.40x
String.data.Medium 117 108 -7.7% 1.08x (?)
DictionaryKeysContainsCocoa 28 26 -7.1% 1.08x (?)

Code size: -O

Improvement OLD NEW DELTA RATIO
TestsUtils.o 28931 24595 -15.0% 1.18x
DriverUtils.o 129479 117847 -9.0% 1.10x
OpenClose.o 3584 3368 -6.0% 1.06x

Performance: -Osize

Regression OLD NEW DELTA RATIO
FlattenListFlatMap 4119 6783 +64.7% 0.61x (?)
FlattenListLoop 1632 2507 +53.6% 0.65x (?)
StringFromLongWholeSubstring 4 5 +25.0% 0.80x
ArrayAppendLatin1Substring 28692 31428 +9.5% 0.91x
ArrayAppendUTF16Substring 28152 30744 +9.2% 0.92x (?)
ArrayAppendAsciiSubstring 28188 30744 +9.1% 0.92x (?)
RandomShuffleLCG2 416 448 +7.7% 0.93x
Data.hash.Medium 39 42 +7.7% 0.93x
 
Improvement OLD NEW DELTA RATIO
OpenClose 114 48 -57.9% 2.37x
DictionaryKeysContainsCocoa 29 26 -10.3% 1.12x (?)
ProtocolDispatch 371 342 -7.8% 1.08x (?)

Code size: -Osize

Improvement OLD NEW DELTA RATIO
TestsUtils.o 23152 18513 -20.0% 1.25x
DriverUtils.o 123221 112113 -9.0% 1.10x
OpenClose.o 3629 3473 -4.3% 1.04x

Performance: -Onone

Improvement OLD NEW DELTA RATIO
OpenClose 1386 1010 -27.1% 1.37x

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac Pro
  Model Identifier: MacPro6,1
  Processor Name: 12-Core Intel Xeon E5
  Processor Speed: 2.7 GHz
  Number of Processors: 1
  Total Number of Cores: 12
  L2 Cache (per Core): 256 KB
  L3 Cache: 30 MB
  Memory: 64 GB

@milseman
Copy link
Member

This has been such a long-standing perf issue. Thanks for fixing this!

@CodaFi
Copy link
Contributor

CodaFi commented Apr 21, 2021

@swift-ci test

@CodaFi CodaFi merged commit ce7bdab into swiftlang:main Apr 21, 2021
slavapestov added a commit to slavapestov/swift that referenced this pull request Aug 18, 2021
This fixes a regression from swiftlang#36752.

Previously, a RawRepresentable enum would use the default implementation
of == from a protocol extension in the standard library. After the above
PR, we began synthesizing the == operator, just like we do for ordinary
enums.

However, when type checking an older swiftinterface file, we would
still synthesize the declaration and try to use it, even though it would
not have existed in the older dylib built with the older compiler.

Fix this by not deriving witnesses in swiftinterface files at all.
The interface should already explicitly list out all derived witnesses;
if one is not listed, it should not be derived, because it does not
exist in the dylib.

Fixes rdar://problem/80466745.
slavapestov added a commit to slavapestov/swift that referenced this pull request Dec 2, 2021
…non-resilient modules

This change was originally introduced in swiftlang#36752.

The problem occurs in the following scenario:

- New compiler with this change is used to build a dylib and swiftinterface
  file for module A, which defines a RawRepresentable enum type E.

- Module B imports module A and references == on two E instances.

- The module B is run against a dylib of module A built with the old compiler.

At this point, module B will not link (or run) because the symbol for E.==
is not present in the old dylib for A.

The only real solution here is to disable this new optimization when
building a module for -enable-library-evolution, unfortunately.

In the future, we could make the derived E.== symbol @_alwaysEmitIntoClient.
However today, synthesized declarations cannot be @_alwaysEmitIntoClient
because they do not have source text that can be emitted into the
swiftinterface file. One day, we might gain the ability to print Exprs as
source text that parses back in, at which point we could allow synthesized
declarations to be @_alwaysEmitIntoClient.

Fixes rdar://problem/84912735 and rdar://problem/82919247.
slavapestov added a commit to slavapestov/swift that referenced this pull request Jan 24, 2022
Unfortunately, we can't make this optimization work internally for now.
However, we can revisit it later. One strategy would be to emit the
witness as a private declaration, so that clients call it through the
witness table.

Fixes rdar://problem/86861522.
slavapestov added a commit to slavapestov/swift that referenced this pull request Jan 24, 2022
Unfortunately, we can't make this optimization work internally for now.
However, we can revisit it later. One strategy would be to emit the
witness as a private declaration, so that clients call it through the
witness table.

Fixes rdar://problem/86861522.
slavapestov added a commit that referenced this pull request Jan 25, 2022
slavapestov added a commit that referenced this pull request Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants