Skip to content

Commit

Permalink
PERF: dispatch on UnitSystem value in _get_multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Nov 23, 2024
1 parent ec2c559 commit 91bc0ab
Showing 1 changed file with 32 additions and 17 deletions.
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 @@ Default behavior of a component. If there is no base_power field, assume is in t
"""
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
error("Undefined Conditional")

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

0 comments on commit 91bc0ab

Please sign in to comment.