-
Notifications
You must be signed in to change notification settings - Fork 23
Performance: refactor unit of measurement storage and lookup #2753
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
base: main
Are you sure you want to change the base?
Conversation
src/units.cpp
Outdated
|
|
||
| static UnitMetadata UnitTable[] { | ||
| // unit label defaultPrec scalable veUnit | ||
| { Victron::VenusOS::Enums::Units_None, "", 1, false, Unit::Default }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use 0 as the default precision for Units_None to avoid confusion. Even if it's a behavioural change it is never used with a precision anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this table could be sorted in alphabetical order (aside from putting Units_None first), to make it easier to read and find values. And for consistency, could do the same for the Units_Type enum in enums.h - this should be fine since there are no dependencies on particular numerical values; the user-preferred units are saved as strings or veutil equivalent values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but whatever the order is in this data structure, the order in the enums.h should be the same. If the table is sorted differently to the enum values, then we can't use the enum value as the index into the array. We could change the datastructure to be a map instead of an array, but honestly for perf reasons I think an array with index lookup is preferable. (We can discuss using std::vector instead of a plain array, shrug.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/units.cpp
Outdated
| { Victron::VenusOS::Enums::Units_Speed_MetresPerSecond, "m/s", 0, false, Unit::MetresPerSecond }, | ||
| { Victron::VenusOS::Enums::Units_Speed_KilometresPerHour, "k/h", 0, false, Unit::KilometresPerHour }, | ||
| { Victron::VenusOS::Enums::Units_Speed_MilesPerHour, "m/h", 0, false, Unit::MilesPerHour }, | ||
| { Victron::VenusOS::Enums::Units_Speed_Knots, "k", 0, false, Unit::Knots }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"k" -> "kn"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/units.cpp
Outdated
| { Victron::VenusOS::Enums::Units_Hertz, "Hz", 1, true, Unit::Default }, | ||
| { Victron::VenusOS::Enums::Units_Energy_KiloWattHour, "kWh", 3, true, Unit::Default }, | ||
| { Victron::VenusOS::Enums::Units_AmpHour, "", 1, true, Unit::Default }, | ||
| { Victron::VenusOS::Enums::Units_WattsPerSquareMetre, "W/m2", 0, false, Unit::Default }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check the scalable values? In main, isScalingSupported() returns true for these:
- Units_WattsPerSquareMetre
- Units_RevolutionsPerMinute
- Units_Speed_MetresPerSecond
- Units_Speed_KilometresPerHour
- Units_Speed_MilesPerHour
- Units_Speed_Knots
- Units_Volume_Litre
- Units_Volume_GallonUS
- Units_Volume_GallonImperial
But in this table, they are false.
I see that you added scaling support for Units_Altitude_Metre, which makes sense, but maybe do that in an extra commit on top to be clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I make a logic judgement on if all the above actually make sense being able to scale in the way the software provides. I have changed it so it replicates behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I didn't realise those changes were all deliberate. In that case, can you mention the changes in the commit so that it's clear that the commit doesn't only make refactoring changes, but also makes changes in the scaling behaviour?
Or alternatively, combine those changes with the Units_Altitude_Metre commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ybott-qinetic ^ I don't see those changes in the latest commit - are you still planning to add them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| return QStringLiteral("W/m2"); | ||
| case VenusOS::Enums::Units_Percentage: | ||
| return QStringLiteral("%"); | ||
| case VenusOS::Enums::Units_Temperature_Celsius: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blammit Is that a build artifact issue? Should I look at fixing how gui_v2 builds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a clean build and I'm still seeing these issues. Hmm - wonder what is going on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified the code manually and reverted it, and now everything is fine, so seems like there was some caching issue there.
src/units.cpp
Outdated
| namespace { | ||
|
|
||
| // \u33A5 is not supported by the font, so use two characters \u006D\u00B3 instead. | ||
| static const QString CubicMetre = QStringLiteral("m³"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the unicode points to be spelled out, e.g. "\u0060\u00B3" in code, rather than the comment. That way the comment can be trimmed to just // \u33A5 is not supported by the font and the code itself becomes self-documenting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/units.cpp
Outdated
| std::string label; | ||
| int precision; | ||
| bool scalable; | ||
| Unit::Type veUnit; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super nitpicky, and maybe a bad suggestion if it degrades readability, but it might be worth considering changing the order pf the struct a bit differently, with std::string first, then the two enums, then the int, then the bool, just for data packing reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/units.cpp
Outdated
|
|
||
| struct UnitMetadata { | ||
| Victron::VenusOS::Enums::Units_Type unit; | ||
| std::string label; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not store as a QString OOI? When we return it, I think we always convert to QString, and it's better to pay the cost once (when constructing the table) rather than on every read, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
79ef992 to
61c336e
Compare
61c336e to
7f96c87
Compare
src/units.cpp
Outdated
| bool scalable; | ||
| }; | ||
|
|
||
| static UnitMetadata UnitTable[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static const, maybe?
also, could be a static const std::vector { ... };
chriadam
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One nitpick, aside from that, LGTM.
But I don't think this should be merged yet, as it's not for v1.2.x series.
7f96c87 to
68e8b8b
Compare
68e8b8b to
c7f8db0
Compare



Fixes #2708