Minor performance improvements to the BaseUnits class (v5) - #1452
Conversation
- avoid storing the IsFullyDefined value to a field (the property is only used when constructing a UnitSystem) - add a reference check to the Equals method (improves the == Undefined) - use the HashCode class for the hashcode (note that the result would be different between Framework and NET projects)
lipchev
commented
Dec 11, 2024
If you think the 3rd point is a "breaker", I could switch it over to v6 but I find it very unlikely that this would affect anyone (I'm frankly not even sure if the same code would produce the same result on both targets 🤷 ). I'd like to change the publicoverridestringToString(){if(Equals(Undefined)){return"Undefined";}returnstring.Join(", ",GetUnitsDefined());IEnumerable<string>GetUnitsDefined(){if(Lengthis not null){yieldreturn$"[Length]: {Length}";}if(Massis not null){yieldreturn$"[Mass]: {Mass}";}if(Timeis not null){yieldreturn$"[Time]: {Time}";}if(Currentis not null){yieldreturn$"[Current]: {Current}";}if(Temperatureis not null){yieldreturn$"[Temperature]: {Temperature}";}if(Amountis not null){yieldreturn$"[Amount]: {Amount}";}if(LuminousIntensityis not null){yieldreturn$"[LuminousIntensity]: {LuminousIntensity}";}}}The difference is that we're not using the |
angularsen
commented
Dec 15, 2024
Looks fine to me, I don't think this should break anyone or at least extremely few. Regarding the ToString change, beware that Of course, we're talking micro optimizations here, but also no real downside to that approach. |
- `BaseUnits`: no longer using the `AbbeviationsCache`, the new format is `L=Meter, M=Kilogram, T=Second` - `BaseDimensions`: the exponent moved inside the dimension-brackets: `[Length][Time^-1]` - `BaseDimensions`: minor performance improvements As mentioned in #1452, the main motivation here is the removal of the potential side effects of accessing/loading the unit abbreviations (e.g. during the `QuantityInfo` construction)
IsFullyDefinedvalue to a field (the property is only used when constructing aUnitSystem)Equalsmethod (improves the == Undefined)HashCodeclass for theGetHashCodemethod (note that the result would be different between Framework and NET projects)