Entitas-csharp: [CodeGenerator:] Cache static component index lookup into local var

Created on 8 Mar 2017  路  7Comments  路  Source: sschmid/Entitas-CSharp

Example:

// Before
public void AddPosition(IntVector2 newValue) {
    var component = CreateComponent<PositionComponent>(CoreComponentsLookup.Position);
    component.value = newValue;
    AddComponent(CoreComponentsLookup.Position, component);
}

// After
public void AddPosition(IntVector2 newValue) {
    var index = CoreComponentsLookup.Position;
    var component = CreateComponent<PositionComponent>(index);
    component.value = newValue;
    AddComponent(index, component);
}
enhancement

Most helpful comment

The difference is that the new variable index is used twice. The compiler won't reuse the same variable for that as it doesn't know what CoreComponentsLookup.Position does. It could have side effects for example.

All 7 comments

Welcome to the world of micro-optimizations

Is that not the same thing? How is that faster?

The difference is that the new variable index is used twice. The compiler won't reuse the same variable for that as it doesn't know what CoreComponentsLookup.Position does. It could have side effects for example.

Ooh. Yes I know that. I overlooked the second use of the lookup variable lol. faster by 1 statement. 馃憤

It'd be cool to see a benchmark and how those changes impact the performance :D

Feel free to do one :)

I don't expect any real life gains unless you call those methods reeeeeaaaly often. I'd call it code maintenance...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FNGgames picture FNGgames  路  5Comments

vaudevillian picture vaudevillian  路  3Comments

zhuchun picture zhuchun  路  4Comments

sschmid picture sschmid  路  4Comments

moixxsyc picture moixxsyc  路  4Comments