MichaelChirico commented on a change in pull request #24885: [SPARK-28040][R]
Add serialization for glue type
URL: https://github.com/apache/spark/pull/24885#discussion_r325521563
##########
File path: R/pkg/R/serialize.R
##########
@@ -71,18 +69,19 @@ writeObject <- function(con, object, writeType = TRUE) {
switch(serdeType,
NULL = writeVoid(con),
integer = writeInt(con, object),
+ glue = ,
Review comment:
I ran a quick benchmark... seems to me using implicit `switch` is about 40%
faster:
```
library(microbenchmark)
set.seed(38459)
options = sprintf(
"'%s'",
replicate(1e4, paste(sample(letters, sample(5L, 1L), TRUE), collapse = ''))
)
nrep = 1e7
timings = matrix(nrow = nrep, ncol = 3L)
for (ii in 1:nrep) {
nopt = sample(20L, 1L)
thisopt = sample(options, nopt)
thisval = paste0(1:nopt)
blank = sample(nopt, sample(nopt, 1L))
headval = sample(options, 1L)
fullexpr = parse(
text = sprintf("switch(%s, %s)", headval,
paste(sprintf('%s=%s', thisopt, thisval), collapse = ',
'))
)
blankexpr = parse(
text = sprintf("switch(%s, %s)", headval,
paste(sprintf('%s=%s', thisopt, `[<-`(thisval, blank,
'')),
collapse = ', '))
)
t0 = get_nanotime()
eval(fullexpr)
t1 = get_nanotime()
eval(blankexpr)
t2 = get_nanotime()
timings[ii, 1L] = nopt
timings[ii, 2L] = t1 - t0
timings[ii, 3L] = t2 - t1
}
```
with
```
summary(timings[ , 3L]/timings[ , 2L])
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
0.00 0.57 0.60 0.77 0.64 137407.00 227997
```
(I cut the benchmark short by 300K repetitions)
effectively uniform across `nopt` as well
```
$`1`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.574 0.607 0.739 0.647 29191.462
$`2`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.570 0.603 0.723 0.642 28419.130
$`3`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00e+00 5.68e-01 6.01e-01 7.06e-01 6.41e-01 2.35e+04
$`4`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.569 0.603 0.665 0.644 22380.997
$`5`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.570 0.605 0.701 0.645 26308.266
$`6`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.569 0.604 0.768 0.645 26223.936
$`7`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.568 0.603 0.673 0.645 26747.025
$`8`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.569 0.605 0.798 0.646 24671.523
$`9`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.57 0.60 0.93 0.65 127229.42
$`10`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.015 0.570 0.606 0.955 0.648 22857.001
$`11`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00e+00 5.69e-01 6.05e-01 7.50e-01 6.47e-01 2.14e+04
$`12`
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00e-02 5.68e-01 6.03e-01 7.00e-01 6.45e-01 2.00e+04
$`13`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.566 0.602 0.732 0.644 20712.366
$`14`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.566 0.601 0.692 0.643 18919.120
$`15`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.041 0.564 0.599 0.675 0.640 17669.048
$`16`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.56 0.60 1.07 0.64 137407.00
$`17`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.05 0.56 0.60 0.98 0.64 121523.06
$`18`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.560 0.595 0.685 0.636 19591.997
$`19`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.559 0.594 0.763 0.635 24281.437
$`20`
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00e+00 5.59e-01 5.94e-01 7.42e-01 6.34e-01 2.19e+04
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]