Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_file][improve]improve performance #501

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

qicosmos
Copy link
Collaborator

Why

improve coro_file performance: make sure fopen, fread, fwrite, fclose in the same thread, otherwise will cause poor performance.

support pread and pwrite.

What is changing

Example

  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_write);
  std::string str = "hello";

  {
    auto ec = co_await file.async_write(str.data(), str.size());
    std::string result;
    result.resize(10);
    file.seek(0, SEEK_SET);
    auto [rd_ec, size] = co_await file.async_read(result.data(), 5);
    std::string_view s(result.data(), size);
    CHECK(s == "hello");
  }

pread pwrite example: linux and enable USE_PREAD_WRITE

#if defined(__GNUC__) and defined(USE_PREAD_WRITE)
    coro_io::coro_file file{};
    async_simple::coro::syncAwait(
        file.async_open(filename.data(), coro_io::flags::read_write));
    CHECK(file.is_open());

    std::string buf = "cccccccccc";
    auto ec = async_simple::coro::syncAwait(
        file.async_write(0, buf.data(), buf.size()));
    CHECK(!ec);

    std::string buf1 = "dddddddddd";
    ec = async_simple::coro::syncAwait(
        file.async_write(10, buf1.data(), buf1.size()));
    CHECK(!ec);

    char buf2[100];
    auto pair = async_simple::coro::syncAwait(file.async_read(0, buf2, 10));
    CHECK(!file.eof());
    CHECK(std::string_view(buf2, pair.second) == "cccccccccc");

    pair = async_simple::coro::syncAwait(file.async_read(10, buf2, 10));
    CHECK(!file.eof());
    CHECK(std::string_view(buf2, pair.second) == "dddddddddd");
#endif

@qicosmos qicosmos changed the title improve performance [coro_file][improve]improve performance Nov 21, 2023
@qicosmos qicosmos merged commit 4935782 into alibaba:main Nov 21, 2023
30 checks passed
@qicosmos qicosmos deleted the improve_coro_file branch November 21, 2023 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant