Skip to content

Use concrete collection types for private fields and variables #1201

@paulirwin

Description

@paulirwin

Is there an existing issue for this?

  • I have searched the existing issues

Task description

I noticed when reviewing #1200 that the existing code (example) sometimes uses interfaces like IDictionary<TKey, TValue> for the type of private fields and variables, instead of the concrete type. While this matches upstream and helps ensure you're coding to the contract rather than the implementation, this might be accidentally harming performance.

By typing the field/variable to the interface, we lose out on some optimizations like how OrderedDictionary uses a struct enumerator to avoid an allocation/boxing. Also, the JIT can more aggressively inline, eliminate bounds checks, vectorize, etc. when the type is concrete.

Microsoft has the CA1859 analyzer specifically to flag this, and this can be used to make finding issues and refactoring easier.

Note that we should only do this for private fields (and potentially other private members, like return types of private methods) as well as variables, so that we are not changing the public contracts. A comment should be added mentioning CA1859, i.e. // LUCENENET: CA1859 - Use concrete types when possible for improved performance

While there is likely benefit beyond collections, let's start small and not "boil the ocean" here. Collections are where there is the biggest benefit due to things like stack allocated enumerators and possible vectorization.

I think this is worth evaluating as a deviation from exactly matching upstream Lucene code. cc @NightOwl888 for thoughts/feedback.

Metadata

Metadata

Labels

good-first-issueGood for newcomershacktoberfestThis is a good issue for Hacktoberfestis:taskA chore to be doneup-for-grabsThis issue is open to be worked on by anyone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions