Skip to content

Optimise mrb_iv_get - #6294

Merged
matz merged 2 commits into
mruby:masterfrom
auroranockert:optimise-obj-iv-p
Jun 30, 2024
Merged

Optimise mrb_iv_get#6294
matz merged 2 commits into
mruby:masterfrom
auroranockert:optimise-obj-iv-p

Conversation

@auroranockert

Copy link
Copy Markdown
Contributor

After Baltic Ruby and getting to talk to people about mruby there I decided to put some work in and started looking at how I could contribute to the project!

I started running optcarrot to find easy to fix perfomance issues in the profiler, and managed to make mrb_iv_get about 20% faster which in total speeds up that particular benchmark by about 2% on my M2 Mac Mini with only minor code changes. But since that code path should be hot in most cases it is probably worth it?

The two core changes are as follows

  1. Rearrange MRB_VTYPE_FOREACH so that the compiler can convert obj_iv_p into a range check, this seems to be the only place where this makes a big difference to performance so it felt worth it.
  2. Make a special variant of mrb_type for use in obj_iv_p that ignores boxed types since they cannot have instance variables anyhow.

I am not sure if mrb_unboxed_type is a good name though, I feel like it isn't but it might be good enough?

There are small gains with NaN boxing too, but they are a bit smaller.

Before patches, word boxing (M2 Mac Mini, macOS 14.5)

Five normal runs

fps: 32.9310863275488
fps: 32.9696898237091
fps: 32.9290379935791
fps: 32.9442253316467
fps: 32.930724759119

Three instrumented runs

The performance counters from Instruments here are not very useful to be honest but I included them since I pulled them out anyhow. Note that the percentages are the percentage of the total in the entire run.

Total Self Cycles L1D Load Miss L1D Store Miss Branch miss Load µops Dispatch stall Symbol
1.01 s 14.0% 256.00 ms 2965530328 14.0% 1332376 3.5% 1214577 4.3% 5951241 13.6% 4637217163 14.3% 262544997 12.2% mrb_iv_get
961.00 ms 13.1% 273.00 ms 2709470377 12.7% 1175692 3.1% 1239348 4.3% 5471383 12.6% 4259931402 13.1% 283538200 12.0% mrb_iv_get
1.04 s 14.5% 260.00 ms 3026314990 14.5% 1106310 2.9% 1104159 3.9% 6374836 14.6% 4780069212 14.9% 247543986 12.2% mrb_iv_get

After patches about 2% faster, word boxing (M2 Mac Mini, macOS 14.5)

Five normal runs

fps: 33.4577465475324
fps: 33.3409894683935
fps: 33.3799416005552
fps: 33.142213293828
fps: 33.2542619427094

Three instrumented runs

Total Self Cycles L1D Load Miss L1D Store Miss Branch miss Load µops Dispatch stall Symbol
797.00 ms 11.1% 797.00 ms 2263684364 11.4% 955245 2.8% 913663 3.5% 4727932 11.4% 3629954349 11.8% 237139177 10.4% mrb_iv_get
796.00 ms 11.2% 796.00 ms 2280738010 10.8% 969872 2.5% 817803 2.9% 4908663 11.0% 3653639613 11.1% 234380474 9.8% mrb_iv_get
799.00 ms 11.2% 799.00 ms 2277257603 11.0% 887822 2.3% 812048 2.8% 4803468 10.8% 3663173470 11.3% 226641653 9.7% mrb_iv_get

Ps. Thanks everyone for the cool work on mruby!

This allows the compiler to optimise the case in obj_iv_p into a range check. There does not seem to be any other very hot uses of this index and it grants a pretty big gain on optcarrot.
By adding a fast-path where we ignore boxed types we can gain a pretty substantial speedup of mrb_iv_get, making it about 25% faster during a standard optcarrot benchmark run.

NOTE: It is just mrb_iv_get that is that much faster, the whole benchmark seems to be about 3-5% faster with word boxing.
@auroranockert
auroranockert requested a review from matz as a code owner June 27, 2024 21:11
@github-actions github-actions Bot added the core label Jun 27, 2024
@matz
matz merged commit 7333713 into mruby:master Jun 30, 2024
@matz

matz commented Jun 30, 2024

Copy link
Copy Markdown
Member

Thank you!!!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants