From ed65498fc5ce047030856a293a76e66f5c47be30 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Sat, 7 Nov 2026 20:00:00 +0000 Subject: [PATCH] perf: Speed up `read_graph()` and `write_graph()` for `~` paths, URLs and connections For these inputs both functions copy the file with the internal `read.graph.toraw()`, which grew its result with `c()` once per 20000-byte chunk. Each `c()` copies everything read so far, so the copy took quadratic time in the file size. Collect the chunks in a list and call `unlist()` once. The `readBin()` calls and the stop condition are unchanged, so the result is the same. Add tests for chunk boundaries and for `~` paths and connections. --- R/foreign.R | 13 ++++++---- tests/testthat/test-foreign.R | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/R/foreign.R b/R/foreign.R index d0768024e41..4f7df21a3b2 100644 --- a/R/foreign.R +++ b/R/foreign.R @@ -128,15 +128,18 @@ read.graph.toraw <- function(filename) { } tmpbufsize <- 20000 - buffer <- tmpbuffer <- readBin(filename, what = raw(0), n = tmpbufsize) - while (length(tmpbuffer) == tmpbufsize) { + # Concatenate once at the end: growing a vector with `c()` is quadratic. + chunks <- list() + repeat { tmpbuffer <- readBin(filename, what = raw(0), n = tmpbufsize) - buffer <- c(buffer, tmpbuffer) + chunks[[length(chunks) + 1L]] <- tmpbuffer + if (length(tmpbuffer) < tmpbufsize) { + break + } } close(filename) - rm(tmpbuffer) - buffer + unlist(chunks, use.names = FALSE) } write.graph.fromraw <- function(buffer, file) { diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index 0332707c7e9..13b079d78ce 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -26,6 +26,52 @@ test_that("reading GraphML file works", { expect_isomorphic(g2, g) }) +test_that("read.graph.toraw() returns every byte, in order", { + # Chunks are 20000 bytes. The pattern differs between chunks, so a lost, + # repeated or reordered chunk fails. + for (size in c(0, 1, 19999, 20000, 20001, 40000, 40001, 123457)) { + path <- withr::local_tempfile() + bytes <- as.raw(seq_len(size) %% 251) + writeBin(bytes, path) + + expect_identical(read.graph.toraw(path), bytes) + expect_identical(read.graph.toraw(file(path)), bytes) + expect_identical(read.graph.toraw(file(path, open = "rb")), bytes) + expect_identical(read.graph.toraw(rawConnection(bytes)), bytes) + } +}) + +test_that("read_graph() and write_graph() through `~` match a plain path", { + # A `~` in the path, or a connection, routes the file through + # read.graph.toraw(). Only a leading `~` is expanded, so files stay in `dir`. + dir <- withr::local_tempdir() + plain <- file.path(dir, "plain.edges") + tilde <- file.path(dir, "tilde~.edges") + out_plain <- file.path(dir, "out.edges") + out_tilde <- file.path(dir, "out~.edges") + + # Four bytes per edge: 0, 4, 20000, 40000 and 104004 bytes. + for (n_edges in c(0L, 1L, 5000L, 10000L, 26001L)) { + i <- seq_len(n_edges) + edge_lines <- sprintf("%d %d\n", i %% 10L, (3L * i + 1L) %% 10L) + writeBin(charToRaw(paste(edge_lines, collapse = "")), plain) + file.copy(plain, tilde, overwrite = TRUE) + expect_equal(file.size(tilde), 4 * n_edges) + + g <- read_graph(plain, format = "edgelist") + expect_ecount(g, n_edges) + expect_identical_graphs(read_graph(tilde, format = "edgelist"), g) + expect_identical_graphs(read_graph(file(plain), format = "edgelist"), g) + + write_graph(g, out_plain, format = "edgelist") + write_graph(g, out_tilde, format = "edgelist") + expect_identical( + readBin(out_tilde, raw(0), n = file.size(out_tilde)), + readBin(out_plain, raw(0), n = file.size(out_plain)) + ) + } +}) + test_that("reading graph in NCOL format", { ncol_path <- withr::local_tempfile(pattern = "testfile", fileext = ".ncol") g <- make_graph(c(1, 2, 2, 3))