# Require Sequence.underestimatedCount be O(1)

**URL:** <https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613>\
**Category:** Pitches\
**Created:** [March 6, 2018, 12:02am UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613 "2018-03-06T00:02:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 12:02am UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/1 "2018-03-06T00:02:25Z")

</div>

Currently the default implementation of `underestimatedCount` property for a `Collection` is to simply return `count`. Problem with that is `count` is allowed to be O(n) operation on some collections and can have a side-effect of iterating over the whole collection, which defeats the whole purpose of `underestimatedCount`.

The proposed change makes `underestimatedCount` strictly O(1), which requires a certain pessimization of it's return value for `Collection` instances that are only forward or bidirectional. That is when such a collection is passed to a method generic over `Sequence`, `append(contentsOf:)` for example, the call to `reserveCapacity` might not result in allocation of enough memory up font.

`RandomAccessCollection` instances guarantee constant time `count` and therefore still can provide a default implementation of `underestimatedCount` in terms of `count`.

Implementation is available at: [apple/swift#14994](https://github.com/apple/swift/pull/14994).

**UPD** : This seems particularly important when working with `String`s. Since `String` conforms to `Collection` but it's `count` requires the traversal, so when `str.append(contentsOf: anotherString)` is called in generic context, `anotherString` will be iterated over twice unnecessarily.

**UPD 2** : swift-evolution proposal is available at [Add an underestimatedCount proposal by moiseev · Pull Request #819 · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/pull/819)

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [March 6, 2018, 2:03am UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/2 "2018-03-06T02:03:10Z")

</div>

I've [already brought this up for `Collection`](https://forums.swift.org/t/time-taken-on-collection-underestimatedcount/7336), including a [bug report](https://bugs.swift.org/browse/SR-6693).

Some of the viewers of that bug want to keep the property as O(n) for legacy compatibility for `Collection` users, although that by definition breaks legacy compatibility for pure-`Sequence` users. Since the property is supposed to be an **under** -estimate, I agree that we should sacrifice accuracy for speed.

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 6:53pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/3 "2018-03-06T18:53:43Z")

</div>

My thinking is: relying on `underestimatedCount` was a mistake if anyone did it. And why would anyone do it in the first place when there's a much more discoverable property called `count`?

Delegating to `isEmpty` does not seem like the right thing to do. The naïve implementation can be just `count == 0`, which in combination with a linear `count` would result in the same problem we're trying to avoid.

---

<div class="post-metadata">

**Author:** ![Michael\_Ilseman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_ilseman/32/44640_2.png) [@Michael\_Ilseman](https://forums.swift.org/u/Michael_Ilseman)\
**Post date:** [March 6, 2018, 6:56pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/4 "2018-03-06T18:56:08Z")

</div>

+1. Without this, I cannot use `underestimatedCount` in any performance-sensitive context (which are all the contexts I currently work in) . The only workaround is to constrain all my generics to `RandomAccessCollection` (which has O(1) count), but that's not possible when implementing more generic API.

Long term, I think a more sophisticated estimate would make sense.

```swift
enum EstimatedCount {
  case unknown
  case infinite
  case exactly(Int)
  case underestimate(Int)
  case overestimate(Int)
}
extension EstimatedCount {
  func isAtLeast(_ n: Int) -> Bool { ... }
  func isAtMost(_ n: Int) -> Bool { ... }
}

```

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 6, 2018, 7:10pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/5 "2018-03-06T19:10:08Z")

</div>

+1

I think in general, performance guarantees follow your generic constraints. So `count` is potentially O(n) unless you add the `RandomAccessCollection`, constraint as @Michael_Ilseman notes. The whole purpose of `underestimatedCount` is to provide a version of `count` which is guaranteed O(1) in non-constrained contexts, with the tradeoff that it might not be accurate.

The only other thing I would change about this property (potentially more source-breaking) is to add the requirement that `underestimatedCount` must be at least 1 if the Sequence is not empty.

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 7:29pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/6 "2018-03-06T19:29:44Z")

</div>

> [@Karl](#):
>
> The only other thing I would change about this property (potentially more source-breaking) is to add the requirement that underestimatedCount must be at least 1 if the Sequence is not empty.

How would you tell that it's not empty? Iterating over a `Sequence` is consuming and this is pretty much the only way I can think of to tell whether there's anything in there: `makeIterator().next() != nil`.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 6, 2018, 7:35pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/7 "2018-03-06T19:35:51Z")

</div>

I was going to say use `isEmpty`, but I see that's only on Collection, not Sequence.

Can we at least make it a requirement for Collection? The same way we change the complexity if it conforms to `RandomAccessCollection`?

Otherwise your Collection-constrained code needs to account for somebody implementing this with `return 0`. That means that any time you use `underestimatedCount`, you instead have to write `max(underestimatedCount, isEmpty ? 0:1)` or you might create a bug.

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 7:37pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/8 "2018-03-06T19:37:38Z")

</div>

> [@Karl](#):
>
> Can we at least make it a requirement for Collection?

See my reply above:

> [@moiseev](#):
>
> Delegating to isEmpty does not seem like the right thing to do. The naïve implementation can be just count == 0, which in combination with a linear count would result in the same problem we’re trying to avoid.

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 7:39pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/9 "2018-03-06T19:39:56Z")

</div>

> [@Karl](#):
>
> you instead have to write max(underestimatedCount, isEmpty ? 0:1) or you might create a bug.

Can you please elaborate on the "bug" thing?

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 6, 2018, 7:43pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/10 "2018-03-06T19:43:31Z")

</div>

That would only be a valid implementation of `isEmpty` if `count` also happens to be O(1). Otherwise you already broke the performance requirements, and when you (the programmer) becomes less naïve, you can fix it and see performance wins from code that was using the protocol correctly.

> [@moiseev](#):
>
> Can you please elaborate on the “bug” thing?

Many would assume that an `underestimatedCount` of 0 means the Collection is empty (not knowing that it comes from Sequence, where such things cannot be easily tested). That might mean you skip processing altogether, allocate a buffer which can hold 0 items (if you gated by `isEmpty` before processing), or calculate a size for batch operations which is some fraction of 0 (i.e. 0).

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 6, 2018, 8:45pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/11 "2018-03-06T20:45:48Z")

</div>

> [@Karl](#):
>
> Many would assume that an underestimatedCount of 0 means the Collection is empty (not knowing that it comes from Sequence, where such things cannot be easily tested).

Why would many assume such a thing? `underestimatedCount` is very clearly underestimated, and 0 is an underestimation.

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [March 6, 2018, 9:05pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/12 "2018-03-06T21:05:59Z")

</div>

> [@moiseev](#):
>
> My thinking is: relying on underestimatedCount was a mistake if anyone did it. And why would anyone do it in the first place when there’s a much more discoverable property called count?
> 
> Delegating to isEmpty does not seem like the right thing to do. The naïve implementation can be just count == 0, which in combination with a linear count would result in the same problem we’re trying to avoid.

You generally would use `underestimatedCount` only when you're using a `Sequence`. This is why the current notes of making `Collection.underestimatedCount` forward to `count` is a big mistake; if you're not calling the equally accessible `count` instead then you _intentionally_ want speed over accuracy. Or your reference to the collection is only as a `Sequence` and the current version of `Collection.underestimatedCount` will secretly screw you over.

Delegating to `isEmpty` is the right thing to do. This property must be O(1), while `count` can be at least O(n); any implementation of `isEmpty` that calls `count` is broken right then and there (modulo that specific type following `RandomAccessCollection`) and you need to file a bug report for that. But the most naïve implementation of `isEmpty` is the existing one, which just calls `startIndex` and `endIndex`, which both have to be O(1). The developer shouldn't be calling `count` within `isEmpty` unless s/he knows that `count` is faster than the two index properties.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 6, 2018, 10:48pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/13 "2018-03-06T22:48:37Z")

</div>

You're right that when somebody does that, it's clearly a bug as 0 is still technically an underestimate.

Still, a non-empty collection returning 0 is still counter-intuitive. I've seen it. It's easy to work-around, but I can also appreciate the view that `return 0` is a crappy estimation given that you can check `isEmpty` in constant-time.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 6, 2018, 10:57pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/15 "2018-03-06T22:57:30Z")

</div>

It's documented as O(1). If you can satisfy that using count, all power to you. If not, find another way or drop down to `Sequence`.

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 11:06pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/16 "2018-03-06T23:06:17Z")

</div>

I do not disagree with using `isEmpty`. Let me explain what situation I am trying to avoid.

Standard library provides a default implementation of `underestimatedCount` that's documented to be O(1), and internally calls `isEmpty`. Now enters the 3rd party developed `Collection` implementation that, for some reason, violates the " ~~count~~ isEmpty is O(1)" rule, and **does not** provide it's own implementation of `underestimatedCount` (rightfully so, because why would you care about the `Sequence` API in a `Collection` type). Voila! The default implementation provided by the standard library now violates it's own set of rules.

This, arguably artificial, situation is easily avoidable by just returning `0` all the time.

**UPD** : Corrected `count` to `isEmpty`.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [March 6, 2018, 11:15pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/17 "2018-03-06T23:15:15Z")

</div>

> [@moiseev](#):
>
> Now enters the 3rd party developed Collection implementation that, for some reason, violates the “count is O(1)” rule

That is not a rule for Collection.

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [March 6, 2018, 11:30pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/18 "2018-03-06T23:30:01Z")

</div>

Yeah... I meant `isEmpty`. Sorry =)

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [March 6, 2018, 11:31pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/19 "2018-03-06T23:31:32Z")

</div>

I'm not sure if I understand you here. If you've violated `isEmpty` being O(1), can you really fault Swift for breaking other guarantees?

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [March 6, 2018, 11:31pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/20 "2018-03-06T23:31:37Z")

</div>

> [@moiseev](#):
>
> I meant isEmpty.

Then that third-party type does not actually conform to `Collection`, and its implementor was lying when they added that conformance.

Protocols are emphatically \*not\* bags of syntax. They have semantic requirements, and a type must meet those requirements in order to conform.

`Collection.isEmpty` must be O(1) or else the type does not conform.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 6, 2018, 11:32pm UTC](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613/21 "2018-03-06T23:32:25Z")

</div>

> [@moiseev](#):
>
> This, arguably artificial, situation is easily avoidable by just returning 0 all the time.

It's artificial, but it's also hugely difficult to debug because the compiler can't help you with complexity requirements.

Given @CTMacUser's recent demonstration of difficulties with conforming to complex protocols and finding unintentional behavior from default implementations, it seems a terribly bad idea to intentionally add to the number of potential footguns.

[Next page](https://forums.swift.org/t/require-sequence-underestimatedcount-be-o-1/10613.md?page=2)
