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

Joining by a character column is slow, compared to joining by a factor column. #1386

Closed
yutannihilation opened this issue Sep 4, 2015 · 5 comments
Assignees

Comments

@yutannihilation
Copy link
Member

*_join() functions are slow when the key is character. Do you have any plan to improve this?

library("dplyr")

set.seed(71)
size1 <- 4*10^5
size2 <- size1 * 0.1
df1 <- data.frame(id=paste0("SERVICE_", 1:size1), value=rnorm(size1), stringsAsFactors=FALSE)
df2 <- data.frame(id=paste0("SERVICE_", sample(1:size1, size2)), value=rnorm(size2), stringsAsFactors=FALSE)

print(system.time(ljd <- dplyr::left_join(df1, df2, "id")))
#>    user  system elapsed 
#>   15.50    0.07   15.56 

I think *_join() can be faster by factorizing the key beforehand in most cases. Futhermore, I believe the key should be treated as factor, since no one will try to join by some column where every row has a different value.

print(system.time({
  lvl <- unique(c(df1$id, df2$id))
  ljd <- dplyr::left_join(mutate(df1, id=factor(id, levels = lvl)),
                          mutate(df2, id=factor(id, levels = lvl)),
                          "id")
  })
)
#>    user  system elapsed 
#>    0.33    0.10    0.42 
@jimhester
Copy link
Contributor

dplyr should be taking advantage of R's global string cache for doing string matching when using in-memory objects. You only need to check if the relevant character pointers are the same.

See https://github.com/wch/r-source/blob/4a2026e8e/src/main/relop.c#L564-L569, https://github.com/wch/r-source/blob/9d4e23e/src/main/memory.c#L3878-L3894 for the C implementation.

@romainfrancois romainfrancois self-assigned this Sep 15, 2015
@romainfrancois
Copy link
Member

It's a bit more complicated than that actually. Sometimes two different pointers are the same strings but with different encodings, and we want the joins to consider them equal.

But your point is great, I am looking into it right now. We base the hashing on the strings on some ordering (which respects encoding etc ...) but actually we only need to be able to differentiate them, not necessarily rank them.

TBC

@romainfrancois
Copy link
Member

Now getting :

set.seed(71)
size1 <- 4*10^5
size2 <- size1 * 0.1
df1 <- data.frame(id=paste0("SERVICE_", 1:size1), value=rnorm(size1), stringsAsFactors=FALSE)
df2 <- data.frame(id=paste0("SERVICE_", sample(1:size1, size2)), value=rnorm(size2), stringsAsFactors=FALSE)

print(system.time(ljd <- dplyr::left_join(df1, df2, "id")))
#>   user  system elapsed
#>  0.333   0.013   0.346
print(system.time({
  lvl <- unique(c(df1$id, df2$id))
  ljd <- dplyr::left_join(mutate(df1, id=factor(id, levels = lvl)),
                          mutate(df2, id=factor(id, levels = lvl)),
                          "id") %>% mutate( id = as.character(id) )
  })
)
#>    user  system elapsed
#>  0.255   0.007   0.262

which is much better than what we had before. Still above the suggested implementation using factor. I might just go ahead and use that next.

@yutannihilation
Copy link
Member Author

Wow, pretty quick!!! Thanks a lot 👍

@tedlie
Copy link

tedlie commented Apr 5, 2016

@romainfrancois Is this a temporary update? I am running this test with the most recent version of dplyr, of which the version is 0.4.3, but the speed is slow.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants