Commit eacc3c3a authored by Ramon Nou's avatar Ramon Nou
Browse files

optimize inline data caching logic in gkfs_do_read function

parent 642b9568
Loading
Loading
Loading
Loading
+25 −14
Original line number Diff line number Diff line
@@ -1426,23 +1426,34 @@ gkfs_do_read(const gkfs::filemap::OpenFile& file, char* buf, size_t count,
       offset < gkfs::config::metadata::inline_data_size) {

        // OPTIMIZATION: Check if we have the inline data cached
        // We optimize if the client requested at least the size of the file (count + offset > size)
        // or if the count is <= size
        if(!file.inline_data().empty() &&
           (offset + count <= file.inline_data_size() ||
            count <= file.inline_data().size())) {
        // We only use the cache if:
        // 1. The read starts within the cached data.
        // 2. EITHER the read fits entirely within the cached data,
        //    OR the cached data contains the entire file (so we can serve up to
        //    EOF).
        if(!file.inline_data().empty() && offset < file.inline_data().size() &&
           ((offset + count <= file.inline_data().size()) ||
            (file.inline_data().size() >= file.inline_data_size()))) {

            // Logic to calculate how much to copy
            // If requested count goes beyond file size, we clamp it
            size_t copy_size = 0;
            if (offset < file.inline_data_size()) {
                 copy_size = std::min(count, file.inline_data_size() - offset);
            }
            // We can read min(requested, what's in cache, distance to EOF)
            size_t valid_cache_size = file.inline_data().size();
            size_t logical_size = file.inline_data_size();

            // If offset is beyond size, copy_size remains 0 (EOF)
            size_t copy_size = std::min(count, valid_cache_size - offset);

            LOG(DEBUG, "{}() Using cached inline data. count: {} offset: {} size: {} copy: {}",
                __func__, count, offset, file.inline_data_size(), copy_size);
            // Also clamp to logical file size to avoid reading padding (e.g.
            // nulls)
            if(offset < logical_size) {
                copy_size = std::min(copy_size, logical_size - offset);
            } else {
                copy_size = 0; // Reading past EOF (shouldn't happen given
                               // conditions, but safe)
            }

            LOG(DEBUG,
                "{}() Using cached inline data. count: {} offset: {} size: {} copy: {}",
                __func__, count, offset, logical_size, copy_size);

            if(copy_size > 0)
                memcpy(buf, file.inline_data().c_str() + offset, copy_size);