Skip to content

Minor clean up of some completion code#11609

Merged
davidwengier merged 7 commits intodotnet:mainfrom
davidwengier:CompletionCleanup
Mar 14, 2025
Merged

Minor clean up of some completion code#11609
davidwengier merged 7 commits intodotnet:mainfrom
davidwengier:CompletionCleanup

Conversation

@davidwengier
Copy link
Copy Markdown
Member

Starting to look at cohosting completion resolve and noticed some things that annoyed me :)

@davidwengier davidwengier requested a review from a team as a code owner March 12, 2025 07:22
Comment on lines -77 to -80
if (!slot.Used)
{
break;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's definitely too subtle, but this check is actually meaningful. When the _items array is initialized, it is filled with Slot structs containing all default values. So, if the _items slot hasn't been set yet, Used will be false indicating that there's no reason to continue searching.

The Used property is definitely a clumsy way to do this. Consider re-adding this test but change it to slot.CompletionList is null with a comment. It looks like CompletionListCache.Add(...) guarantees that CompletionList will be non-null for _items slots that have been set, so that should be functionally equivalent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you for saying its subtle, because I definitely missed it! Glad our forebearers were there with uncommented code to prevent us iterating up to 9 times. Will fix.

There should probably be another test too, as this would have caused a null ref which clearly isn't covered.

Copy link
Copy Markdown
Member

@DustinCampbell DustinCampbell Mar 13, 2025

Choose a reason for hiding this comment

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

Honestly, I think it was me who didn't comment it. 😄 #stillgrowing

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay, I was wrong about the null ref, so didn't bother writing a test. I could artificially introduce a problem (ie, call .AssumeNotNull() on a non-nullable property, but it seems silly when this loop is pretty quick even if left unattended. There is a test that already covers the completely uninitialized state of the cache, so we'll know if we accidentally introduce an infinite loop or something.

Comment on lines +21 to +22
[return: NotNullIfNotNull(nameof(razorCompletionList))]
[return: NotNullIfNotNull(nameof(delegatedCompletionList))]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💘

Comment on lines +69 to +71
// CompletionList is annotated as non-nullable, but we are allocating an array of 10 items for our cache, so initially
// those array entries will be default. By checking for null here, we detect if we're hitting an unused part of the array
// so stop looping.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lovely! 💖

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.

3 participants