From b949e61752224aafaeaa8b210a656beda9038839 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 15 Feb 2018 12:09:27 -0500 Subject: [PATCH] omg optimize --- .gitignore | 4 +++- Makefile | 2 ++ llarp/ev.cpp | 24 +++++++++++------------- test/test_async_cipher.c | 6 +++--- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index bc0c35b2e..ee6fc24fb 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ llarpd *.test -*.bin \ No newline at end of file +*.bin + +callgrind.* \ No newline at end of file diff --git a/Makefile b/Makefile index b2a5bb5a2..9bcc55471 100644 --- a/Makefile +++ b/Makefile @@ -65,11 +65,13 @@ $(REPO)/test/%.cpp.bin: $(REPO)/test/%.cpp $(CXX) $(REQUIRED_CXXFLAGS) $< -o $<.bin $(TEST_LDFLAGS) mv $<.bin $<.test $<.test + valgrind --tool=callgrind $<.test $(REPO)/test/%.c.bin: $(REPO)/test/%.c $(CC) $(REQUIRED_CFLAGS) $< -o $<.bin $(TEST_LDFLAGS) mv $<.bin $<.test $<.test + valgrind --tool=callgrind $<.test $(EXE): $(DAEMON_OBJ) $(STATIC_LIB) $(CXX) $(DAEMON_OBJ) $(STATIC_LIB) $(LIB_LDFLAGS) -o $(EXE) diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 6dd2d5f4c..f4ad1f1dc 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -5,6 +5,7 @@ #include #include + struct llarp_ev_caller { static void *operator new(size_t sz) { return llarp::Alloc(); @@ -21,37 +22,34 @@ struct llarp_ev_caller { bool appendCall(void *user) { std::unique_lock lock(access); - bool should = pending.size() == 0; - llarp_ev_async_call *call = - new llarp_ev_async_call{loop, this, user, this->work}; - pending.push(call); - return should; + pending.emplace_back(std::move(llarp_ev_async_call{loop, this, user, this->work})); + return true; } bool appendManyCalls(void **users, size_t n) { std::unique_lock lock(access); - bool should = pending.size() == 0; while (n--) { - pending.push(new llarp_ev_async_call{loop, this, *users, this->work}); + pending.emplace_back(std::move(llarp_ev_async_call{loop, this, *users, this->work})); users++; } - return should; + return true; } void Call() { std::unique_lock lock(access); - while (pending.size() > 0) { + auto sz = pending.size(); + while (sz > 0) { auto &front = pending.front(); - front->work(front); - pending.pop(); - delete front; + front.work(&front); + pending.pop_front(); + --sz; } } std::mutex access; struct llarp_ev_loop *loop; uv_async_t async; - std::queue pending; + std::deque pending; llarp_ev_work_func work; }; diff --git a/test/test_async_cipher.c b/test/test_async_cipher.c index 98fe80009..bc372ff40 100644 --- a/test/test_async_cipher.c +++ b/test/test_async_cipher.c @@ -35,14 +35,14 @@ int main(int argc, char *argv[]) { struct bench_main b_main; struct llarp_threadpool *tp; - llarp_mem_jemalloc(); + llarp_mem_std(); llarp_crypto_libsodium_init(&b_main.crypto); llarp_ev_loop_alloc(&b_main.ev); tp = llarp_init_threadpool(2); - b_main.num = 10000000; - b_main.jobs = 10000; + b_main.num = 100000; + b_main.jobs = 1000; b_main.completed = 0; llarp_sharedkey_t key; b_main.crypto.randbytes(key, sizeof(llarp_sharedkey_t));