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

Support for long vectors #239

Open
joshuaulrich opened this issue May 1, 2018 · 2 comments
Open

Support for long vectors #239

joshuaulrich opened this issue May 1, 2018 · 2 comments
Assignees
Labels
feature request New features

Comments

@joshuaulrich
Copy link
Owner

joshuaulrich commented May 1, 2018

R-3.0.0 added support for vectors longer than the maximum positive value for a signed 32-bit integer (2^31-1 ~ 2.147e9). It would be nice to support these in internal C code, but it is not particularly urgent.

The smallest xts object that would need this functionality would have 2^31 rows. That object would occupy 16GB of RAM if the index and the data were integers, and 32GB of RAM if they were both doubles.

I doubt many (if any) users have xts objects that large at the moment, but would be nice to address this.

@joshuaulrich joshuaulrich added the feature request New features label May 1, 2018
@joshuaulrich
Copy link
Owner Author

Well, I finally encountered this... time to fix. :-D

R$ n <- 1.1e7
   x <- replicate(200, xts::.xts(double(n), seq_len(n)), simplify = FALSE)
   y <- do.call(merge, x)
## Error in merge.xts(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  : 
##   negative length vectors are not allowed

This is because 200 * 1.1e7 = 2.2e9 is larger than .Machine$integer.max (2,147,483,647). So multiplying 200 * 1.1e7 in C overflows the 32-bit integer and returns a random value (which can be negative).

The lack of long vector support also crashes na.locf.xts

x <- xts::.xts(matrix(0.0, nrow = 1.1e7, ncol = 200), seq_len(1.1e7))

# then:
#    zoo::na.locf(x)  # segfaults
##  *** caught segfault ***
## address 0x7f5deb095848, cause 'memory not mapped'
## 
## Traceback:
##  1: na.locf.xts(x)
##  2: zoo::na.locf(x)

This is likely a problem in multiple places in the C code.

joshuaulrich added a commit that referenced this issue Mar 7, 2023
R added support for long vectors in version 3.0.0. The xts C code had
not been updated to take advantage of this feature. That meant xts
objects were limited to matrices with less than 2^32-1 elements.

Most of the changes are fall into these categories:

    1. Change index counters from 'int' to 'R_xlen_t'
    2. Use xlength() instead of LENGTH() or length() to get length
    3. Use xlengthgets() instead of lengthgets() to set length

There are a few non-functional changes to move type declaration closer
to where the variable is used.

See #239.
@joshuaulrich joshuaulrich self-assigned this Mar 7, 2023
@joshuaulrich
Copy link
Owner Author

More test cases:

n <- 1.1e9
x <- xts::.xts(matrix(seq_len(n*2), nrow = n, ncol = 2), seq_len(n))

y <- merge(x, x[,2])
y <- zoo::coredata(x)
y <- lag(x)
y <- diff(x)  # due to lag.xts()
y <- x[ seq_len(n*2),]
y <- x[-seq_len(n*2),]
xts::isOrdered(seq_len(n*2))
xts:::naCheck(seq_len(n*2))
y <- rbind(x, x)     ####  negative length vectors are not allowed
y <- xts::make.index.unique(x)

# zoo::na.locf(x)   ### segfaults
# na.omit(x)        ### killed

joshuaulrich added a commit that referenced this issue Mar 11, 2023
This also requires a change in the lag() and coredata() zoo functions.
They have been added to the 1.8-12 development version of zoo.

See #239.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New features
Projects
None yet
Development

No branches or pull requests

1 participant