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

Optimize index bounds check for immutable sorted set and list #53266

Merged
merged 4 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,20 @@ internal ref readonly T ItemRef(int index)
{
Requires.Range(index >= 0 && index < this.Count, nameof(index));

return ref ItemRefUnchecked(index);
}

internal ref readonly T ItemRefUnchecked(int index)
{
Debug.Assert(_left != null && _right != null);
if (index < _left._count)
{
return ref _left.ItemRef(index);
return ref _left.ItemRefUnchecked(index);
}

if (index > _left._count)
{
return ref _right.ItemRef(index - _left._count - 1);
return ref _right.ItemRefUnchecked(index - _left._count - 1);
}

return ref _key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,22 @@ internal T this[int index]
internal ref readonly T ItemRef(int index)
{
Requires.Range(index >= 0 && index < this.Count, nameof(index));

return ref ItemRefUnchecked(index);
}

internal ref readonly T ItemRefUnchecked(int index)
{
Debug.Assert(_left != null && _right != null);

if (index < _left._count)
{
return ref _left.ItemRef(index);
return ref _left.ItemRefUnchecked(index);
}

if (index > _left._count)
{
return ref _right.ItemRef(index - _left._count - 1);
return ref _right.ItemRefUnchecked(index - _left._count - 1);
}

return ref _key;
Expand Down