MQTT message topic parsing optimization

Thanks for the response!
I took the code (expanded for 10 topics) and ran a benchmark comparison and unfortunately it ends up being a bit slower. It does seem like it should be a better solution though.

# @benchmark str = handle_new_message_dict("topic$(rand(1:10))", randstring(16))
"""
julia> @benchmark str = handle_new_message_dict("topic$(rand(1:10))", randstring(16))
BenchmarkTools.Trial: 10000 samples with 195 evaluations.
 Range (min … max):  481.836 ns …  10.591 μs  ┊ GC (min … max): 0.00% … 94.89%
 Time  (median):     511.318 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   562.296 ns ± 451.526 ns  ┊ GC (mean ± σ):  3.52% ±  4.31%

  ▂▆█▆▃▁▁▁▂▂▂▂▁▁                                                ▂
  ██████████████▇▇███▇▆▆▇███▇▇▆▆▇█▆▇▇▅▆▆▅▅▆▅▄▄▃▄▄▄▁▄▅▁▄▃▄▃▅▁▃▄▅ █
  482 ns        Histogram: log(frequency) by time       1.18 μs <

 Memory estimate: 512 bytes, allocs estimate: 9.
 """
# @benchmark str = handle_new_message_ifelse("topic$(rand(1:10))", randstring(16))
"""
julia> @benchmark str = handle_new_message_ifelse("topic$(rand(1:10))", randstring(16))
BenchmarkTools.Trial: 10000 samples with 259 evaluations.
 Range (min … max):  305.332 ns …   7.591 μs  ┊ GC (min … max): 0.00% … 95.00%
 Time  (median):     329.878 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   359.609 ns ± 358.605 ns  ┊ GC (mean ± σ):  5.23% ±  5.02%

  ▁▃▅▆███▆▄▃▂▂▁   ▁▂▂   ▁▁  ▁                                   ▂
  ██████████████▇█████▇▇██████▇▆▇█▇▆▆▆▆▆▆▆▅▅▆▆▅▆▅▅▅▆▅▆▆▅▂▂▅▅▃▅▅ █
  305 ns        Histogram: log(frequency) by time        566 ns <

 Memory estimate: 496 bytes, allocs estimate: 8.
"""
function handle_new_message_dict(topic, package)
    t = topic_map[topic]
    parsed_package = parse_package(t, package)
    do_something(parsed_package)
end

function handle_new_message_ifelse(topic, package)
    if topic == "topic1"
        do_something(FormatAPackage(package))
    elseif topic == "topic2"
        do_something(FormatBPackage(package))
    elseif topic == "topic3"
        do_something(FormatCPackage(package))
    elseif topic == "topic4"
        do_something(FormatDPackage(package))
    elseif topic == "topic5"
        do_something(FormatEPackage(package))
    elseif topic == "topic6"
        do_something(FormatFPackage(package))
    elseif topic == "topic7"
        do_something(FormatGPackage(package))
    elseif topic == "topic8"
        do_something(FormatHPackage(package))
    elseif topic == "topic9"
        do_something(FormatIPackage(package))
    elseif topic == "topic10"
        do_something(FormatJPackage(package))
    else
        @error "Failed"
    end
en

my guess is that I will just have to accept that it’s an if/else block and move on.