From 03083424de84ca0f41e58f1eeff1b498a9e4affd Mon Sep 17 00:00:00 2001 From: Neil Dhar Date: Fri, 22 Nov 2024 18:23:00 -0800 Subject: [PATCH] Add a fast path for ToInt32 Summary: Like other operations that need an int32, we can add a fast path for ToInt32 that performs the conversion inline. Reviewed By: lavenzg Differential Revision: D65826733 fbshipit-source-id: 6b689e84de3123e8ea25081f26baf997767ebb1c --- lib/VM/Interpreter.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/VM/Interpreter.cpp b/lib/VM/Interpreter.cpp index 855a37e39a5..cce7b127dd3 100644 --- a/lib/VM/Interpreter.cpp +++ b/lib/VM/Interpreter.cpp @@ -2729,11 +2729,18 @@ CallResult Interpreter::interpretFunction( } CASE(ToInt32) { - CAPTURE_IP(res = toInt32_RJS(runtime, Handle<>(&O2REG(ToInt32)))); - if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) - goto exception; - gcScope.flushToSmallCount(KEEP_HANDLES); - O1REG(ToInt32) = res.getValue(); + int32_t argInt; + if (LLVM_LIKELY( + _sh_ljs_tryfast_truncate_to_int32(O2REG(ToInt32), &argInt))) { + /* Fast-path. */ + O1REG(ToInt32) = HermesValue::encodeTrustedNumberValue(argInt); + } else { + CAPTURE_IP(res = toInt32_RJS(runtime, Handle<>(&O2REG(ToInt32)))); + if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) + goto exception; + gcScope.flushToSmallCount(KEEP_HANDLES); + O1REG(ToInt32) = res.getValue(); + } ip = NEXTINST(ToInt32); DISPATCH; }