Perform minimal type checking for dictSet function, #5456
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.
What is the problem/goal being addressed?
There's a performance problem with the
dictSet
function: the dictionary is a map, which we convert to a list, only for the dictionary type-checking function to convert it back to a map.Suboptimal performance comes from two things:
dictSet
function.What is the solution to this problem?
I think we can perform fewer operations while upholding type safety.
No need to check the type of every single element, or convert the map to a list, which are both O(n) operations.
What are the changes here? How do they solve the problem and what other product impacts do they cause?
Commit only contains changes to a bit of purely functional code, and all the Dict.set tests which passed before still pass now.
I also added two new tests, adding different key/value pairs to empty dictionaries.
Has this information been included in the comments?
The general idea is commented.
Other notes
I wanted to add a test to check if a type error is raised when we:
but I wasn't sure how to write multi-line tests as there didn't seem to be any in the files I looked at. I think my code won't introduce any regression in that regard because it passes the exact same arguments to the type-checking function, except the list to type-check is shorter.
As an alternative to this approach, if we want to type check N elements but not to covert the whole map to a list, we can call Maq.toSeq and iterate through a number of elements smaller than the length of the map, but I'm not sure what we gain with that approach.