perf: have ErrorThrower
lazily lookup the current isolate (#46415)
perf: have ErrorThrower lazy-lookup the current isolate ErrorThrower's default constructor is marked as "should rarely if ever be used" because it's expensive to call. Unfortunately, nearly every instance of ErrorThrower comes as an argument in gin_helper's JS-->C++ function marshalling where a thrower is default-constructed and then populated in gin_helper::GetNextArgument() with an assignment operator to a temporary ErrorThrower constructed with the gin::Arguments' isolate. tldr: most of the time we use the slow constructor first, then throw that work away unused by overwriting with a fast-constructed one. This refactor avoids that cost by deferring the expensive work to `ErrorThrower::isolate()`, where it happens only as a fallback iff isolate_ hasn't been set. Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
parent
58b2c2e651
commit
01ce103ae1
2 changed files with 13 additions and 13 deletions
|
@ -10,12 +10,11 @@
|
||||||
|
|
||||||
namespace gin_helper {
|
namespace gin_helper {
|
||||||
|
|
||||||
ErrorThrower::ErrorThrower(v8::Isolate* isolate) : isolate_(isolate) {}
|
v8::Isolate* ErrorThrower::isolate() const {
|
||||||
|
// Callers should prefer to specify the isolate in the constructor,
|
||||||
// This constructor should be rarely if ever used, since
|
// since GetCurrent() uses atomic loads and is thus a bit costly to invoke
|
||||||
// v8::Isolate::GetCurrent() uses atomic loads and is thus a bit
|
return isolate_ ? isolate_.get() : v8::Isolate::GetCurrent();
|
||||||
// costly to invoke
|
}
|
||||||
ErrorThrower::ErrorThrower() : isolate_(v8::Isolate::GetCurrent()) {}
|
|
||||||
|
|
||||||
void ErrorThrower::ThrowError(const std::string_view err_msg) const {
|
void ErrorThrower::ThrowError(const std::string_view err_msg) const {
|
||||||
Throw(v8::Exception::Error, err_msg);
|
Throw(v8::Exception::Error, err_msg);
|
||||||
|
@ -39,9 +38,10 @@ void ErrorThrower::ThrowSyntaxError(const std::string_view err_msg) const {
|
||||||
|
|
||||||
void ErrorThrower::Throw(ErrorGenerator gen,
|
void ErrorThrower::Throw(ErrorGenerator gen,
|
||||||
const std::string_view err_msg) const {
|
const std::string_view err_msg) const {
|
||||||
v8::Local<v8::Value> exception = gen(gin::StringToV8(isolate_, err_msg), {});
|
v8::Isolate* isolate = this->isolate();
|
||||||
if (!isolate_->IsExecutionTerminating())
|
|
||||||
isolate_->ThrowException(exception);
|
if (!isolate->IsExecutionTerminating())
|
||||||
|
isolate->ThrowException(gen(gin::StringToV8(isolate, err_msg), {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace gin_helper
|
} // namespace gin_helper
|
||||||
|
|
|
@ -14,8 +14,8 @@ namespace gin_helper {
|
||||||
|
|
||||||
class ErrorThrower {
|
class ErrorThrower {
|
||||||
public:
|
public:
|
||||||
explicit ErrorThrower(v8::Isolate* isolate);
|
constexpr explicit ErrorThrower(v8::Isolate* isolate) : isolate_{isolate} {}
|
||||||
ErrorThrower();
|
constexpr ErrorThrower() = default;
|
||||||
~ErrorThrower() = default;
|
~ErrorThrower() = default;
|
||||||
|
|
||||||
void ThrowError(std::string_view err_msg) const;
|
void ThrowError(std::string_view err_msg) const;
|
||||||
|
@ -24,14 +24,14 @@ class ErrorThrower {
|
||||||
void ThrowReferenceError(std::string_view err_msg) const;
|
void ThrowReferenceError(std::string_view err_msg) const;
|
||||||
void ThrowSyntaxError(std::string_view err_msg) const;
|
void ThrowSyntaxError(std::string_view err_msg) const;
|
||||||
|
|
||||||
v8::Isolate* isolate() const { return isolate_; }
|
v8::Isolate* isolate() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using ErrorGenerator = v8::Local<v8::Value> (*)(v8::Local<v8::String> err_msg,
|
using ErrorGenerator = v8::Local<v8::Value> (*)(v8::Local<v8::String> err_msg,
|
||||||
v8::Local<v8::Value> options);
|
v8::Local<v8::Value> options);
|
||||||
void Throw(ErrorGenerator gen, std::string_view err_msg) const;
|
void Throw(ErrorGenerator gen, std::string_view err_msg) const;
|
||||||
|
|
||||||
raw_ptr<v8::Isolate> isolate_;
|
raw_ptr<v8::Isolate> isolate_ = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gin_helper
|
} // namespace gin_helper
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue