Skip to content

s.c.m.Shrinkable#subtractAll could return early once this is known to be empty. #11269

Open
@joshlemer

Description

@joshlemer

The scala.collection.mutable.Shrinkable[T] trait defines a method

  def subtractAll(xs: collection.IterableOnce[A]): this.type = {
    @tailrec def loop(xs: collection.LinearSeq[A]): Unit = {
      if (xs.nonEmpty) {
        subtractOne(xs.head)
        loop(xs.tail)
      }
    }
    xs match {
      case xs: collection.LinearSeq[A] => loop(xs)
      case xs => xs.iterator.foreach(subtractOne)
    }
    this
  }

however this could be optimized in the case where it is an Iterable that is implementing Shrinkable. In that case, after each removal, we could check if we are known to be empty (via knownSize or sizeCompare). If we are known empty, we can return early.

Does this warrant something like a trait IterableOptimizedShrinkable[T] extends Iterable[T] with Shrinkable[T]? I would say probably. From a quick look in the standard library, I think every single implementation of shrinkable is in fact an Iterable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions