Skip to content

Optimise immutable.Queue for head/tail #11396

Open
@NthPortal

Description

@NthPortal

Optimise immutable.Queue so that head/tail usage pattern does not cause double traversal of in.

Take the following usage pattern:

var queue: Queue[Int] = ???
while (queue.nonEmpty) {
  val elem = queue.head
  queue = queue.tail
  // do something with `elem` here
}

If out is empty and in is not, val elem = queue.head will require traversing in to find its last element, and queue = queue.tail will require reversing in (which requires traversal).

immutable.Queue's methods should be optimised so that when dequeueing elements, it does not leave out empty (unless in is also empty), and when enqueueing elements, it puts at least one element in out if both in and out are empty (although once you're checking, might as well put everything in out).


This change should be binary compatible, as it only requires changes to class internals.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions