Dplyr: Joining by a character column is slow, compared to joining by a factor column.

Created on 5 Sep 2015  路  5Comments  路  Source: tidyverse/dplyr

*_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 

Most helpful comment

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.

All 5 comments

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.

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

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.

Wow, pretty quick!!! Thanks a lot :+1:

@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.

Was this page helpful?
0 / 5 - 0 ratings