nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

[Mapping by code] In ModelMapper.AddMapping, evaluate to replace Activator.CreateInstance with a compiled lambda to increase performance.

Open pasqualedante opened this issue 4 years ago • 4 comments

In one app, I have 360 mapped types. NH configuration was created as show under:

var map = new ModelMapper();
map.AddMappings(types);

var hbm = map.CompileMappingForAllExplicitlyAddedEntities();
hbm.autoimport = false;

configuration.AddMapping(hbm);

The time to execute the map.AddMappings (types) statement; is approximately 1268 ms and is related to running Activator.CreateInstance 360 times. Maybe by converting Activator.CreateInstance to compiled lambda we can get better performance?

modelmapper

pasqualedante avatar Jun 28 '21 13:06 pasqualedante

is related to running Activator.CreateInstance 360 times. Maybe by converting Activator.CreateInstance to compiled lambda we can get better performance?

I don't think it would improve anything as such lambdas are not reusable - 360 lambdas would need to be compiled for each type.

bahusoid avatar Jun 28 '21 14:06 bahusoid

If you can provide your own instances of the mapping classes, you can call the AddMapping which takes an IConformistHoldersProvider instance.

gliljas avatar Jun 28 '21 16:06 gliljas

Hi @gliljas thanks for the suggestion: can you tell me some documentation about it? I couldn't find any.

pasqualedante avatar Jun 29 '21 08:06 pasqualedante

@gliljas suggestion implies explicit by hand population of mappings somewhere in your code. Something like:

map.AddMapping<YourClassMapping1>();
//or
map.AddMapping(new YourClassMapping2()); //<- That's AddMapping which takes an IConformistHoldersProvider instance. So in theory you can populate somewhere list of such instances add supply it here
// Add all other mappings explicitly...

bahusoid avatar Jun 29 '21 09:06 bahusoid