Skip to content

Performance Concerns for Mersenne31Field::as_representative #1031

@JulianGCalderon

Description

@JulianGCalderon

While adding QM31 support to starknet-io/types-rs, some concerns were raised on the performance impact of as_representative(). Original comment: starknet-io/types-rs#163 (comment).

The current implementation uses branching to check if the inner value is equal to MERSENNE_31_PRIME_FIELD_ORDER

fn as_representative(n: &u32) -> u32 {
if *n == MERSENNE_31_PRIME_FIELD_ORDER {
0
} else {
*n
}

Would changing the implementation to be branchless improve performance? Using either:

  • Modulo: n % MERSENNE_31_PRIME_FIELD_ORDER.
  • Mask: n & (1u32 << 31) - 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions