Skip to content

Optimize FieldAccessor - #335

Merged
SergeiPavlov merged 4 commits into
master-servicetitanfrom
fieldAccessor
Jan 15, 2025
Merged

Optimize FieldAccessor#335
SergeiPavlov merged 4 commits into
master-servicetitanfrom
fieldAccessor

Conversation

@SergeiPavlov

@SergeiPavlov SergeiPavlov commented Jan 14, 2025

Copy link
Copy Markdown
Collaborator

This class is critical for the performance.

Most of them are value-type accessors (DefaultFieldAccessor<>).
Using FieldIndex field instead of FieldInfo.MappingInfo.Offset expression will save one pointer dereference.

@snaumenko-st snaumenko-st left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to have some benchmark for this change.

BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
null, Array.Empty<object>(), null);
accessor.Field = field;
accessor.SetFieldInfo(field);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud: maybe we can convert all these accessors to structs and get rid of virtual call dispatching as well? E.g. make them generic arguments of FieldAccessor
Could it be possible?

And maybe we can replace SetFieldInfo with single ctor argument?

@SergeiPavlov SergeiPavlov Jan 15, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation .SetFieldInfo() invoked only once, during Domain building
Little profit of optimizing it.

And I don't see easy way to use Accesor.GetValue()/SetValue() without virtual functions.
It would be to emulate vtbl by some other means

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean not only this operation is worth optimizing. But others as well. These accessors seem to be good candidates for compiler optimizations with devirtualizing. There is not much of shared code and actually there is little sense to use inheritance.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had an idea to inherit *Accessor from FieldInfo
because they are always together and mutually referenced
that will save yet one pointer dereference during access

@SergeiPavlov
SergeiPavlov merged commit da22ea2 into master-servicetitan Jan 15, 2025
@SergeiPavlov
SergeiPavlov deleted the fieldAccessor branch January 15, 2025 10:48
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.

3 participants