Skip to content

Commit 713c701

Browse files
Refactor copyFile() message fragment for ease of translation (#6483)
* Refactor copyFile() message fragment for ease of translation * Alternative approach: refactor logic outside of copyFile() helper * missing '}' * match improved grammar in test * exact equality to -1.0
1 parent 741d362 commit 713c701

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

inst/tests/tests.Rraw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12479,7 +12479,7 @@ if (test_R.utils) {
1247912479
output = "Copying file in RAM.*file is very unusual.*ends abruptly.*multiple of 4096")
1248012480
test(1869.7, fread(testDir("onecol4096.csv.bz2"), verbose=TRUE)[c(1,2,245,246,249,255:.N),],
1248112481
data.table(A=c("FooBarBazQux000","FooBarBazQux001","","FooBarBazQux245","","FooBarBazQux254","FooBarBazQux","FooBarBaz12","FooBarBazQux256","","","")),
12482-
output = "Copying file in RAM.*file is very unusual.*one single column, ends with 2 or more end-of-line.*and is a multiple of 4096")
12482+
output = "Copying file in RAM.*file is very unusual.*one single column, ends with 2 or more end-of-line.*and the file size is a multiple of 4096")
1248312483
}
1248412484

1248512485
# better colname detection by comparing potential column names to the whole sample not just the first row of the sample, #2526

src/fread.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -437,19 +437,16 @@ static const char* filesize_to_str(size_t fsize)
437437
snprintf(output, BUFFSIZE, "%"PRIu64" bytes", (uint64_t)lsize);
438438
return output;
439439
}
440-
441-
void copyFile(size_t fileSize, const char *msg, bool verbose) // only called in very very rare cases
440+
double copyFile(size_t fileSize) // only called in very very rare cases
442441
{
443442
double tt = wallclock();
444-
mmp_copy = (char *)malloc((size_t)fileSize + 1/* extra \0 */);
443+
mmp_copy = (char *)malloc((size_t)fileSize + 1 /* extra \0 */);
445444
if (!mmp_copy)
446-
STOP(_("Unable to allocate %s of contiguous virtual RAM. %s allocation."), filesize_to_str(fileSize), msg);
445+
return -1.0;
447446
memcpy(mmp_copy, mmp, fileSize);
448447
sof = mmp_copy;
449448
eof = (char *)mmp_copy + fileSize;
450-
tt = wallclock()-tt;
451-
if (tt>0.5) DTPRINT(_("Avoidable %.3f seconds. %s time to copy.\n"), tt, msg); // not warning as that could feasibly cause CRAN tests to fail, say, if test machine is heavily loaded
452-
if (verbose) DTPRINT(_(" File copy in RAM took %.3f seconds.\n"), tt);
449+
return wallclock()-tt;
453450
}
454451

455452

@@ -1534,10 +1531,20 @@ int freadMain(freadMainArgs _args) {
15341531
// field) since we rely on that logic to avoid the copy below when fileSize$4096==0 but there is a final eol ok.
15351532
// TODO: portable way to discover relevant page size. 4096 is lowest common denominator, though, and should suffice.
15361533
} else {
1537-
const char *msg = _("This file is very unusual: it ends abruptly without a final newline, and also its size is a multiple of 4096 bytes. Please properly end the last row with a newline using for example 'echo >> file' to avoid this ");
1538-
if (verbose) DTPRINT(_(" File ends abruptly with '%c'. Copying file in RAM. %s copy.\n"), eof[-1], msg);
1534+
const char *msg = _("This file is very unusual: it ends abruptly without a final newline, and also its size is a multiple of 4096 bytes. Please properly end the last row with a newline using for example 'echo >> file'");
1535+
if (verbose)
1536+
DTPRINT(_(" File ends abruptly with '%c'. Final end-of-line is missing. Copying file in RAM. %s.\n"), eof[-1], msg);
15391537
// In future, we may discover a way to mmap fileSize+1 on all OS when fileSize%4096==0, reliably. If and when, this clause can be updated with no code impact elsewhere.
1540-
copyFile(fileSize, msg, verbose);
1538+
double time_taken = copyFile(fileSize);
1539+
if (time_taken == -1.0) {
1540+
if (!verbose)
1541+
DTPRINT("%s. Attempt to copy file in RAM failed.", msg);
1542+
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str(fileSize));
1543+
}
1544+
if (verbose)
1545+
DTPRINT(_(" File copy in RAM took %.3f seconds.\n"), time_taken);
1546+
else if (time_taken > 0.5)
1547+
DTPRINT(_("Avoidable file copy in RAM took %.3f seconds. %s.\n"), time_taken, msg); // not warning as that could feasibly cause CRAN tests to fail, say, if test machine is heavily loaded
15411548
}
15421549
}
15431550
*_const_cast(eof) = '\0'; // cow page
@@ -1806,10 +1813,20 @@ int freadMain(freadMainArgs _args) {
18061813
if (ncol==1 && lastEOLreplaced && (eof[-1]=='\n' || eof[-1]=='\r')) {
18071814
// Multiple newlines at the end are significant in the case of 1-column files only (multiple NA at the end)
18081815
if (fileSize%4096==0) {
1809-
const char *msg = _("This file is very unusual: it's one single column, ends with 2 or more end-of-line (representing several NA at the end), and is a multiple of 4096, too.");
1810-
if (verbose) DTPRINT(_(" Copying file in RAM. %s\n"), msg);
1816+
const char *msg = _("This file is very unusual: it's one single column, ends with 2 or more end-of-line (representing several NA at the end), and the file size is a multiple of 4096, too");
1817+
if (verbose)
1818+
DTPRINT(_(" Copying file in RAM. %s\n"), msg);
18111819
ASSERT(mmp_copy==NULL, "mmp has already been copied due to abrupt non-eol ending, so it does not end with 2 or more eol.%s", ""/*dummy arg for macro*/); // #nocov
1812-
copyFile(fileSize, msg, verbose);
1820+
double time_taken = copyFile(fileSize);
1821+
if (time_taken == -1.0) {
1822+
if (!verbose)
1823+
DTPRINT("%s. Attempt to copy file in RAM failed.", msg);
1824+
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str(fileSize));
1825+
}
1826+
if (verbose)
1827+
DTPRINT(_(" File copy in RAM took %.3f seconds.\n"), time_taken);
1828+
else if (tt>0.5)
1829+
DTPRINT(_("Avoidable file copy in RAM took %.3f seconds. %s.\n"), time_taken, msg); // not warning as that could feasibly cause CRAN tests to fail, say, if test machine is heavily loaded
18131830
pos = sof + (pos-(const char *)mmp);
18141831
firstJumpEnd = sof + (firstJumpEnd-(const char *)mmp);
18151832
} else {

0 commit comments

Comments
 (0)