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

Deregister cancellation callbacks #1420

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

john4744
Copy link

Potentially resolves the problem described in #589.
We have a workflow where we use the same long-lived cancellation_token_source for lots of tasks. We then use when_all to schedule some work once those tasks complete. We were seeing continuous growth in memory usage, and also in the amount of time it would take to create new tasks and to deregister any cancellation callbacks that were associated with the cancellation_token.
Here is some repro code:

pplx::cancellation_token_source cts;
auto token = cts.get_token();
std::string msg;
auto start_time = std::chrono::high_resolution_clock::now();
auto end_time = std::chrono::high_resolution_clock::now();
std::array<pplx::task<void>, 100> tasks;
std::ofstream ofs("data.csv");
ofs << "construction time,deregister_callback time\n";
for (int i = 0; i < 1000; ++i)
{
  auto token_reg = token.register_callback([&]() {});

  start_time = std::chrono::high_resolution_clock::now();
  for (auto& task : tasks)
  {
    task = pplx::task<void>([&] {}, token);
  }
  end_time = std::chrono::high_resolution_clock::now();
  msg = std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count()) +","; // construction time

  pplx::when_all(std::begin(tasks), std::end(tasks));

  start_time = std::chrono::high_resolution_clock::now();
  token.deregister_callback(token_reg);
  end_time = std::chrono::high_resolution_clock::now();
  msg += std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count()) + "\n"; // deregister_callback

  ofs << msg;
}

Test Results On Master
Takes about 30s to execute. Memory usage starts around 2.4MB and climbs to around 13MB (using VS memory diagnostic tool).
The timing data gathered in the test is reflected in these charts:
image
image

Test Results On This Branch
Takes about 1s to execute. The VS memory diagnostic tool didn't have enough time to take a sample, so I increased the number of loop iterations to 100,000. The Memory usage starts around 2.2MB and stayed around there, ending at 2.4MB.
The timing data gathered in the test is reflected in these charts:
image
image

@msftclas
Copy link

msftclas commented May 14, 2020

CLA assistant check
All CLA requirements met.

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.

2 participants