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

Convert Hashtable usage to Dictionary<TKey, TValue #8143

Open
elachlan opened this issue Nov 9, 2022 · 9 comments
Open

Convert Hashtable usage to Dictionary<TKey, TValue #8143

elachlan opened this issue Nov 9, 2022 · 9 comments
Labels
enhancement Product code improvement that does NOT require public API changes/additions tenet-performance Improve performance, flag performance regressions across core releases

Comments

@elachlan
Copy link
Contributor

elachlan commented Nov 9, 2022

Is your feature request related to a problem? Please describe

Hashtable has been used all over the winforms code base.

Describe the solution you'd like and alternatives you've considered

Refactoring to Dictionary<TKey, TValue is explicit in its typing and avoids boxing.

The changes should not affect Public APIs.

System.Windows.Forms: 31 references
System.Windows.Forms.Design: 126 references

Related: #8140, #2644

@elachlan
Copy link
Contributor Author

elachlan commented Nov 11, 2022

HelpProvider has a few Hashtables. _boundControls a bit weird since the key and value are the same and of type Control.

private readonly Hashtable _boundControls = new Hashtable();

I thought this might be a good use of Hashset<T>, but this suggests the performance isn't as good as List<T> for objects until after 20 items. https://stackoverflow.com/a/10762995/908889

Does the team have a suggestion here?

@elachlan
Copy link
Contributor Author

@Jericho, you might want to jump in on this issue as well.

@Jericho
Copy link
Contributor

Jericho commented Nov 15, 2022

@elachlan sure. As soon as I'm done working on #8140, I'll inventory the remaining HashTable and create a list to track progress (like I did for the ArrayList).

@elachlan
Copy link
Contributor Author

I attempted WeakHashtable. It is a complicated one. I am unsure how to proceed.

Maybe add a type parameter and implement IDictionary<object, TValue>? That would allow using a private Dictionary<EqualityWeakReference, TValue>, thus hiding the implementation and avoiding boxing.

@merriemcgaw merriemcgaw added the enhancement Product code improvement that does NOT require public API changes/additions label Nov 16, 2022
@Jericho
Copy link
Contributor

Jericho commented Nov 17, 2022

I'm done with #8140 for the time being so I'll start contributing here. First priority: create a list of the classes that need to be refactored (similar to what I did here). This will enable us to track our progress.

@Jericho
Copy link
Contributor

Jericho commented Nov 17, 2022

Here's the list of classes that contains at least one private property of type HashTable:

@elachlan
Copy link
Contributor Author

elachlan commented Dec 3, 2022

ImmutablePropertyDescriptorGridEntry.PropertyValue is something I am not 100% sure on. It creates an HashTable into an IDictionary variable, then passes that to TypeConverter.CreateInstance. I imagine the underlying converter probably relies on it being a hashtable? Anyone have any ideas on where the TypeConverter code is located?

IDictionary values = new Hashtable(properties.Count);
object newObject;
for (int i = 0; i < properties.Count; i++)
{
if (PropertyDescriptor.Name is not null && PropertyDescriptor.Name.Equals(properties[i].Name))
{
values[properties[i].Name] = value;
}
else
{
values[properties[i].Name] = properties[i].GetValue(owner);
}
}
try
{
newObject = parentConverter.CreateInstance(parentEntry, values);
}

@halgab
Copy link
Contributor

halgab commented Mar 12, 2023

I see that ContextMenuStripGroupCollection, which is internal, overrides DictionaryBase, which is basically just a wrapper around a HashTable with extra events we're not using. As part of this issue, do you agree it would be a good idea for this class to use a Dictionary<string, ContextMenuStripGroup> directly instead?

@elachlan
Copy link
Contributor Author

elachlan commented Jan 4, 2024

@lonitra I believe the remaining usages of HashTable to either be:

  • a part of the Public API
  • used for Binary Serializer purposes
  • passed to public APIs defined as IDictionary. (maybe we can use a dictionary adapter here?)

Could the team verify that the remaining items can't be refactored, maybe we can close this issue off?

@merriemcgaw merriemcgaw removed the api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Product code improvement that does NOT require public API changes/additions tenet-performance Improve performance, flag performance regressions across core releases
Projects
None yet
Development

No branches or pull requests

5 participants