⚡️ Only track variables indices once one is deleted #568
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If we didn't have to support deleting variables, we could just implement
add_variables
as follows and get +- 2ns and 0 allocation for creating one variable.However, because some variables can be deleted we keep a
Set{MOI.VariableIndex}
of the variables and everytime one is created, we add it to the set.This means that when adding a variable, we need to compute its hash, and we sometime need to allocate to resize the memory used by the set.
We can see that the performance is +- 20 slower and it gets even slower if we keep calling
MOI.add_variable
because when the set is resized, it allocates even more memoryIt seems a bit unfortunate to do all this if the user is never going to delete a variable. Indeed, in that case the indices are simply
MOI.VariableIndex.(1:n)
wheren
is the number of variables created. So we only need to keep track ofn
.A similar reasoning lead to only create the dictionary for looking up by names only when the first lookup is queried : #535
We follow a similar approach in this PR and only create this
Set
when a variable is deleted.This gives a 10x speedup in time and ∞ speedup in memory.