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

PERF: dispatch on UnitSystem value in _get_multiplier #1216

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions src/models/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,38 @@
"""
get_base_power(c::Component) = get_system_base_power(c)

function _get_multiplier(c::T) where {T <: Component}
setting = get_internal(c).units_info
if isnothing(setting)
return 1.0
elseif setting.unit_system == IS.UnitSystem.DEVICE_BASE
return 1.0
elseif setting.unit_system == IS.UnitSystem.SYSTEM_BASE
numerator = get_base_power(c)
denominator = setting.base_value
elseif setting.unit_system == IS.UnitSystem.NATURAL_UNITS
numerator = get_base_power(c)
denominator = 1.0
else
error("Undefined Conditional")
end
return numerator / denominator
end
_get_multiplier(c::T) where {T <: Component} =
_get_multiplier(c, get_internal(c).units_info)

_get_multiplier(::T, ::Nothing) where {T <: Component} =

Check warning on line 13 in src/models/components.jl

View check run for this annotation

Codecov / codecov/patch

src/models/components.jl#L13

Added line #L13 was not covered by tests
1.0
_get_multiplier(c::T, setting::IS.SystemUnitsSettings) where {T <: Component} =
_get_multiplier(c, setting, Val(setting.unit_system))

# PERF: dispatching on the UnitSystem values instead of comparing with if/else avoids the
# performance hit associated with consulting the dictionary that backs the @scoped_enum --
# i.e., IS.UnitSystem.NATURAL_UNITS by itself isn't treated as a constant, it's a dictionary
# lookup each time.
_get_multiplier(

Check warning on line 22 in src/models/components.jl

View check run for this annotation

Codecov / codecov/patch

src/models/components.jl#L22

Added line #L22 was not covered by tests
::T,
::IS.SystemUnitsSettings,
::Val{IS.UnitSystem.DEVICE_BASE},
) where {T <: Component} =
1.0
_get_multiplier(
c::T,
setting::IS.SystemUnitsSettings,
::Val{IS.UnitSystem.SYSTEM_BASE},
) where {T <: Component} =
get_base_power(c) / setting.base_value
_get_multiplier(

Check warning on line 34 in src/models/components.jl

View check run for this annotation

Codecov / codecov/patch

src/models/components.jl#L34

Added line #L34 was not covered by tests
c::T,
::IS.SystemUnitsSettings,
::Val{IS.UnitSystem.NATURAL_UNITS},
) where {T <: Component} =
get_base_power(c)
_get_multiplier(::T, ::IS.SystemUnitsSettings, ::Val) where {T <: Component} =

Check warning on line 40 in src/models/components.jl

View check run for this annotation

Codecov / codecov/patch

src/models/components.jl#L40

Added line #L40 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this one or can let Julia do its normal behavior?

Copy link
Collaborator Author

@GabrielKS GabrielKS Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with letting Julia do its normal behavior on this one.

error("Undefined Conditional")

function get_value(c::Component, value::Float64)
return _get_multiplier(c) * value
Expand Down
Loading