UniqueArray/_RigidArray count not using _assumeNonNegative

Hi, I was tracing down some performance deviation between an algorithm implemented in Rust and Swift. Initially the rust implementation was faster, I traced that down to various runtime checks and switched my array to a unique array. But range checks were not elided seemingly because the count is not assumed non-negative like capacity is.

Flagging it here in case someone want to weigh in if this is a bug or not, I can raise an issue if it is.

1 Like

@lorentey opinions?

I think you're on point.
I'd wrap this _count in here, in _assumeNonNegative:

I can run the CI etc... if you'd like to PR this to the repository and mention it here (disclaimer I'm not part of the Swift team so they'll have to approve as well).

If you don't feel comfortable enough, I can PR it instead.

I'm not sure if swift-collections where UniqueArray and RigidArray originate from would like to keep these in sync as well or not. @lorentey would know.

2 Likes

I can prepare a PR for the change itself, but I am not really sure if this requires a specific test to be added as well.

You can open a pr without a test, and reviewers will decide if we need one. This kind of tiny perf fix have often been done without adding a test.

2 Likes

I prepared this Add _assumeNonNegative on _RigidArray count property by lorrden · Pull Request #94193 · swiftlang/swift · GitHub