Skip to content
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

Avoid reverse SortedSet in MemoryPool #3822

Closed
wants to merge 17 commits into from
Closed

Conversation

shargon
Copy link
Member

@shargon shargon commented Mar 11, 2025

Description

I don't know why we always reverse the iterator in these two sorted set, @roman-khimov could you check these changes please.

This could helps with tps because it improves the memPool management.

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

How Has This Been Tested?

  • UT_ReversePoolItemComparer

Test Configuration:

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

@shargon shargon requested a review from roman-khimov March 11, 2025 17:09
@shargon shargon marked this pull request as ready for review March 11, 2025 17:25
@shargon shargon requested a review from superboyiii March 11, 2025 18:18
@shargon shargon mentioned this pull request Mar 17, 2025
16 tasks
[TestClass]
public class UT_ReversePoolItemComparer : UT_PoolItem
{
public UT_ReversePoolItemComparer() : base(ReversePoolItemComparer.Instance, -1) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

This is empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

Same tests, but reversed

@shargon
Copy link
Member Author

shargon commented Mar 18, 2025

@Jim8y could you help me with benchmarks here?

{
if (otherTx == null) return 1;
int ret = (Tx.GetAttribute<HighPriorityAttribute>() != null).CompareTo(otherTx.GetAttribute<HighPriorityAttribute>() != null);
var ret = (Tx.GetAttribute<HighPriorityAttribute>() != null)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe for the _sortedTransactions there will be an effect, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

This return the same, is inverted later

@Jim8y
Copy link
Contributor

Jim8y commented Mar 24, 2025

Before:

Method Mean Error StdDev Gen0 Allocated
RetrieveSortedTransactions 75.91 us 2.777 us 0.152 us 1.7090 11.16 KB
GetSorted_ReversedSortedSet 124.9 us 170.7 us 9.36 us 12.4512 1.9531
AddTransactions 75.25 us 6.104 us 0.335 us 1.4648 9.38 KB
GetSorted_DirectSortedSet 449.5 us 84.08 us 4.61 us 51.2695 31.2500

After:

Method Mean Error StdDev Gen0 Allocated
RetrieveSortedTransactions 84.79 us 2.614 us 0.143 us 1.7090 11.16 KB
GetSorted_ReversedSortedSet 130.3 us 16.48 us 0.90 us 12.4512 1.9531
AddTransactions 79.59 us 0.965 us 0.053 us 1.4648 9.38 KB
GetSorted_DirectSortedSet 426.6 us 60.68 us 3.33 us 50.7813 30.7617

I dont see much performance difference, basically the same.

Jim8y
Jim8y previously approved these changes Mar 24, 2025
@shargon
Copy link
Member Author

shargon commented Mar 24, 2025

Before:

Method Mean Error StdDev Gen0 Allocated
RetrieveSortedTransactions 75.91 us 2.777 us 0.152 us 1.7090 11.16 KB
GetSorted_ReversedSortedSet 124.9 us 170.7 us 9.36 us 12.4512 1.9531
AddTransactions 75.25 us 6.104 us 0.335 us 1.4648 9.38 KB
GetSorted_DirectSortedSet 449.5 us 84.08 us 4.61 us 51.2695 31.2500
After:

Method Mean Error StdDev Gen0 Allocated
RetrieveSortedTransactions 84.79 us 2.614 us 0.143 us 1.7090 11.16 KB
GetSorted_ReversedSortedSet 130.3 us 16.48 us 0.90 us 12.4512 1.9531
AddTransactions 79.59 us 0.965 us 0.053 us 1.4648 9.38 KB
GetSorted_DirectSortedSet 426.6 us 60.68 us 3.33 us 50.7813 30.7617
I dont see much performance difference, basically the same.

Seems slower? 🫣

private static NeoSystem MockNeoSystem()
{
// Use reflection to create a minimal NeoSystem without initializing all components
var constructor = typeof(NeoSystem).GetConstructor(
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to use Internal Visiable To?

/// <summary>
/// A class that mimics the behavior of the internal PoolItem for MemoryPool benchmarks
/// </summary>
public class TestMemPoolItem : IComparable<TestMemPoolItem>
Copy link
Contributor

Choose a reason for hiding this comment

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

Duplicated PoolItem.
Is it possible to use Internal Visiable To?(Same as calling MemoryPool internal methods)

@shargon
Copy link
Member Author

shargon commented Mar 31, 2025

Closed because no speed up

@shargon shargon closed this Mar 31, 2025
@shargon shargon deleted the mem-pool-improve branch March 31, 2025 14:13
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.

4 participants