faster setkeyv if keys already exist. #2332
Merged
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.
Closes #2331.
A small benchmark highlights the benefit of the new implementation (code at the end of the comment):
DT is a data.table with 1e7 rows. x1 and x2 are columns with 10 groups each (1:10), x3 is numeric random numbers.
Time is median time in milliseconds of 100 iterations
Cases 3 and 4 see a significant speed improvement since forderv is not called.
The only drawback is that the internal check that the data.table is really sorted by the key is not performed anymore. This has two implications that are absolutely acceptable from my perspective:
test.data.table()
, and should not hamper the speed of a function that is called frequently.sorted
attribute manually, it is not guaranteed that the key is still correct. I believe that users that go under the hood should be expected to know what they do. No need to maintain a costly check for this very specific case.Of course, I had to remove the test that checked this warning.
Here is the code for my benchmark: