Skip to content

Step::forward/backward_overflowing #767

@pitaj

Description

@pitaj

Proposal

Problem statement

The existing Step functions optimize poorly in certain cases which come up often in real-world usage.

Motivating examples or use cases

We all know the RangeInclusive optimization issue. Often, it results in a measurable decrease in performance when using s..=e instead of s..e+1. I've discovered that switching from Step::forward* to overflowing_add convinces LLVM that the bool field is actually just an extra bit. This allows many of the same optimizations that occur on the normal Range type.

godbolt

Solution sketch

trait Step: ... {
    // ... existing functions

    // New required functions
    fn forward_overflowing(start: Self, count: usize) -> (Self, bool);
    fn backward_overflowing(start: Self, count: usize) -> (Self, bool);
}

Adding these still allows Step to still be compatible with BigInt use-cases, in those cases they will just never overflow. For char, AsciiChar, Ipv4Addr, and Ipv6Addr overflow is still well-defined, even if it is less meaningful it still works for this usecase.

Alternatives

We could instead add the overflowing stuff to TrustedStep, but that would make eventual third-party implementors worse off.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions