Skip to content

FlattenSequence/distance(from:to:) benchmarks. #71786

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

Conversation

oscbyspro
Copy link
Contributor

@oscbyspro oscbyspro commented Feb 21, 2024

I wrote some benchmarks for (#71648). I'm new to /benchmark, but I read the README and ran the script.

@glessard
Copy link
Contributor

@swift-ci please benchmark

@glessard
Copy link
Contributor

@swift-ci please smoke test

Comment on lines 44 to 46
let minor = repeatElement(00000, count: inner)
let major = repeatElement(minor, count: outer)
let flattened: FlattenSequence = major.joined()
Copy link
Contributor

@glessard glessard Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be at the top level or, alternatively, in a setUpFunction, so that creating them isn't part of the measurement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want to move these? It would be a lot of ceremony for 3 integers. Note that distance(from:to:) is called 2 * outer^2 * inner^2 * iterations times. I'm asking just in case you thought they were arrays.

▿ Swift.FlattenSequence<Swift.Repeated<Swift.Repeated<Swift.Int>>>
 ▿ _base: Swift.Repeated<Swift.Repeated<Swift.Int>>
  - count: 32
  ▿ repeatedValue: Swift.Repeated<Swift.Int>
   - count: 32
   - repeatedValue: 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I found a solution I don't hate, then added a few sequential benchmarks for good measure. It's boilerplate-y but symmetrical. You could make it generic, but perhaps manual specialization is preferred in benchmarks?

@@ -89,6 +89,7 @@ import Exclusivity
import ExistentialPerformance
import Fibonacci
import FindStringNaive
import FlattenDistanceFromTo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add register(FlattenDistanceFromTo.benchmarks) at line 286 below.
With the PR as-is the benchmark runner still does not know about this addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. The script could use some love too:

The script will automatically:

  1. Edit main.swift by importing and registering* your new Swift module.

*The cake auto-registration is a lie.

1. Registered the new benchmarks in benchmark/utils/main.swift.
1. Parameterized benchmark dependencies, per code review.
1. Added some sequential tests with String as the inner collection.
@oscbyspro
Copy link
Contributor Author

oscbyspro commented Feb 22, 2024

I've updated the PR and added a few string-based tests. Here's some results using XCTest:

MacBook Pro, 13-inch, M1, 2020.

Swift 5.9

Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess16x16]' passed (0.027 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess16x32]' passed (0.116 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess32x16]' passed (0.125 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess32x32]' passed (0.908 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString08x08]' passed (0.007 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString08x16]' passed (0.038 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString16x08]' passed (0.021 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString16x16]' passed (0.299 seconds).

After (#71648)

Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess16x16]' passed (0.002 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess16x32]' passed (0.003 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess32x16]' passed (0.004 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRandomAccess32x32]' passed (0.016 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString08x08]' passed (0.002 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString08x16]' passed (0.015 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString16x08]' passed (0.013 seconds).
Test Case '-[FlattenTests.FlattenDistanceFromTo testRepeatString16x16]' passed (0.108 seconds).

So if these tests do not "run in a few milliseconds for N = 1", they might after applying the patch.

1. Replaced occurrences of Repeated<T> with Array<T>.
@oscbyspro
Copy link
Contributor Author

oscbyspro commented Feb 22, 2024

I suppose you may write a better algorithm if all inner collections have the same number of elements, so I replaced occurrences of Repeated<T> with Array<T>. Array may also be a more interesting test subject, as it’s ubiquitous 🤷‍♂️

@glessard
Copy link
Contributor

@swift-ci please benchmark

1. The auto-generated [BenchmarkInfo] was given the wrong name.
@oscbyspro
Copy link
Contributor Author

oscbyspro commented Feb 23, 2024

I looked at the logs and saw that the auto-generated [BenchmarkInfo] was given the wrong name. I have changed the name in the most recent commit.

/Users/ec2-user/jenkins/workspace/swift-PR-macos-perf/swift/benchmark/utils/main.swift:286:32: error: value of type '[BenchmarkInfo]' has no member 'benchmarks'
register(FlattenDistanceFromTo.benchmarks)
         ~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~

@glessard
Copy link
Contributor

@swift-ci please benchmark

@glessard
Copy link
Contributor

Benchmark Check Report
⛔️⏱ FlattenDistanceFromTo.Array.String.08.16 execution took at least 46308 μs.
Decrease the workload of FlattenDistanceFromTo.Array.String.08.16 by a factor of 64 (100), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.08.16
⛔️⏱ FlattenDistanceFromTo.Array.Array.32.16 execution took at least 514988 μs.
Decrease the workload of FlattenDistanceFromTo.Array.Array.32.16 by a factor of 1024 (1000), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.32.16
⚠️ FlattenDistanceFromTo.Array.String.08.08 execution took at least 4786 μs.
Decrease the workload of FlattenDistanceFromTo.Array.String.08.08 by a factor of 8 (10), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.08.08
⛔️⏱ FlattenDistanceFromTo.Array.String.16.08 execution took at least 37444 μs.
Decrease the workload of FlattenDistanceFromTo.Array.String.16.08 by a factor of 64 (100), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.16.08
⛔️⏱ FlattenDistanceFromTo.Array.Array.16.16 execution took at least 64735 μs.
Decrease the workload of FlattenDistanceFromTo.Array.Array.16.16 by a factor of 128 (100), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.16.16
⛔️⏱ FlattenDistanceFromTo.Array.Array.16.32 execution took at least 306107 μs.
Decrease the workload of FlattenDistanceFromTo.Array.Array.16.32 by a factor of 512 (1000), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.16.32
⛔️⏱ FlattenDistanceFromTo.Array.String.16.16 execution took at least 368676 μs.
Decrease the workload of FlattenDistanceFromTo.Array.String.16.16 by a factor of 512 (1000), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.16.16
⛔️⏱ FlattenDistanceFromTo.Array.Array.32.32 execution took at least 2418596 μs.
Decrease the workload of FlattenDistanceFromTo.Array.Array.32.32 by a factor of 4096 (10000), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.32.32

@glessard
Copy link
Contributor

glessard commented Feb 24, 2024

It looks like they'd still be a bit long-running even after the performance improvement.

The benchmarks took too long to perform, so I reduced the size of each payload.
@oscbyspro
Copy link
Contributor Author

oscbyspro commented Feb 24, 2024

I have cut the payload dimensions into halves and quarters in the most recent commit.

oscbyspro added a commit to oscbyspro/swift that referenced this pull request Feb 24, 2024
This commit addresses some trials and tribulations I encountered while working on (swiftlang#71786). It:

1. fixes the auto-registration regex
2. fixes the auto-generated array's name
3. generates the current year for the license header
4. generates some dashes for the license header
@glessard
Copy link
Contributor

@swift-ci please benchmark

@glessard
Copy link
Contributor

✅ | Benchmark Check Report
---|---
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.08.04
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.04.04
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.04.08
⛔️⏱ | FlattenDistanceFromTo.Array.String.04.04 has setup overhead of 9 μs (7.6%).
Move initialization of benchmark data to the setUpFunction registered in BenchmarkInfo.
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.04.04
⚠️⏱ | FlattenDistanceFromTo.Array.Array.08.08 execution took at least 2021 μs.
Decrease the workload of FlattenDistanceFromTo.Array.Array.08.08 by a factor of 4 (10), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.08.08
unable to compute memory footprint of FlattenDistanceFromTo.Array.Array.04x08
⚠️⏱ | FlattenDistanceFromTo.Array.String.08.08 execution took at least 5171 μs.
Decrease the workload of FlattenDistanceFromTo.Array.String.08.08 by a factor of 8 (10), to be less than 1000 μs.
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.08.08
unable to compute memory footprint of FlattenDistanceFromTo.Array.String.08.04

@glessard
Copy link
Contributor

Two mysterious issues remaining in the benchmarks:

  • it complains about the setup time for FlattenDistanceFromTo.Array.String.04.04, though it's no different than the others.
  • it complains about not figuring out the memory footprint in most cases.
    These seem like non-issues to me.

@glessard
Copy link
Contributor

@swift-ci please smoke test

@glessard glessard enabled auto-merge February 24, 2024 22:21
@glessard glessard merged commit 7ed54c9 into swiftlang:main Feb 25, 2024
@oscbyspro oscbyspro deleted the better-joined-distance-from-to-benchmarks branch March 2, 2024 12:58
glessard pushed a commit to glessard/swift that referenced this pull request Mar 7, 2024
This commit addresses some trials and tribulations I encountered while working on (swiftlang#71786). It:

1. fixes the auto-registration regex
2. fixes the auto-generated array's name
3. generates the current year for the license header
4. generates some dashes for the license header
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.

2 participants