Repository: ClickHouse/ClickHouse
File: src/Disks/IO/AsynchronousBoundedReadBuffer.cpp (C++)

Code before:

        if (read_settings.enable_filesystem_read_prefetches_log)
            appendToPrefetchLog(FilesystemPrefetchState::USED, result.size, result.execution_watch);

        last_prefetch_info = {};

        ProfileEvents::increment(ProfileEvents::RemoteFSPrefetchedReads);
        ProfileEvents::increment(ProfileEvents::RemoteFSPrefetchedBytes, result.size);
    }
    else
    {
        if (!use_page_cache)
            memory.resize(buffer_size);

        {
            ProfileEventTimeIncrement<Microseconds> watch(ProfileEvents::SynchronousRemoteReadWaitMicroseconds);
            result = readSync(memory.data(), buffer_size);
        }

        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedReads);
        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedBytes, result.size);
    }

    bytes_to_ignore = 0;
    resetWorkingBuffer();

    size_t bytes_read = result.size - result.offset;
    if (bytes_read)
    {
        page_cache_cell = result.page_cache_cell;
        working_buffer = Buffer(result.buf + result.offset, result.buf + result.size);
        pos = working_buffer.begin();
    }

    file_offset_of_buffer_end = impl->getFileOffsetOfBufferEnd();

    chassert(file_offset_of_buffer_end <= getFileSize());

    if (read_until_position && (file_offset_of_buffer_end > *read_until_position))
    {
        size_t excessive_bytes_read = file_offset_of_buffer_end - *read_until_position;

        if (excessive_bytes_read > working_buffer.size())
            throw Exception(ErrorCodes::LOGICAL_ERROR,
                            "File offset moved too far: old_file_offset = {}, new_file_offset = {}, read_until_position = {}, bytes_read = {}",
                            old_file_offset_of_buffer_end, file_offset_of_buffer_end, *read_until_position, bytes_read);

        working_buffer.resize(working_buffer.size() - excessive_bytes_read);
        file_offset_of_buffer_end = *read_until_position;
    }


Review comment (performance):
```suggestion
        if (!use_page_cache)
            /// memory.size() would always be zero, 
            /// we will use buffer allocated by page cache implementation.
            memory.resize(buffer_size);
```

Code after:
    IAsynchronousReader::Result result;
    if (prefetch_future.valid())
    {
        {
            ProfileEventTimeIncrement<Microseconds> watch(ProfileEvents::AsynchronousRemoteReadWaitMicroseconds);
            CurrentMetrics::Increment metric_increment{CurrentMetrics::AsynchronousReadWait};

            result = prefetch_future.get();
        }

        prefetch_future = {};
        prefetch_buffer.swap(memory);

        if (read_settings.enable_filesystem_read_prefetches_log)
            appendToPrefetchLog(FilesystemPrefetchState::USED, result.size, result.execution_watch);

        last_prefetch_info = {};

        ProfileEvents::increment(ProfileEvents::RemoteFSPrefetchedReads);
        ProfileEvents::increment(ProfileEvents::RemoteFSPrefetchedBytes, result.size);
    }
    else
    {
        /// Don't allocate any buffers if page cache is in use, the cache has its own buffers (PageCacheCell).
        if (!use_page_cache)
            memory.resize(buffer_size);

        {
            ProfileEventTimeIncrement<Microseconds> watch(ProfileEvents::SynchronousRemoteReadWaitMicroseconds);
            result = readSync(memory.data(), buffer_size);
        }

        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedReads);
        ProfileEvents::increment(ProfileEvents::RemoteFSUnprefetchedBytes, result.size);
    }

    bytes_to_ignore = 0;
    resetWorkingBuffer();

    chassert(use_page_cache || !result.page_cache_cell);

    size_t bytes_read = result.size - result.offset;
    if (bytes_read)
    {
        page_cache_cell = result.page_cache_cell;
        working_buffer = Buffer(result.buf + result.offset, result.buf + result.size);
        pos = working_buffer.begin();
    }

    file_offset_of_buffer_end = result.file_offset_of_buffer_end;