Skip to content

Commit 17f251c

Browse files
committed
Add support for fadvise DONT_NEED to the PBF reader
This got lost when we switched to reading PBF files without an extra thread in 7f9c486.
1 parent 313a3e5 commit 17f251c

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

include/osmium/io/detail/input_format.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace osmium {
6565
osmium::osm_entity_bits::type read_which_entities;
6666
osmium::io::read_meta read_metadata;
6767
osmium::io::buffers_type buffers_kind;
68+
bool want_buffered_pages_removed;
6869
};
6970

7071
class Parser {

include/osmium/io/detail/pbf_input_format.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ namespace osmium {
6666
class PBFParser final : public Parser {
6767

6868
std::string m_input_buffer{};
69+
std::size_t m_offset = 0;
6970
int m_fd;
71+
bool m_want_buffered_pages_removed;
7072

7173
/**
7274
* Make sure the input data contains at least the specified
@@ -118,17 +120,26 @@ namespace osmium {
118120
* @returns true if size bytes could be read
119121
* false if EOF was encountered
120122
*/
121-
static bool read_exactly(int fd, char* buffer, std::size_t size) {
123+
bool read_exactly(char* buffer, std::size_t size) {
122124
std::size_t to_read = size;
123125

124126
while (to_read > 0) {
125-
auto const read_size = osmium::io::detail::reliable_read(fd, buffer + (size - to_read), to_read);
127+
auto const read_size = osmium::io::detail::reliable_read(m_fd, buffer + (size - to_read), to_read);
126128
if (read_size == 0) { // EOF
127129
return false;
128130
}
129131
to_read -= read_size;
130132
}
131133

134+
m_offset += size;
135+
136+
if (m_want_buffered_pages_removed && size > 100) {
137+
// The size check is there to avoid the system call
138+
// when we have only been reading a small number of
139+
// bytes, i.e. for a header.
140+
osmium::io::detail::remove_buffered_pages(m_fd, m_offset);
141+
}
142+
132143
return true;
133144
}
134145

@@ -139,7 +150,7 @@ namespace osmium {
139150
uint32_t read_blob_header_size_from_file() {
140151
if (m_fd != -1) {
141152
std::array<char, sizeof(uint32_t)> buffer;
142-
if (!read_exactly(m_fd, buffer.data(), buffer.size())) {
153+
if (!read_exactly(buffer.data(), buffer.size())) {
143154
return 0; // EOF
144155
}
145156
return check_size(get_size_in_network_byte_order(buffer.data()));
@@ -225,7 +236,7 @@ namespace osmium {
225236
if (m_fd != -1) {
226237
buffer.resize(size);
227238

228-
if (!read_exactly(m_fd, &*buffer.begin(), size)) {
239+
if (!read_exactly(&*buffer.begin(), size)) {
229240
throw osmium::pbf_error{"unexpected EOF"};
230241
}
231242
} else {
@@ -263,7 +274,8 @@ namespace osmium {
263274

264275
explicit PBFParser(parser_arguments& args) :
265276
Parser(args),
266-
m_fd(args.fd) {
277+
m_fd(args.fd),
278+
m_want_buffered_pages_removed(args.want_buffered_pages_removed) {
267279
}
268280

269281
PBFParser(const PBFParser&) = delete;

include/osmium/io/reader.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ namespace osmium {
161161
std::promise<osmium::io::Header>&& header_promise,
162162
osmium::osm_entity_bits::type read_which_entities,
163163
osmium::io::read_meta read_metadata,
164-
osmium::io::buffers_type buffers_kind) {
164+
osmium::io::buffers_type buffers_kind,
165+
bool want_buffered_pages_removed) {
165166
std::promise<osmium::io::Header> promise{std::move(header_promise)};
166167
osmium::io::detail::parser_arguments args = {
167168
pool,
@@ -171,7 +172,8 @@ namespace osmium {
171172
promise,
172173
read_which_entities,
173174
read_metadata,
174-
buffers_kind
175+
buffers_kind,
176+
want_buffered_pages_removed
175177
};
176178
creator(args)->parse();
177179
}
@@ -339,7 +341,8 @@ namespace osmium {
339341
m_thread = osmium::thread::thread_handler{parser_thread, std::ref(*m_pool), fd_for_parser, std::ref(m_creator),
340342
std::ref(m_input_queue), std::ref(m_osmdata_queue),
341343
std::move(header_promise), m_read_which_entities,
342-
m_read_metadata, m_buffers_kind};
344+
m_read_metadata, m_buffers_kind,
345+
m_decompressor->want_buffered_pages_removed()};
343346
}
344347

345348
template <typename... TArgs>

0 commit comments

Comments
 (0)