Skip to content

Is there a way we can lazy load interface mappings? #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JROBOTO opened this issue Sep 23, 2024 · 7 comments
Closed

Is there a way we can lazy load interface mappings? #74

JROBOTO opened this issue Sep 23, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@JROBOTO
Copy link

JROBOTO commented Sep 23, 2024

Some of the objects we are attempting to map can be quite big however we rarely need every property of every bit. Would it be possible to adapt the functionality so that, internally, we only map the property of the instance to its interface if that interface is called upon? I have a function I am calling where the API takes 0.08 seconds however the resulting mapping takes ~1 minute for 12000 items in the resulting collection.

For example:
If I have object

public class Thing
{
    public OtherObject SubThing { get; }
}

If I had a function returning the mapped IThing, the SubThing property would not be mapped until I explicitly asked for it.

Purely hypothetical of course

@StefH StefH self-assigned this Sep 23, 2024
@StefH
Copy link
Owner

StefH commented Sep 23, 2024

@JROBOTO
At this moment when I have these classes:

public class Person : Human
{
  public Human Human { get; set; }
}

public class Human
{
  public bool IsAlive { get; set; }
}

The generated code for the Human property is :

public global::ProxyInterfaceSourceGeneratorTests.Source.IHuman Human { get => Mapster.TypeAdapter.Adapt<global::ProxyInterfaceSourceGeneratorTests.Source.IHuman>(_Instance.Human); set => _Instance.Human = Mapster.TypeAdapter.Adapt<ProxyInterfaceSourceGeneratorTests.Source.Human>(value); }

Which would be changed to support Lazy?

@StefH StefH added the enhancement New feature or request label Sep 23, 2024
@JROBOTO
Copy link
Author

JROBOTO commented Sep 24, 2024

Ok just had a deeper look and its not the actual properties but the TypeAdapterConfig being generated in each constructor that is causing it to be slow especially if a class has a lot of subclasses proxied.

In my example. constructing each proxy takes about 4ms however for 15000 objects in total, that adds up

@StefH
Copy link
Owner

StefH commented Sep 24, 2024

I do not understand yet how to build this and if this is possible using mapster.

As a possible quick fix, I've defined the mappings in a static constructor, so it's only executed once.

public partial class PersonProxy : global::ProxyInterfaceSourceGeneratorTests.Source.HumanProxy, global::ProxyInterfaceSourceGeneratorTests.Source.IPerson
    {
        static PersonProxy()
        {
            Mapster.TypeAdapterConfig<global::ProxyInterfaceSourceGeneratorTests.Source.Human, global::ProxyInterfaceSourceGeneratorTests.Source.IHuman>.NewConfig().ConstructUsing(instance2145588841 => new global::ProxyInterfaceSourceGeneratorTests.Source.HumanProxy(instance2145588841));
            Mapster.TypeAdapterConfig<global::ProxyInterfaceSourceGeneratorTests.Source.IHuman, global::ProxyInterfaceSourceGeneratorTests.Source.Human>.NewConfig().MapWith(proxy1567394325 => ((global::ProxyInterfaceSourceGeneratorTests.Source.HumanProxy) proxy1567394325)._Instance);

        }

        // . . .

#76

@JROBOTO
Copy link
Author

JROBOTO commented Sep 24, 2024

Fair enough, I'm not going to pretend to be an expert in Mapster, in fact my only interaction with it is through your library 😆
Static constructors sound like they would resolve the issue nicely

@JROBOTO
Copy link
Author

JROBOTO commented Sep 24, 2024

Not sure what policy you have on who you count as a reviewer on a PR but I have given it a tick

@StefH
Copy link
Owner

StefH commented Sep 24, 2024

I've create a new NuGet (0.3.0) which should be available shortly.

Please test this one, and report back if this helps.

@JROBOTO
Copy link
Author

JROBOTO commented Sep 24, 2024

That's given us an incredible amount of performance, thanks!!!

@JROBOTO JROBOTO closed this as completed Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants