lidavidm commented on code in PR #36180:
URL: https://github.com/apache/arrow/pull/36180#discussion_r1244343178
##########
cpp/src/arrow/io/caching.h:
##########
@@ -43,10 +43,14 @@ struct ARROW_EXPORT CacheOptions {
int64_t range_size_limit;
/// \brief A lazy cache does not perform any I/O until requested.
bool lazy;
+ /// \brief The maximum number of ranges to be prefetched. This is only used
Review Comment:
Explain what prefetching means here?
##########
cpp/src/arrow/io/memory_test.cc:
##########
@@ -845,13 +845,61 @@ TEST(RangeReadCache, Lazy) {
ASSERT_EQ(3, file->read_count());
}
+TEST(RangeReadCache, LazyWithPrefetching) {
+ std::string data = "abcdefghijklmnopqrstuvwxyz";
+
+ auto file = std::make_shared<CountingBufferReader>(Buffer(data));
+ CacheOptions options = CacheOptions::LazyDefaults();
+ options.hole_size_limit = 1;
+ options.range_size_limit = 3;
+ options.prefetch_limit = 2;
+ internal::ReadRangeCache cache(file, {}, options);
+
+ ASSERT_OK(cache.Cache({{1, 1}, {3, 1}, {5, 2}, {8, 2}, {20, 2}, {25, 0}}));
+
+ // Lazy cache doesn't fetch ranges until requested
+ ASSERT_EQ(0, file->read_count());
+
+ ASSERT_OK_AND_ASSIGN(auto buf, cache.Read({8, 2}));
+ AssertBufferEqual(*buf, "ij");
+ // Read {8, 2} and prefetch {20, 2}
+ ASSERT_EQ(2, file->read_count());
+
+ ASSERT_OK_AND_ASSIGN(buf, cache.Read({20, 2}));
+ AssertBufferEqual(*buf, "uv");
+ // Read count remains 2 as the range {20, 2} has already been prefetched
+ ASSERT_EQ(2, file->read_count());
+
+ ASSERT_OK_AND_ASSIGN(buf, cache.Read({1, 1}));
+ AssertBufferEqual(*buf, "b");
+ // Read {1, 3} and prefetch {5, 2}
+ ASSERT_EQ(4, file->read_count());
+
+ ASSERT_OK_AND_ASSIGN(buf, cache.Read({3, 1}));
+ AssertBufferEqual(*buf, "d");
+ // Already prefetched
+ ASSERT_EQ(4, file->read_count());
+
+ // Requested ranges are still cached
+ ASSERT_OK_AND_ASSIGN(buf, cache.Read({5, 1}));
+ AssertBufferEqual(*buf, "f");
+ // Already prefetched
+ ASSERT_EQ(4, file->read_count());
+
+ // Non-cached ranges
+ ASSERT_RAISES(Invalid, cache.Read({20, 3}));
+ ASSERT_RAISES(Invalid, cache.Read({19, 3}));
+ ASSERT_RAISES(Invalid, cache.Read({0, 3}));
+ ASSERT_RAISES(Invalid, cache.Read({25, 2}));
+}
+
TEST(CacheOptions, Basics) {
auto check = [](const CacheOptions actual, const double
expected_hole_size_limit_MiB,
const double expected_range_size_limit_MiB) -> void {
const CacheOptions expected = {
static_cast<int64_t>(std::round(expected_hole_size_limit_MiB * 1024 *
1024)),
static_cast<int64_t>(std::round(expected_range_size_limit_MiB * 1024 *
1024)),
- /*lazy=*/false};
+ /*lazy=*/false, /*prefetch_limit=*/0};
Review Comment:
For compatibility, we should probably add explicit constructors here now
##########
cpp/src/arrow/io/caching.cc:
##########
@@ -204,6 +206,17 @@ struct ReadRangeCache::Impl {
if (it != entries.end() && it->range.Contains(range)) {
auto fut = MaybeRead(&*it);
ARROW_ASSIGN_OR_RAISE(auto buf, fut.result());
+ if (options.lazy && options.prefetch_limit > 0) {
+ int64_t num_prefetched = 0;
+ for (auto next_it = it + 1;
+ next_it != entries.end() && num_prefetched++ <
options.prefetch_limit;
Review Comment:
nit: instead of confusing postfix, just increment the counter inside the loop
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]