How to fast get reference to an element among the tree? when reference and enum involved

I have a tree, and 2 functions, one using recursive functions:

one maintaining a Cell and using a loop:

I made a simple bench function, the as the result, it's faster to use loop,

loop_get                time:   [20.444 us 20.667 us 20.886 us]
                        change: [-3.2910% -1.7348% -0.2509%] (p = 0.04 < 0.05)
                        Change within noise threshold.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

ref_get                 time:   [30.671 us 30.842 us 31.007 us]
                        change: [-10.065% -9.5309% -8.9736%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

however, the story does not end here. I use this library in my project, which uses a large enum(see below), it's significantly slower(results from running script on Mac and Ubuntu):

I don't have full benchmark for that, only some perf and flamegraph(sorry I don't have accurate and stable results except for the perf on Ubuntu... I can't analyze ASM by myself as well).

Is there any reason or tricks why the function fast using usize turned slow using on references large enum?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.