mongosoup/rmongodb — pull request #55 by dselivanov (2014-10-01 07:14:57+00:00)
Title: Fetching improvements.

Hi, Markus! Here is pull request mainly targeted to improve fetching. ```mongo.cursor.to.rlist``` is quite efficient function to retrieve data from cursor (not as fast as can be C version, but only 3-10x slower). I believe it can replace original ```mongo.cursor.to.list``` in future releases.
The second thing - I think, we should change default behavior of ```mongo.bson.to.list```. It should use @jeroenooms ```R_ConvertObject``` [implementation](https://github.com/mongosoup/rmongodb/commit/bd173ffc79ce4c8c487885499e139420bea76c39) by default.
P.S.
There is also some tests of appending elements to list:
![test_append](https://cloud.githubusercontent.com/assets/5123805/4471490/7f24895a-493a-11e4-9506-fb3e86d4efb0.png)

```R
library(ggplot2)
library(microbenchmark)
f_append <- function (n) {
l <- list()
for(idx in 1:n) {
l <- append(l, idx)
}
return(l)
}

f_index_append <- function (n) {
l <- list()
for(idx in 1:n) {
l[[length(l) + 1]] <- idx
}
return(l)
}

f_preallocated <- function (n) {
l <- vector(mode = 'list', length = n)
for(idx in 1:n) {
l[[idx]] <- idx
}
return(l)
}

f_environment <- function (n) {
e <- new.env()
for(idx in 1:n) {
assign(x = as.character(idx), value = idx, envir = e)
}
res <- as.list(e)
setNames(res[order(as.integer(names(res)))], NULL)
}

df <- data.frame()
for (n in seq(2, 4.5, 0.25)) {
print(n)
N <- 10**n
bench <- microbenchmark(f_append(N), f_index_append(N), f_environment(N), f_preallocated(N), times = 5)
df <- rbind2(df, data.frame(N = N, fun = bench$expr, time = bench$time / 1e6))
}
ggplot(data=df) + geom_line(aes(x = N, y = time, col = fun))

```