@enum is slow?

We have a project with 67 different enum types, each with say a dozen enumerated values. Each type and value is exported. Simply including this file takes about 15s, which is a surprisingly long time. When I comment out the enum definitions in that file, the file takes ~2s in include, so it certainly seems to be the enums causing this.

Does anyone know why?

Is there anything I can do?

(A lot of extra context for the deeply concerned: The file I’m including is itself generated from a bunch of type definitions in a YAML file, these types being shared with a bunch of other languages. Hence, since the file is itself generated, I don’t mind doing something that involves tedious code to make it faster, since that code can be written automatically. I could also bypass writing this file and directly evaluate expressions by parsing that YAML file, but it’s not clear to me how I’d make enums, which are created macros, in that case, since my code would have to already be running. I think I’d have to do manually what the @enum macro is doing, and maybe that’s even the best way to go. Not sure.)

This generates a file with a bunch of enums. Including the generated filed takes ~9s on my computer.

open("enums.jl", "w") do f
    for k in 1:100
        write(f, "@enum Enum$(k)")
        for j in 1:12
            write(f, " enum$(k)value$(j)=$(j)")
        end
        write(f, "\n")
    end
end
# Now run include("enums.jl"); it will take a long time.

Put it into a package/module so that it benefits from precompilation?

1 Like

Enums expand to a fair chunk of code but that sounds excessive. I would open an issue about it.

1 Like

Thanks. I’ve started an issue here:

@jeff.bezanson just fixed this here: https://github.com/JuliaLang/julia/pull/33494 . Thanks Jeff!

3 Likes