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

Speed up initialization #1977

Merged
merged 5 commits into from
Dec 20, 2024
Merged

Speed up initialization #1977

merged 5 commits into from
Dec 20, 2024

Conversation

visr
Copy link
Member

@visr visr commented Dec 17, 2024

For lhm_2024_12_3 this speeds up initialization (Ribasim.Model(toml_path)), from 234 seconds to 4 seconds. Large models were especially slow to initialize since in create_graph we were constructing NodeID structs for each node with a separate call to the database.

This changes get_node_ids to return a Vector{NodeID} rather than a Vector{Int32}. This means that the type and index of each ID is now directly included, leading to simpler code.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is responsible for the speedup, going from n database queries to 1.

)
if idx <= 0
error("Node ID #$value of type $type is not in the Node table.")
function NodeID(node_type, value::Integer, node_ids::Vector{NodeID})::NodeID
Copy link
Member Author

Choose a reason for hiding this comment

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

Often you know the node_type, and since node_ids has it as well we can check if it is correct. Especially in test code I find NodeID(:Pump, 5, v) easier to read than NodeID(5, v). Both is possible however, we call the latter for e.g. listen_node_type handling where we don't know the type.

Copy link
Member

@evetion evetion left a comment

Choose a reason for hiding this comment

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

LGTM, good to bring back the queries from N to 1. I remember having a discussion on this a while (year?) back, and we were opinion that even though it was suboptimal, it wasn't hurting is yet. Nice to see we have reached that stage/growth now.

Also, TIL about counter (I knew it existed, hadn't seen an example yet).

core/src/read.jl Outdated
@@ -1395,11 +1397,52 @@ function Parameters(db::DB, config::Config)::Parameters
return p
end

function get_ids(db::DB, nodetype)::Vector{Int32}
sql = "SELECT node_id FROM Node WHERE node_type = $(esc_id(nodetype)) ORDER BY node_id"
function get_node_ints(db::DB, node_type)::Vector{Int32}
Copy link
Member

Choose a reason for hiding this comment

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

Nit: This signature is very similar to get_node_ids and I almost thought you made a typo. get_node_ids_int32 or similar?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ha yeah good point, done.

@visr visr merged commit ac0920d into main Dec 20, 2024
17 of 24 checks passed
@visr visr deleted the node-id-please branch December 20, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants