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

[Request] setkeyv can be accelerated if key already exists #2331

MarkusBonsch opened this issue Sep 6, 2017 · 2 comments

[Request] setkeyv can be accelerated if key already exists #2331

MarkusBonsch opened this issue Sep 6, 2017 · 2 comments


Copy link

@MarkusBonsch MarkusBonsch commented Sep 6, 2017

setkeyv() can be accelerated significantly in cases where the key already exists.
This can be useful e.g. if you create a function that takes a data.table as an argument and you need to set a key but don't know if the user has already set the key on the input.
With the new implementation, you can just use setkey without worrying about speed penalties.

setkeyv() does two things:

  1. determine the correct sorting calling forderv
  2. Rearranging the data.table by reference calling Creorder

Currently, if the key already exists, the call to forderv is still executed and only step 2 is skipped.
The only reason is a sanity check that the data.table is really sorted by the key.
I believe, it is not necessary to perform this sanity check each time, especially since it has been around for quite a while so that potential bugs should have popped up.

@mattdowle mattdowle added this to the v1.10.6 milestone Sep 6, 2017
Copy link

@mattdowle mattdowle commented Sep 6, 2017

Great! Your PR merged. See comment there : #2332 (comment)

Copy link

@brodieG brodieG commented Jan 8, 2019

@MarkusBonsch thanks for doing this. I had hacky work-arounds that did this myself, and I didn't get to actually submit a PR. And of course thanks to the data.table team for all they do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

3 participants