From 97de144574f683cff9e5bf9732eb2ff50ca7a2cf Mon Sep 17 00:00:00 2001 From: Sidorov Aleksei Date: Fri, 14 Apr 2023 21:40:04 +0800 Subject: [PATCH] [Runtime] Make global Random Engine for BuiltinsMath::Random The profiler says that creating a random engine is a heavy operation. That is why I created global Random Engine and put it in Ecma VM. Signed-off-by: Sidorov Aleksei --- runtime/builtins/builtins_math.cpp | 6 ++---- runtime/ecma_vm.cpp | 1 + runtime/ecma_vm.h | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/runtime/builtins/builtins_math.cpp b/runtime/builtins/builtins_math.cpp index f58d08125..f76318f8f 100644 --- a/runtime/builtins/builtins_math.cpp +++ b/runtime/builtins/builtins_math.cpp @@ -565,11 +565,9 @@ JSTaggedValue BuiltinsMath::Random([[maybe_unused]] EcmaRuntimeCallInfo *argv) { ASSERT(argv); BUILTINS_API_TRACE(argv->GetThread(), Math, Random); - std::random_device rd; - std::default_random_engine engine(rd()); - std::uniform_real_distribution dis(0, std::random_device::max() - 1); // result range [0,1) - double result = dis(engine) / std::random_device::max(); + std::uniform_real_distribution dis(0.0, 1.0); + double result = dis(argv->GetThread()->GetEcmaVM()->GetRandomEngine()); return GetTaggedDouble(result); } diff --git a/runtime/ecma_vm.cpp b/runtime/ecma_vm.cpp index 526474811..d78758796 100644 --- a/runtime/ecma_vm.cpp +++ b/runtime/ecma_vm.cpp @@ -171,6 +171,7 @@ EcmaVM::EcmaVM(JSRuntimeOptions options) : string_table_(new EcmaStringTable(thi rendezvous_ = allocator->New(this); notification_manager_ = allocator->New(runtime->GetInternalAllocator()); notification_manager_->SetRendezvous(rendezvous_); + InitializeRandomEngine(); LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::ECMASCRIPT); mm_ = CreateMM(ctx, Runtime::GetCurrent()->GetInternalAllocator(), options_); diff --git a/runtime/ecma_vm.h b/runtime/ecma_vm.h index 9e7c26d68..a08f908cd 100644 --- a/runtime/ecma_vm.h +++ b/runtime/ecma_vm.h @@ -16,6 +16,7 @@ #ifndef ECMASCRIPT_ECMA_VM_H #define ECMASCRIPT_ECMA_VM_H +#include #include #include "include/mem/panda_containers.h" @@ -467,6 +468,12 @@ public: return MEMBER_OFFSET(EcmaVM, global_env_); } + std::default_random_engine &GetRandomEngine() + { + ASSERT(random_engine_); + return *random_engine_; + } + protected: bool CheckEntrypointSignature([[maybe_unused]] Method *entrypoint) override { @@ -512,6 +519,13 @@ private: void LoadEcmaStdLib(); const panda_file::File *GetLastLoadedPandaFile(); + void InitializeRandomEngine() + { + ASSERT(!random_engine_); + std::random_device rd; + random_engine_.emplace(rd()); + } + NO_MOVE_SEMANTIC(EcmaVM); NO_COPY_SEMANTIC(EcmaVM); @@ -575,6 +589,8 @@ private: HostPromiseRejectionTracker host_promise_rejection_tracker_ {nullptr}; void *data_ {nullptr}; PandaList finalization_registries_; + // optional for lazy initialization + std::optional random_engine_; friend class ObjectFactory; friend class panda::JSNApi; -- Gitee