Skip to content

Take advantage of type uniformity in _bisect.bisect() #96538

Closed
@sweeneyde

Description

@sweeneyde

We can make bisect.bisect(seq, i) get roughly 1.3x faster by taking advantage of type stability, of both the sequence and the keys.

Possible opportunities:

  • All calls to PySequence_GetItem should use the same underlying tp->tp_as_sequence->sq_item
  • We never call with i < 0, so no need to adjust indices
  • Similar to the strategy employed in list.sort()'s unsafe_object_compare, we can keep a tp_richcompare function pointer around, then check types and use it, with less type-checking. We can always fall back to PyObject_RichCompareBool in case some assumption fails.
  • Recursion checks only need to happen once per bisect call.

Much of this work can be moved outside the loop, so that instead of happening lg(n) times, it only needs to happened once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions