-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Ensure that LoadSequentialFile() actually read the whole file #5831
Changes from 11 commits
6810799
5a5f99c
469705b
c8fabb2
5083ca2
efc6f47
d57e0ab
4782c7d
2685ce6
29708e9
7bb99c6
4faae5b
c4b7ffa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,10 @@ | |
#include <unistd.h> | ||
#endif // defined(__unix__) | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <fstream> | ||
#include <string> | ||
#include <utility> | ||
#include <cstdio> | ||
|
||
#include "xgboost/logging.h" | ||
#include "io.h" | ||
|
@@ -120,27 +121,22 @@ std::string LoadSequentialFile(std::string fname) { | |
#if defined(__linux__) | ||
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
#endif // defined(__linux__) | ||
ssize_t bytes_read = read(fd, &buffer[0], f_size_bytes); | ||
if (bytes_read < 0) { | ||
close(fd); | ||
ReadErr(); | ||
ssize_t bytes_read = 0; | ||
while (bytes_read < f_size_bytes) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @trivialfis Take a look at this while loop. This is a commonly used construct in UNIX programs. |
||
ssize_t result = read(fd, &buffer[bytes_read], f_size_bytes - bytes_read); | ||
if (result < 0) { | ||
close(fd); | ||
ReadErr(); | ||
} | ||
bytes_read += result; | ||
} | ||
close(fd); | ||
buffer.back() = '\0'; | ||
#else // defined(__unix__) | ||
FILE *f = fopen(fname.c_str(), "r"); | ||
if (f == NULL) { | ||
std::string msg; | ||
OpenErr(); | ||
} | ||
fseek(f, 0, SEEK_END); | ||
auto fsize = ftell(f); | ||
fseek(f, 0, SEEK_SET); | ||
Comment on lines
-135
to
-137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: According to the MSDN documentation, Fix. We do away with On Linux, we use |
||
|
||
buffer.resize(fsize + 1); | ||
fread(&buffer[0], 1, fsize, f); | ||
fclose(f); | ||
std::ifstream ifs(fname); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please note that this is terribly slow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slower than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In https://social.msdn.microsoft.com/Forums/en-US/87e128e2-5320-4796-b4a2-36248f86a8ce/huge-performance-hit-when-using-ifstream-vs-fopen?forum=Vsexpressvc, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Interesting as I just tried on Linux, it's about 10x slower. But I guess when file is that huge JSON would be the bottleneck here. |
||
buffer = std::string((std::istreambuf_iterator<char>(ifs)), | ||
(std::istreambuf_iterator<char>())); | ||
#endif // defined(__unix__) | ||
buffer.back() = '\0'; | ||
return buffer; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trivialfis Previously, we called
read()
only once. For a file containing 2896075944 bytes (2.69 GB),read()
only reads 2147479552 bytes (1.99 GB), and the variables would be set as follows:This PR ensures that
bytes_read
would be identical tof_size_bytes
, by callingread()
multiple times in awhile
loop.