Skip to content
New issue

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

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

Already on GitHub? # to your account

Dictionary and Enumerable Mappings: Call EnsureCapacity if source count is known and target has a EnsureCapacity(int) method #251

Closed
latonz opened this issue Jan 23, 2023 · 3 comments · Fixed by #361
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@latonz
Copy link
Contributor

latonz commented Jan 23, 2023

If Mapperly generates a foreach / Add mapping the capacity of the target is increased as needed.
If the source/target count is known (type does have a Count/ Length property or Enumerable.TryGetNonEnumeratedCount() returns true) and the target type does have a Method EnsureCapacity(int), EnsureCapacity(source.Count + target.Count) can be called to ensure the target collection has a big enough capacity for all source objects plus the existing target objects and does not need to resize as mapped objects are added.

@latonz latonz added the enhancement New feature or request label Jan 23, 2023
@Blackclaws
Copy link

This is already done via constructor arguments for dictionaries and lists I think. Calling EnsureCapacity there would be redundant

@latonz
Copy link
Contributor Author

latonz commented Feb 5, 2023

You are correct, if the target is created by Mapperly via new and the target type is a List or Dictionary. But if the target instance already exists, it is created via an object factory or is any type implementig IDictionary<,> or IList<> but is not Dictionary/List (eg. a custom List implementation), the capacity is not ensured.

@github-actions
Copy link

🎉 This issue has been resolved in version 2.8.0-next.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants