Skip to content

Optimize bindings generator #9

@Hermanest

Description

@Hermanest

The problem

Currently bindings generator makes the following code for a single property:

public Reactive.IState<string> sText {
    set {
        value.ValueChangedEvent += x => obj.Text = x;
    }
}

Take a close look at the code: each call to this property allocates a delegate and a lambda instance which is very expensive.

The solution

Change the ValueChangedEvent signature so that we pass an instance directly via the event, instead of capturing it into the lambda in-place:

interface IState {
    ...

    void AddCallback(object instance, Action<object, T> callback);
    void RemoveCallback(object instance, Action<object, T> callback);
}

public Reactive.IState<string> sText {
    set {
        value.AddCallback(obj, static (x, val) => ((Label)x).Text = val);
    }
}

In this case both the lambda and the delegate are allocated only once and then cached by the compiler. However this method adds a castclass instruction, but the tradeoff is negligible as GC pressure is a more significant problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions