Skip to content

impls on arrays seem slower than equivalent tuples #80140

Description

@smmalis37

When running the following program:

Cargo.toml:

[dependencies]
criterion = "0.3"

main.rs:

use criterion::{black_box, Criterion};

fn main() {
    let mut c = Criterion::default();
    c.bench_function("partialeq-array", |b| {
        b.iter(|| {
            let a = black_box([0; 3]);
            let b = black_box([0; 3]);
            for _ in 0..1_000_000 {
                black_box(a.eq(&b));
            }
        })
    });
    c.bench_function("partialeq-tuple", |b| {
        b.iter(|| {
            let a = black_box((0, 0, 0));
            let b = black_box((0, 0, 0));
            for _ in 0..1_000_000 {
                black_box(a.eq(&b));
            }
        })
    });
}

I get these results in debug builds:

partialeq-array         time:   [46.308 ms 46.462 ms 46.618 ms]
partialeq-tuple         time:   [31.133 ms 31.230 ms 31.339 ms]

And these results in release builds:

partialeq-array         time:   [628.51 us 633.17 us 638.23 us]
partialeq-tuple         time:   [211.77 us 212.61 us 213.60 us]

I had expected these to be equivalent, but tuples are significantly faster than arrays. This seems to apply to any size of number (i8, u32, etc), and some other traits as well (notably Hash).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions