Skip to content

Faster basic tagged integer operations #757

@JukkaL

Description

@JukkaL

The current tagged integer operations are kind of slow, especially things like addition and subtraction. I believe that we can make them significantly faster.

Here are a few ideas:

  • Inline the fast path of the most basic arithmetic operations, such as x + 1, x - 2 and x += 1 (one operand is a constant). Perform tag check, addition/subtraction, and overflow check inline, and if the tag check or overflow check fail, call a function.
    • Alternatively, implement specialized primitives that take a short int as the second operand.
    • Maybe also optimize these for non-constant second operands.
  • Use C extensions such as clang's __builtin_add_overflow for faster overflow checks.
  • Check if two separate tag bit checks ((x & 1) || (y & 1)) are faster or a single check ((x | y) & 1).
  • Inline the fast path of tagged int decref (tag check).
  • Implement some of the hottest operations in assembly, if the code generated by C compilers seems suboptimal (might be worth it, since some of these are very hot operations).
  • Implement optimized boxing operation that only does minimal sanity checks and the fast path is inlined as much as possible (probably requires copying and pasting bunch of code from CPython).

The next steps would be to prototype some ideas and see if they have a significant impact on any benchmarks, and to report results here. After initial experiments we can decide which of the above would be worth a more careful investigation. We need to think about code size / performance tradeoffs.

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