Commit 951a822c authored by Patryk Zaryjewski's avatar Patryk Zaryjewski Committed by Facebook Github Bot

Make cancelling and rescheduling of functions O(1)

Summary: Currently FunctionScheduler calls that cancel/restart timer for a function of particular id are O(n). By introducing hashmap that translate id to pointer of particular RepeatFunc, we make it O(1).

Reviewed By: simpkins

Differential Revision: D5668557

fbshipit-source-id: e5e8bf9bd75b6d5d42f0bfa398d476703e5801fa
parent db9fd491
This diff is collapsed.
......@@ -18,11 +18,13 @@
#include <folly/Function.h>
#include <folly/Range.h>
#include <folly/Hash.h>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <unordered_map>
namespace folly {
......@@ -241,20 +243,21 @@ class FunctionScheduler {
};
struct RunTimeOrder {
bool operator()(const RepeatFunc& f1, const RepeatFunc& f2) const {
return f1.getNextRunTime() > f2.getNextRunTime();
bool operator()(const std::unique_ptr<RepeatFunc>& f1, const std::unique_ptr<RepeatFunc>& f2) const {
return f1->getNextRunTime() > f2->getNextRunTime();
}
};
typedef std::vector<RepeatFunc> FunctionHeap;
typedef std::vector<std::unique_ptr<RepeatFunc>> FunctionHeap;
typedef std::unordered_map<StringPiece, RepeatFunc*, Hash> FunctionMap;
void run();
void runOneFunction(std::unique_lock<std::mutex>& lock,
std::chrono::steady_clock::time_point now);
void cancelFunction(const std::unique_lock<std::mutex>& lock,
FunctionHeap::iterator it);
RepeatFunc* it);
void addFunctionToHeap(const std::unique_lock<std::mutex>& lock,
RepeatFunc&& func);
std::unique_ptr<RepeatFunc> func);
void addFunctionInternal(
Function<void()>&& cb,
......@@ -279,6 +282,7 @@ class FunctionScheduler {
// The functions to run.
// This is a heap, ordered by next run time.
FunctionHeap functions_;
FunctionMap functionsMap_;
RunTimeOrder fnCmp_;
// The function currently being invoked by the running thread.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment