Summary : Technically speaking, underestimatedCount owned by Sequence and inherits Collection and Dictionary . Dictionary does not override the default implementation, which returns zero.
Looking at the source code, it seems that underestimatedCount used as an indicator to determine the growth rate of a volatile collection when new items are added.
Here is a snippet from StringCore.Swift :
public mutating func append<S : Sequence>(contentsOf s: S) where S.Iterator.Element == UTF16.CodeUnit { ........... let growth = s.underestimatedCount var iter = s.makeIterator() if _fastPath(growth > 0) { let newSize = count + growth let destination = _growBuffer(newSize, minElementWidth: width)
Similar, from StringCharacterView.swift :
public mutating func append<S : Sequence>(contentsOf newElements: S) where S.Iterator.Element == Character { reserveCapacity(_core.count + newElements.underestimatedCount) for c in newElements { self.append(c) } }
Or better yet, from Arrays.swift.gyb :
public mutating func append<S : Sequence>(contentsOf newElements: S) where S.Iterator.Element == Element { let oldCount = self.count let capacity = self.capacity let newCount = oldCount + newElements.underestimatedCount if newCount > capacity { self.reserveCapacity( Swift.max(newCount, _growArrayCapacity(capacity))) } _arrayAppendSequence(&self._buffer, newElements) }
Oddly enough, I could only find one implementation for underestimatedCount , in Sequence , and that returns zero.
Currently, it seems that underestimatedCount is more important for custom collection / sequence implementations, as Apple already has a good idea about growing those for standard Swift collections.
Cristik
source share