Skip to content

Look for more opportunities to fold_with and consume_iter #465

@cuviper

Description

@cuviper

These functions have reasonable default implementations, but sometimes they can be improved.

  • The default Producer::fold_with just converts to an iterator and passes that to the Folder.
    • ChainProducer::fold_with does better by folding its two parts separately, which can reduce conditional branching. (This is much like the motivation for the custom std::iter::Chain::fold.)
    • Most of the other producers that wrap a base type could probably have a custom fold_with too, if only to allow a chain base type to optimize itself as before.
      • e.g. ClonedProducer::fold_with could call self.base.fold_with(ClonedFolder { base: folder })
  • The default Folder::consume_iter just picks off items one by one to consume() them until full(). (If it weren't for that "full" short-circuiting, it could just call Iterator::fold instead.)
    • SumFolder::consume_iter just calls the standard Iterator::sum(). (which actually is a fold()... here we know it will never be full.)
    • There may be other folders which can do a better job in consume_iter too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions