Skip to content

(Performance) Change static readonly fields to const fields - #4211

Merged
Mag-nus merged 1 commit into
ACEmulator:masterfrom
Flaggs-ACE-Contributions:rf/performance/constant-fields
Aug 24, 2024
Merged

Mag-nus merged 1 commit into
ACEmulator:masterfrom
Flaggs-ACE-Contributions:rf/performance/constant-fields

Conversation

@FlaggAC

@FlaggAC FlaggAC commented Aug 11, 2024

Copy link
Copy Markdown
Contributor

This will at least somewhat improve performance, as confirmed through microbenchmark testing.

const fields are replaced with literal values in the Assembly's IL code, while static readonly fields may be optimized by the JIT, and is not guaranteed to happen. const fields may also lend themselves to stronger optimizations at runtime.

Method is LandDefs.AdjustToOutside.
Baseline (static readonly):

| Method          | Mean     | Error    | StdDev   | Allocated |
|---------------- |---------:|---------:|---------:|----------:|
| AdjustToOutside | 15.90 ns | 0.300 ns | 0.308 ns |         - |

With constants:

| Method          | Mean     | Error    | StdDev   | Allocated |
|---------------- |---------:|---------:|---------:|----------:|
| AdjustToOutside | 13.36 ns | 0.243 ns | 0.227 ns |         - |

This is a 15% improvement for LandDefs.AdjustToOutside. Additionally, I measured through some custom profiler code that LandDefs.AdjustToOutside is called very frequently. So I would estimate this to be a solid improvement.

This will at least somewhat improve performance, as confirmed through microbenchmark testing.

const fields are replaced with literal values in the Assembly's IL code, while static readonly fields may be optimized by the JIT, and is not guaranteed to happen. const fields may also lend themselves to stronger optimizations at runtime.
@gmriggs
gmriggs requested a review from Mag-nus August 11, 2024 05:04
@Mag-nus
Mag-nus merged commit 7f1b58a into ACEmulator:master Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants