From d815651455b4d77f086a598ade80faff96a1e294 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 14 Mar 2018 14:14:48 +0800 Subject: [PATCH] src: simplify native_immediate_callbacks - Use std::list instead of std::vector because we do not need random access. - Use list.erase instead of moving elements then resizing to remove the first n elements in the list for simplicity and avoiding reallocation. --- src/env.cc | 5 ++--- src/env.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/env.cc b/src/env.cc index 913f0e865beb1d..f817edc0ff930d 100644 --- a/src/env.cc +++ b/src/env.cc @@ -369,7 +369,7 @@ void Environment::RunAndClearNativeImmediates() { size_t count = native_immediate_callbacks_.size(); if (count > 0) { size_t ref_count = 0; - std::vector list; + std::list list; native_immediate_callbacks_.swap(list); auto drain_list = [&]() { v8::TryCatch try_catch(isolate()); @@ -381,8 +381,7 @@ void Environment::RunAndClearNativeImmediates() { FatalException(isolate(), try_catch); // Bail out, remove the already executed callbacks from list // and set up a new TryCatch for the other pending callbacks. - std::move_backward(it, list.end(), list.begin() + (list.end() - it)); - list.resize(list.end() - it); + list.erase(list.begin(), it); return true; } } diff --git a/src/env.h b/src/env.h index e6060b9e6faab9..4a2ef983422bda 100644 --- a/src/env.h +++ b/src/env.h @@ -845,7 +845,7 @@ class Environment { std::unique_ptr> keep_alive_; bool refed_; }; - std::vector native_immediate_callbacks_; + std::list native_immediate_callbacks_; void RunAndClearNativeImmediates(); static void CheckImmediate(uv_check_t* handle);