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

Code before:
        ProfileEvents::increment(ProfileEvents::ThreadpoolReaderSubmitReadSynchronously);
        ProfileEvents::increment(ProfileEvents::ThreadpoolReaderSubmitReadSynchronouslyBytes, res.size);
        if (res.execution_watch)
            ProfileEvents::increment(ProfileEvents::ThreadpoolReaderSubmitReadSynchronouslyMicroseconds, res.execution_watch->elapsedMicroseconds());

        promise.set_value(std::move(res));
        return future;
    }

    ProfileEventTimeIncrement<Microseconds> elapsed(ProfileEvents::ThreadpoolReaderSubmit);
    return scheduleFromThreadPoolUnsafe<Result>(
        [request, this]() -> Result { return execute(request, /*seek_performed=*/true); }, *pool, "VFSRead", request.priority);
}

IAsynchronousReader::Result ThreadPoolRemoteFSReader::execute(Request request)
{
    return execute(request, /*seek_performed=*/false);
}

IAsynchronousReader::Result ThreadPoolRemoteFSReader::execute(Request request, bool seek_performed)
{
    CurrentMetrics::Increment metric_increment{CurrentMetrics::RemoteRead};

    auto * fd = assert_cast<RemoteFSFileDescriptor *>(request.descriptor.get());
    auto & reader = fd->getReader();
    auto * cached_reader = typeid_cast<CachedInMemoryReadBufferFromFile *>(&reader);

    auto read_counters = fd->getReadCounters();
    std::optional<AsyncReadIncrement> increment = read_counters ? std::optional<AsyncReadIncrement>(read_counters) : std::nullopt;

    {
        ProfileEventTimeIncrement<Microseconds> elapsed(ProfileEvents::ThreadpoolReaderPrepareMicroseconds);
        if (!seek_performed)
        {
            if (!cached_reader)
                reader.set(request.buf, request.size);
            reader.seek(request.offset, SEEK_SET);
        }

        if (request.ignore)
        {
            ProfileEvents::increment(ProfileEvents::AsynchronousReaderIgnoredBytes, request.ignore);
            reader.ignore(request.ignore);
        }
    }

    auto watch = std::make_unique<Stopwatch>(CLOCK_REALTIME);

    bool result = reader.available();
    if (!result)
    {

Review comment (performance):
```suggestion
    auto * page_cache_reader = typeid_cast<CachedInMemoryReadBufferFromFile *>(&reader);
```
Technically `cached_reader` could also mean `CachedOnDiskReadBufferFromFile`, not only `CachedInMemoryReadBufferFromFile`, so `cached_reader` here is misleading.

Code after:
    {
        std::promise<Result> promise;
        std::future<Result> future = promise.get_future();
        auto && res = execute(request, /*seek_performed=*/true);

        ProfileEvents::increment(ProfileEvents::ThreadpoolReaderSubmitReadSynchronously);
        ProfileEvents::increment(ProfileEvents::ThreadpoolReaderSubmitReadSynchronouslyBytes, res.size);
        if (res.execution_watch)
            ProfileEvents::increment(ProfileEvents::ThreadpoolReaderSubmitReadSynchronouslyMicroseconds, res.execution_watch->elapsedMicroseconds());

        promise.set_value(std::move(res));
        return future;
    }

    ProfileEventTimeIncrement<Microseconds> elapsed(ProfileEvents::ThreadpoolReaderSubmit);
    return scheduleFromThreadPoolUnsafe<Result>(
        [request, this]() -> Result { return execute(request, /*seek_performed=*/true); }, *pool, ThreadName::REMOTE_FS_READ_THREAD_POOL, request.priority);
}

IAsynchronousReader::Result ThreadPoolRemoteFSReader::execute(Request request)
{
    return execute(request, /*seek_performed=*/false);
}

IAsynchronousReader::Result ThreadPoolRemoteFSReader::execute(Request request, bool seek_performed)
{
    CurrentMetrics::Increment metric_increment{CurrentMetrics::RemoteRead};

    auto * fd = assert_cast<RemoteFSFileDescriptor *>(request.descriptor.get());
    auto & reader = fd->getReader();
    auto * page_cache_reader = typeid_cast<CachedInMemoryReadBufferFromFile *>(&reader);

    /// Page cache readers use their own buffers (PageCacheCell), so request.buf is expected to be null.
    if (!page_cache_reader && !request.buf)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Request buffer is invalid");

    if (!request.size)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Request buffer size cannot be zero");

    auto read_counters = fd->getReadCounters();
    std::optional<AsyncReadIncrement> increment = read_counters ? std::optional<AsyncReadIncrement>(read_counters) : std::nullopt;

    {
        ProfileEventTimeIncrement<Microseconds> elapsed(ProfileEvents::ThreadpoolReaderPrepareMicroseconds);
        if (!seek_performed)
        {
            if (!page_cache_reader)
                reader.set(request.buf, request.size);
            reader.seek(request.offset, SEEK_SET);
        }