Skip to content

Conversation

@Wi1l-B0t
Copy link
Contributor

@Wi1l-B0t Wi1l-B0t commented Jun 27, 2025

Description

When set a value to map, The old value reference removed in Line 55 or the new key reference added in Line 57.

But if Line 60 throws a InvalidOperationException, the reference count will be wrong,
because the ReferenceCounter has been updated, but the internal collection not.

if (ReferenceCounter != null)
{
if (dictionary.TryGetValue(key, out StackItem? old_value))
ReferenceCounter.RemoveReference(old_value, this);
else
ReferenceCounter.AddReference(key, this);
if (value is CompoundType { ReferenceCounter: null })
{
throw new InvalidOperationException("Can not set a Map without a ReferenceCounter.");
}
ReferenceCounter.AddReference(value, this);

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link
Contributor

@roman-khimov roman-khimov left a comment

Choose a reason for hiding this comment

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

#3693, please...

IIUC correctly bytecode triggering this will make VM fail, so the exact count is not much relevant in this case. But worth fixing anyway.

}
}

foreach (var item in _array)
Copy link
Member

Choose a reason for hiding this comment

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

Why don't use the same foreach?

Copy link
Contributor Author

@Wi1l-B0t Wi1l-B0t Jun 28, 2025

Choose a reason for hiding this comment

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

Why don't use the same foreach?

For example:
The _array has 2 items, and the first added, but the second is invalid.
Line 86 throws a InvalidOperationException, and then the ReferenceCounter is wrong.

Copy link
Contributor Author

@Wi1l-B0t Wi1l-B0t Jun 28, 2025

Choose a reason for hiding this comment

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

Why don't use the same foreach?

This issue shouldn't be triggered in real environments, but it’s better to be cautious.

Copy link
Member

Choose a reason for hiding this comment

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

If throw an exceptiotge execution is halted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If throw an exceptiotge execution is halted

Make it more rigorous.

Copy link
Member

Choose a reason for hiding this comment

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

If throw an exceptiotge execution is halted

Make it more rigorous.

Slower without need

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If throw an exceptiotge execution is halted

Make it more rigorous.

Slower without need

It's so trivial.
Few items in array in most cases.
And after the first foreach, the code will be hot(cache and branch predictor), it should be fast enough.

Copy link
Member

Choose a reason for hiding this comment

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

No need, it will be a fault, have no sense

@Wi1l-B0t Wi1l-B0t closed this Jun 28, 2025
@Wi1l-B0t Wi1l-B0t deleted the fix.reference-count-in-Map branch June 30, 2025 13:35
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.

6 participants