apple / swift-collections

Commonly used data structures for Swift
Apache License 2.0
3.8k stars 302 forks source link

Is _BTree defaultLeafCapacity correct? #257

Open jessegrosjean opened 1 year ago

jessegrosjean commented 1 year ago

I assume I'm wrong here, asking for education mostly :)

The code looks like this:

/// Recommended node size of a given B-Tree
@inlinable
@inline(__always)
internal static var defaultLeafCapacity: Int {
  #if DEBUG
  return 5
  #else
  let capacityInBytes = 2000
  return Swift.min(16, capacityInBytes / MemoryLayout<Key>.stride)
  #endif
}

From my reading the max leaf capacity is 16? Is that right? I would expect (without really understanding much of the code here) performance to be better if leaves could contain more then 16 elements when element size is small.

lorentey commented 1 year ago

Hm, I think so -- it looks like this should use Swift.max, not Swift.min. The capacity in bytes also looks a little low to me -- we'll probably want to increase it to something like 16kiB or so as part of the performance work that'll precede its release.

(Beware, the SortedCollections module is not yet ready for production use, and its API may see source-breaking changes before it ships.)