# UniqueArray/\_RigidArray count not using \_assumeNonNegative

**URL:** <https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621>\
**Category:** Standard Library\
**Created:** [November 10, 2026, 1:11pm UTC](https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621 "2026-11-10T13:11:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorrden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorrden/32/28670_2.png) [@lorrden](https://forums.swift.org/u/lorrden)\
**Post date:** [November 10, 2026, 1:11pm UTC](https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621/1 "2026-11-10T13:11:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MahdiBM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mahdibm/32/45271_2.png) [@MahdiBM](https://forums.swift.org/u/MahdiBM)\
**Post date:** [November 10, 2026, 1:53pm UTC](https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621/2 "2026-11-10T13:53:58Z")

</div>

@lorentey opinions?

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

> <https://github.com/swiftlang/swift/blob/f438fd778702d2ae50f8be1102de9c6e150eae3d/stdlib/public/core/RigidArray/RigidArray%2BContainer.swift#L28>

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.

---

<div class="post-metadata">

**Author:** ![lorrden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorrden/32/28670_2.png) [@lorrden](https://forums.swift.org/u/lorrden)\
**Post date:** [November 10, 2026, 2:38pm UTC](https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621/3 "2026-11-10T14:38:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [November 10, 2026, 3:10pm UTC](https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621/4 "2026-11-10T15:10:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lorrden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorrden/32/28670_2.png) [@lorrden](https://forums.swift.org/u/lorrden)\
**Post date:** [November 11, 2026, 9:12am UTC](https://forums.swift.org/t/uniquearray-rigidarray-count-not-using-assumenonnegative/90621/5 "2026-11-11T09:12:07Z")

</div>

I prepared this [Add \_assumeNonNegative on \_RigidArray count property by lorrden · Pull Request #94193 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/pull/94193)
