Skip to content

Commit 58c0d99

Browse files
committed
Avoid padding the data.
1 parent 2660c66 commit 58c0d99

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/common/io.cc

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ std::size_t PadPageForMmap(std::size_t file_bytes, dmlc::Stream* fo) {
187187
auto padded = n_pages * page_size;
188188
auto padding = padded - file_bytes;
189189
std::vector<std::uint8_t> padding_bytes(padding, 0);
190-
fo->Write(padding_bytes.data(), padding_bytes.size());
191-
return padded;
190+
// fo->Write(padding_bytes.data(), padding_bytes.size());
191+
return file_bytes;
192192
}
193193

194194
struct PrivateMmapStream::MMAPFile {
@@ -197,6 +197,8 @@ struct PrivateMmapStream::MMAPFile {
197197
#else
198198
std::int32_t fd {0};
199199
#endif
200+
char* base_ptr{ nullptr };
201+
std::size_t base_size{0};
200202
std::string path;
201203
};
202204

@@ -217,54 +219,58 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
217219
auto fd = open(path.c_str(), O_RDONLY);
218220
CHECK_GE(fd, 0) << "Failed to open:" << path << ". " << strerror(errno);
219221
#endif
220-
handle_ = nullptr;
221-
handle_.reset(new MMAPFile{fd, path});
222222

223-
void* ptr{nullptr};
223+
char* ptr{nullptr};
224+
auto view_start = offset / GetPageSize() * GetPageSize();
225+
auto view_size = length + (offset - view_start);
226+
std::cout << view_start << " size: " << view_size << std::endl;
224227
#if defined(__linux__) || defined(__GLIBC__)
225228
int prot{PROT_READ};
226229
if (!read_only) {
227230
prot |= PROT_WRITE;
228231
}
229-
ptr = reinterpret_cast<char*>(mmap64(nullptr, length, prot, MAP_PRIVATE, handle_->fd, offset));
230-
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << handle_->path << ". " << strerror(errno);
232+
ptr = reinterpret_cast<char*>(mmap64(nullptr, view_size, prot, MAP_PRIVATE, fd, view_start));
233+
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << path << ". " << strerror(errno);
231234
#elif defined(_MSC_VER)
232-
auto file_size = GetFileSize(handle_->fd, nullptr);
235+
auto file_size = GetFileSize(fd, nullptr);
233236
DWORD access = read_only ? PAGE_READONLY : PAGE_READWRITE;
234-
auto map_file = CreateFileMapping(handle_->fd, nullptr, access, 0, file_size, nullptr);
237+
auto map_file = CreateFileMapping(fd, nullptr, access, 0, file_size, nullptr);
235238
access = read_only ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
236-
std::uint32_t loff = static_cast<std::uint32_t>(offset);
237-
std::uint32_t hoff = offset >> 32;
238-
CHECK(map_file) << "Failed to map: " << handle_->path << ". " << GetLastError();;
239-
ptr = MapViewOfFile(map_file, access, hoff, loff, length);
239+
std::uint32_t loff = static_cast<std::uint32_t>(view_start);
240+
std::uint32_t hoff = view_start >> 32;
241+
CHECK(map_file) << "Failed to map: " << path << ". " << GetLastError();
242+
ptr = reinterpret_cast<char*>(MapViewOfFile(map_file, access, hoff, loff, view_size));
240243
if (ptr == nullptr) {
241244
system::ThrowAtError("MapViewOfFile");
242245
}
243-
CHECK_NE(ptr, nullptr) << "Failed to map: " << handle_->path << ". " << GetLastError() ;
246+
CHECK_NE(ptr, nullptr) << "Failed to map: " << path << ". " << GetLastError();
244247
#else
245248
CHECK_LE(offset, std::numeric_limits<off_t>::max())
246249
<< "File size has exceeded the limit on the current system.";
247250
int prot{PROT_READ};
248251
if (!read_only) {
249252
prot |= PROT_WRITE;
250253
}
251-
ptr = reinterpret_cast<char*>(mmap(nullptr, length, prot, MAP_PRIVATE, fd_, offset));
252-
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << handle_->path << ". " << strerror(errno);
254+
ptr = reinterpret_cast<char*>(mmap(nullptr, view_size, prot, MAP_PRIVATE, fd, view_start));
255+
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << path << ". " << strerror(errno);
253256
#endif // defined(__linux__)
254-
return reinterpret_cast<char*>(ptr);
257+
258+
handle_.reset(new MMAPFile{ fd, ptr, view_size, std::move(path) });
259+
ptr += (offset - view_start);
260+
return ptr;
255261
}
256262

257263
PrivateMmapStream::~PrivateMmapStream() {
258264
CHECK(handle_);
259265
#if defined(_MSC_VER)
260266
if (p_buffer_) {
261-
CHECK(UnmapViewOfFile(p_buffer_)) "Faled to munmap." << GetLastError();
267+
CHECK(UnmapViewOfFile(handle_->base_ptr)) "Faled to munmap." << GetLastError();
262268
}
263269
if (handle_->fd != INVALID_HANDLE_VALUE) {
264270
CHECK(CloseHandle(handle_->fd));
265271
}
266272
#else
267-
CHECK_NE(munmap(p_buffer_, buffer_size_), -1)
273+
CHECK_NE(munmap(handle_->base_ptr, handle_->base_size), -1)
268274
<< "Faled to munmap." << handle_->path << ". " << strerror(errno);
269275
CHECK_NE(close(handle_->fd), -1) << "Faled to close: " << handle_->path << ". " << strerror(errno);
270276
#endif

0 commit comments

Comments
 (0)