Skip to content

Commit

Permalink
Micro-optimize Module._resolveFilename (#13322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Aug 15, 2024
1 parent 36fc324 commit 3f68622
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/bun.js/bindings/CommonJSModuleRecord.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include "root.h"

#include "JavaScriptCore/JSGlobalObject.h"
#include "JavaScriptCore/JSString.h"
#include "root.h"
#include "headers-handwritten.h"
#include "wtf/NakedPtr.h"

Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/bindings/ImportMetaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static JSC::EncodedJSValue functionRequireResolve(JSC::JSGlobalObject* globalObj
// require.resolve also supports a paths array
// we only support a single path
if (!fromValue.isUndefinedOrNull() && fromValue.isObject()) {
if (auto pathsObject = fromValue.getObject()->getIfPropertyExists(globalObject, JSC::Identifier::fromString(vm, "paths"_s))) {
if (auto pathsObject = fromValue.getObject()->getIfPropertyExists(globalObject, builtinNames(vm).pathsPublicName())) {
if (pathsObject.isCell() && pathsObject.asCell()->type() == JSC::JSType::ArrayType) {
auto pathsArray = JSC::jsCast<JSC::JSArray*>(pathsObject);
if (pathsArray->length() > 0) {
Expand Down Expand Up @@ -209,7 +209,7 @@ extern "C" JSC::EncodedJSValue functionImportMeta__resolveSync(JSC::JSGlobalObje

if (!fromValue.isUndefinedOrNull() && fromValue.isObject()) {

if (auto pathsObject = fromValue.getObject()->getIfPropertyExists(globalObject, JSC::Identifier::fromString(vm, "paths"_s))) {
if (auto pathsObject = fromValue.getObject()->getIfPropertyExists(globalObject, builtinNames(vm).pathsPublicName())) {
if (pathsObject.isCell() && pathsObject.asCell()->type() == JSC::JSType::ArrayType) {
auto pathsArray = JSC::jsCast<JSC::JSArray*>(pathsObject);
if (pathsArray->length() > 0) {
Expand Down Expand Up @@ -358,7 +358,7 @@ JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolve,
JSValue fromValue = callFrame->uncheckedArgument(1);

if (!fromValue.isUndefinedOrNull() && fromValue.isObject()) {
if (JSValue pathsObject = fromValue.getObject()->getIfPropertyExists(globalObject, JSC::Identifier::fromString(vm, "paths"_s))) {
if (JSValue pathsObject = fromValue.getObject()->getIfPropertyExists(globalObject, builtinNames(vm).pathsPublicName())) {
if (pathsObject.isCell() && pathsObject.asCell()->type() == JSC::JSType::ArrayType) {
auto* pathsArray = JSC::jsCast<JSC::JSArray*>(pathsObject);
if (pathsArray->length() > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/NodeVM.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "root.h"
#include "JavaScriptCore/ExecutableInfo.h"

#include "BunClientData.h"
#include "NodeVM.h"
#include "JavaScriptCore/JSObjectInlines.h"
#include "wtf/text/ExternalStringImpl.h"
Expand Down Expand Up @@ -55,7 +55,7 @@ class ScriptOptions {
}
JSObject* options = asObject(optionsArg);

if (JSValue filenameOpt = options->getIfPropertyExists(globalObject, Identifier::fromString(vm, "filename"_s))) {
if (JSValue filenameOpt = options->getIfPropertyExists(globalObject, builtinNames(vm).filenamePublicName())) {
if (filenameOpt.isString()) {
opts.filename = filenameOpt.toWTFString(globalObject);
any = true;
Expand Down
18 changes: 11 additions & 7 deletions src/bun.js/modules/NodeModuleModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionResolveFileName, (JSC::JSGlobalObject * globa
// weird thing.
(fromValue.isObject()) {

if (auto idValue = fromValue.getObject()->getIfPropertyExists(globalObject, Identifier::fromString(vm, "filename"_s))) {
if (auto idValue = fromValue.getObject()->getIfPropertyExists(globalObject, builtinNames(vm).filenamePublicName())) {
if (idValue.isString()) {
fromValue = idValue;
}
Expand Down Expand Up @@ -334,21 +334,25 @@ struct Parent {

Parent getParent(VM&vm, JSGlobalObject* global, JSValue maybe_parent) {
Parent value { nullptr, nullptr };
if (!maybe_parent.isCell()) {

if (!maybe_parent) {
return value;
}
if (!maybe_parent.isObject()) {

auto parent = maybe_parent.getObject();
if (!parent) {
return value;
}
auto parent = maybe_parent.getObject();

auto scope = DECLARE_THROW_SCOPE(vm);
JSValue paths = parent->get(global, Identifier::fromString(vm, "paths"_s));
const auto& builtinNames = Bun::builtinNames(vm);
JSValue paths = parent->get(global, builtinNames.pathsPublicName());
RETURN_IF_EXCEPTION(scope, value);
if (paths.isCell()) {
value.paths = jsDynamicCast<JSArray*>(paths);
}

JSValue filename = parent->get(global, Identifier::fromString(vm, "filename"_s));
JSValue filename = parent->get(global, builtinNames.filenamePublicName());
RETURN_IF_EXCEPTION(scope, value);
if (filename.isString()) {
value.filename = filename.toString(global);
Expand Down Expand Up @@ -517,7 +521,7 @@ DEFINE_NATIVE_MODULE(NodeModule) {
putNativeFn(Identifier::fromString(vm, "_resolveLookupPaths"_s), jsFunctionResolveLookupPaths);

putNativeFn(Identifier::fromString(vm, "createRequire"_s), jsFunctionNodeModuleCreateRequire);
putNativeFn(Identifier::fromString(vm, "paths"_s), Resolver__nodeModulePathsForJS);
putNativeFn(builtinNames(vm).pathsPublicName(), Resolver__nodeModulePathsForJS);
putNativeFn(Identifier::fromString(vm, "findSourceMap"_s), jsFunctionFindSourceMap);
putNativeFn(Identifier::fromString(vm, "syncBuiltinExports"_s), jsFunctionSyncBuiltinExports);
putNativeFn(Identifier::fromString(vm, "SourceMap"_s), jsFunctionSourceMap);
Expand Down
3 changes: 2 additions & 1 deletion src/js/builtins/BunBuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ using namespace JSC;
macro(fetch) \
macro(fetchRequest) \
macro(file) \
macro(filePath) \
macro(filename) \
macro(fillFromJS) \
macro(finishConsumingStream) \
macro(flush) \
Expand Down Expand Up @@ -152,6 +152,7 @@ using namespace JSC;
macro(password) \
macro(patch) \
macro(path) \
macro(paths) \
macro(pathname) \
macro(pause) \
macro(pendingAbortRequest) \
Expand Down

0 comments on commit 3f68622

Please sign in to comment.