Skip to content

Optimize PersistentGenericBag.EqualsSnapshot #2399

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

Merged
merged 5 commits into from
Jun 7, 2020

Conversation

bahusoid
Copy link
Member

@bahusoid bahusoid commented May 25, 2020

Added tests directly calling EqualsSnapshot to verify it works correctly.

Notes: This check is called when NHibernate detects cases when underlying collection can be modified directly in user code and not via IPersistenCollection wrapper, for instance array mappings and PersistentArryaHolder.
For bags it's executed if new instance is created with bag then flushed (so List<Entity> become PersistentGenericBag). Check is executed for all additional flushes in this session as theoretically user can still have instance of original bag List<Entity> not wrapped in PersistentGenericBag that can be modified directly

{
if (CountOccurrences(elt, _gbag, elementType) != CountOccurrences(elt, sn, elementType))
{
if(elementType.IsSame(_gbag[i], sn[i]))
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to count elements placed on the same positions.

Copy link
Member

Choose a reason for hiding this comment

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

New white space glitch after if.

continue;

var elt = _gbag[i];
var countInSnapshot = CountOccurrences(elt, sn, elementType);
Copy link
Member Author

Choose a reason for hiding this comment

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

Added shortcut if element is no longer present in snapshot

@bahusoid bahusoid force-pushed the bagSpanshotEquals branch from 1dca9d9 to b743c9e Compare May 25, 2020 14:49
bahusoid added 3 commits May 25, 2020 18:54
…fied directly in user code and not via IPersistenCollection wrapper, for instance array mappings and PersistentArryaHolder).

This reverts commit a329fdf
Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

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

I also had took a look to other EqualsSnapshot implementation, and I was wondering about optimizing this one.
But I was thinking about something much heavier : building dictionaries of bag elements. Not sure it is worth it.
Excepted for the space glitch, your proposal is ok for me.

{
if (CountOccurrences(elt, _gbag, elementType) != CountOccurrences(elt, sn, elementType))
{
if(elementType.IsSame(_gbag[i], sn[i]))
Copy link
Member

Choose a reason for hiding this comment

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

New white space glitch after if.

@bahusoid
Copy link
Member Author

bahusoid commented Jun 1, 2020

But I was thinking about something much heavier : building dictionaries of bag elements. Not sure it is worth it.

I don't think it's worth for bags. As I tried to explain in PR description this check is rarely executed and when executed in most cases it's unmodified collection. So added IsSame shortcut covers most use cases.

@fredericDelaporte fredericDelaporte merged commit ae4aed2 into nhibernate:master Jun 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants