Trending

World technology news

→
R 3.0.0: handling index and memory limits in long vectors
Software Engineering · 2013
Software Engineering2013

R 3.0.0: handling index and memory limits in long vectors

Use R 3.0.0’s long-vector milestone to test the 32-bit index boundary, paged reads, and package interfaces without a huge allocation.

Archive retrospective · Topic year 2013

Developer's JournalPublished: Nov 27, 20266 min read
Reading progress0%
In this article
  1. Why test an index above two billion?
  2. Separate a position from the width of a page
  3. Exercise the boundary without a large allocation
  4. Keep the fake source small and its results obvious
  5. Review native interfaces and their length variables
  6. Budget for both source values and copies

Why test an index above two billion?

When R 3.0.0 arrived in 2013 it introduced long-vector support on 64-bit builds beyond the previous 2^31−1 element limit. A memory allocation experiment is only one possible response. An application may need to preserve a large position without reading the whole object. Our synthetic device archive has numbered observations and serves small pages instead of loading everything into memory. Assume it reaches 2^31+2 records. A four-value fixture can still exercise a read crossing the old boundary. The historical milestone made large positions expressible in R, while memory, architecture, and package interfaces remained relevant. We will check how an index is represented, how much is read at a time, and which native calls can handle it.

Separate a position from the width of a page

The largest positive R integer is 2^31−1. Forcing the next value through as.integer yields NA with a warning. For a long vector, length may return a double. It represents an integral count in a wider numeric type. Our page contract uses integral numbers bounded by 2^52 and rejects non-finite or fractional values. Storage and transport also need a contract. Converting a correctly held offset to a 32-bit integer column later recreates the original problem. Page width is a separate, smaller choice that controls work per request. This example caps it at one hundred thousand. A position can be high while a read stays inexpensive. Check downstream representations as well as the values received by R.

Application flowConceptual sketch · summary of the article's technical flow

Exercise the boundary without a large allocation

The function returns just two page boundaries. Observation positions begin at one. A request starting after the last observation returns an empty result; a page at the end is shortened to the remaining extent. Our sample starts at 2^31−1 and reaches 2^31+2. It crosses the old positive integer limit without allocating the complete archive. Avoid expressions such as 1:total in this historical exercise. A modern interpreter may represent some ranges compactly, which is not an assumption for a 2013 implementation. Assert the exact first and last values and confirm that as.integer fails on the first out-of-range position. Then inspect whether a real storage adapter preserves the position through its own parameter conversions.

page_bounds <- function(total, first, width) {
  integer_value <- function(x) {
    is.numeric(x) && length(x) == 1L && is.finite(x) && x == floor(x)
  }
  if (!integer_value(total) || total < 0 || total > 2^52 ||
      !integer_value(first) || first < 1 || first > 2^52 ||
      !integer_value(width) || width < 1 || width > 100000) {
    stop("invalid page request")
  }
  if (first > total) return(numeric(0))
  remaining <- total - first + 1
  c(first = as.double(first), last = as.double(first + min(width, remaining) - 1))
}

p <- page_bounds(2^31 + 2, 2^31 - 1, 100)
stopifnot(identical(unname(p), c(2^31 - 1, 2^31 + 2)))
stopifnot(is.na(suppressWarnings(as.integer(2^31))))
stopifnot(length(page_bounds(20, 21, 5)) == 0L)

Keep the fake source small and its results obvious

The fixture generates only the four selected positions and subtracts 2^31. The expected values are minus one, zero, one, and two. This gives the test plain outcomes for both order and page length. Check a single remaining record and an empty page as separate cases. A real archive that grows between reads needs a snapshot or a stable source identifier. An offset derived from changing order can shift when another event arrives ahead of it. Numeric capacity and consistent iteration are separate issues. This fixture uses a fixed total on purpose. Check how an actual reader carries its start position and visible data snapshot when a page needs a retry.

read_fixture <- function(first, last) {
  stopifnot(last - first < 10)
  (first:last) - 2^31
}
stopifnot(identical(read_fixture(p["first"], p["last"]), c(-1, 0, 1, 2)))
stopifnot(identical(unname(page_bounds(2^31 + 2, 2^31 + 2, 5)), rep(2^31 + 2, 2)))

Review native interfaces and their length variables

The R 3.0.0 announcement notes that .C and .Fortran do not accept long-vector inputs. A package using .Call is not automatically ready. Its implementation might still store a length returned by LENGTH in an int. For a truly long object, inspect XLENGTH, R_xlen_t, and the variables used in loops and size arithmetic. Another design is to keep the large position outside the native helper and pass a small page to code that works only on short vectors. That is possible when the native algorithm does not need the entire source at once. Test the combination of chunk results as well as one chunk. Identify the actual package code path using the full integration, since a public method may finish in a different routine than expected.

Budget for both source values and copies

At just over two billion double values, the raw elements alone can occupy about 16 GiB without metadata or copies. A transformation may hold both source and destination objects at once. Start with a bounded fixture and later measure peak memory in an isolated environment appropriate for the historical or supported runtime. Record R and package versions together. A useful acceptance matrix includes a small index, both sides of the old limit, an empty page, and the largest permitted width. Record the point of failure when an external adapter changes the position type. The enduring lesson is that large index support does not require a large allocation. A small fixture can find a conversion bug that would otherwise only appear much later in a large archive.

Be the first reader ✎ Write a comment

Reader discussion

Discussion prompt

What is the most important unanswered question about this topic?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.