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)
|
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 public override string ToString()
{
if (Equals(Undefined))
{
return "Undefined";
}
return string.Join(", ", GetUnitsDefined());
IEnumerable<string> GetUnitsDefined()
{
if (Length is not null)
{
yield return $"[Length]: {Length}";
}
if (Mass is not null)
{
yield return $"[Mass]: {Mass}";
}
if (Time is not null)
{
yield return $"[Time]: {Time}";
}
if (Current is not null)
{
yield return $"[Current]: {Current}";
}
if (Temperature is not null)
{
yield return $"[Temperature]: {Temperature}";
}
if (Amount is not null)
{
yield return $"[Amount]: {Amount}";
}
if (LuminousIntensity is not null)
{
yield return $"[LuminousIntensity]: {LuminousIntensity}";
}
}
}The difference is that we're not using the |
|
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)