Skip to content

Commit 02e939d

Browse files
committed
perf(runtime): spell each module URL once per key
NormalizeHttpModuleUrl rejects a string that cannot be an http(s) URL from its first character instead of attempting a parse, so file paths pay nothing for it. ParseModuleUrl returns the parser's href unchanged when the written authority already matches it, skipping the rebuild and its re-parse. CanonicalizeHttpUrlKey splits into the spelling step and CanonicalizeNormalizedHttpUrlKey, which applies the vocabulary to an already spelled URL. CanonicalizeRegistryKey, the graph walk (enqueue, fetch completion, root), the dynamic-import HTTP branch, LoadESModule and LoadHttpModuleForUrl, whose callers pass resolved URLs, spell a URL at most once.
1 parent 0d28268 commit 02e939d

7 files changed

Lines changed: 54 additions & 26 deletions

File tree

‎NativeScript/runtime/HttpLoader.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ struct CanonicalizationConfig {
5858
// module under exactly one URL and never varies it for freshness.
5959
std::string CanonicalizeHttpUrlKey(const std::string& url);
6060

61+
// CanonicalizeHttpUrlKey for a URL already spelled by NormalizeHttpModuleUrl.
62+
std::string CanonicalizeNormalizedHttpUrlKey(const std::string& normalizedUrl);
63+
6164
// What a module response turned out to be. Decided once, by the shared
6265
// classifier, for whichever transport produced the response.
6366
enum class ModuleResponseKind {

‎NativeScript/runtime/HttpLoader.mm‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ bool IsRemoteUrlAllowed(const std::string& url) {
138138
// clean `/ns/core/*` imports) carry no query, so they canonicalize identically
139139
// under any vocabulary.
140140
std::string CanonicalizeHttpUrlKey(const std::string& url) {
141+
const std::string normalizedUrl = NormalizeHttpModuleUrl(url);
142+
return normalizedUrl.empty() ? url : CanonicalizeNormalizedHttpUrlKey(normalizedUrl);
143+
}
144+
145+
std::string CanonicalizeNormalizedHttpUrlKey(const std::string& normalizedUrl) {
141146
// Per-isolate vocabulary, so this runs on the isolate's own thread. Fetch
142147
// threads never reach here — they carry keys computed for them.
143148
const CanonicalizationConfig* canonConfig = CanonicalizationConfigForCurrentIsolate();
144-
const std::string normalizedUrl = NormalizeHttpModuleUrl(url);
145-
if (normalizedUrl.empty()) {
146-
return url;
147-
}
148149
// Drop fragment entirely
149150
size_t hashPos = normalizedUrl.find('#');
150151
std::string noHash =

‎NativeScript/runtime/ModuleInternal.mm‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static bool IsHttpModulePath(const std::string& path) {
8383
static std::string CanonicalizeModulePath(const std::string& path) {
8484
const std::string url = NormalizeHttpModuleUrl(path);
8585
if (!url.empty()) {
86-
return CanonicalizeHttpUrlKey(url);
86+
return CanonicalizeNormalizedHttpUrlKey(url);
8787
}
8888

8989
return NormalizePath(path);
@@ -1466,9 +1466,11 @@ throw NativeScriptException("Module evaluation interrupted by isolate terminatio
14661466
14671467
Local<Value> ModuleInternal::LoadESModule(Isolate* isolate, const std::string& path,
14681468
const ModuleEvaluationOptions& options) {
1469-
bool isHttpModule = IsHttpModulePath(path);
1470-
std::string canonicalPath = CanonicalizeModulePath(path);
1471-
std::string requestPath = isHttpModule ? NormalizeHttpModuleUrl(path) : canonicalPath;
1469+
const std::string httpUrl = NormalizeHttpModuleUrl(path);
1470+
bool isHttpModule = !httpUrl.empty();
1471+
std::string canonicalPath =
1472+
isHttpModule ? CanonicalizeNormalizedHttpUrlKey(httpUrl) : NormalizePath(path);
1473+
std::string requestPath = isHttpModule ? httpUrl : canonicalPath;
14721474
auto context = isolate->GetCurrentContext();
14731475
auto* registryPtr = ModuleRegistryFor(isolate);
14741476
if (registryPtr == nullptr) {

‎NativeScript/runtime/ModuleInternalCallbacks.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ v8::MaybeLocal<v8::Module> GetOrCreateRequireFacade(
115115

116116
// Authoritative HTTP URL loader for dev-served ESM. This compiles and
117117
// registers the module under its canonical URL key without evaluating it.
118+
// `requestedUrl` must be spelled by NormalizeHttpModuleUrl: it is fetched as is
119+
// and its key derives from it without spelling it again.
118120
v8::MaybeLocal<v8::Module> LoadHttpModuleForUrl(
119121
v8::Isolate* isolate, v8::Local<v8::Context> context,
120122
const std::string& requestedUrl);

‎NativeScript/runtime/ModuleInternalCallbacks.mm‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,9 @@ static bool ShouldTraceRegistryKey(const std::string& rawKey, const std::string&
777777
// Before any other scheme test: an http(s) URL in a spelling other than the
778778
// key's (`HTTP://`, `http:/host`) must not fall through as a custom scheme
779779
// and give the module a second identity.
780-
if (!NormalizeHttpModuleUrl(key).empty()) {
781-
registryKey = CanonicalizeHttpUrlKey(key);
780+
const std::string httpUrl = NormalizeHttpModuleUrl(key);
781+
if (!httpUrl.empty()) {
782+
registryKey = CanonicalizeNormalizedHttpUrlKey(httpUrl);
782783
classification = "http";
783784
} else if (StartsWith(key, "file://")) {
784785
registryKey = NormalizePath(FileURLToPath(key));
@@ -812,15 +813,13 @@ static bool ShouldTraceRegistryKey(const std::string& rawKey, const std::string&
812813
813814
v8::MaybeLocal<v8::Module> LoadHttpModuleForUrl(v8::Isolate* isolate,
814815
v8::Local<v8::Context> context,
815-
const std::string& requestedUrlArg) {
816+
const std::string& requestedUrl) {
816817
auto* moduleState = ModuleLoaderStateFor(isolate);
817818
if (moduleState == nullptr) {
818819
return v8::MaybeLocal<v8::Module>();
819820
}
820821
auto& registry = moduleState->registry;
821-
const std::string normalizedUrl = NormalizeHttpModuleUrl(requestedUrlArg);
822-
const std::string& requestedUrl = normalizedUrl.empty() ? requestedUrlArg : normalizedUrl;
823-
const std::string registryKey = CanonicalizeHttpUrlKey(requestedUrl);
822+
const std::string registryKey = CanonicalizeNormalizedHttpUrlKey(requestedUrl);
824823
825824
TNS_DEBUG(Esm, "[http-esm][load][begin] request=%s key=%s", requestedUrl.c_str(),
826825
registryKey.c_str());
@@ -1974,7 +1973,7 @@ static void AsyncGraphOnFetchCompleted(const std::shared_ptr<AsyncGraphLoad>& lo
19741973
19751974
load->pendingFetches--;
19761975
1977-
const std::string key = CanonicalizeHttpUrlKey(url);
1976+
const std::string key = CanonicalizeNormalizedHttpUrlKey(url);
19781977
const bool isRoot = (key == load->rootKey);
19791978
19801979
if (!load->failed) {
@@ -2096,10 +2095,11 @@ static void AsyncGraphEnqueue(const std::shared_ptr<AsyncGraphLoad>& load,
20962095
const ModuleResolution& resolution) {
20972096
const bool isHttp = resolution.kind == ModuleResolution::Kind::kHttp;
20982097
const std::string& target = isHttp ? resolution.url : resolution.path;
2099-
// One keying function for both schemes: it dispatches to the HTTP canonical
2100-
// key for URLs and to the normalized path otherwise, so the walk registers
2101-
// every module under the exact key the resolver will look up.
2102-
const std::string key = CanonicalizeRegistryKey(target);
2098+
// The keys CanonicalizeRegistryKey gives these targets, so the walk registers
2099+
// every module under the exact key the resolver will look up; a resolved
2100+
// URL is already spelled, so only its vocabulary is applied.
2101+
const std::string key =
2102+
isHttp ? CanonicalizeNormalizedHttpUrlKey(target) : CanonicalizeRegistryKey(target);
21032103
if (!load->visited.insert(key).second) {
21042104
return;
21052105
}
@@ -2207,10 +2207,13 @@ void StartModuleGraphLoad(
22072207
double fetchDeadline,
22082208
std::function<void(bool ok, const std::string& errorMessage, v8::Local<v8::Context> context)>
22092209
onComplete) {
2210+
const ModuleResolution rootResolution = ResolutionForRoot(root);
22102211
auto load = std::make_shared<AsyncGraphLoad>();
22112212
load->isolate = isolate;
22122213
load->context.Reset(isolate, context);
2213-
load->rootKey = CanonicalizeRegistryKey(root);
2214+
load->rootKey = rootResolution.kind == ModuleResolution::Kind::kHttp
2215+
? CanonicalizeNormalizedHttpUrlKey(rootResolution.url)
2216+
: CanonicalizeRegistryKey(root);
22142217
load->startUs = (uint64_t)(CFAbsoluteTimeGetCurrent() * 1000.0 * 1000.0);
22152218
load->fetchDeadline = fetchDeadline;
22162219
load->onComplete = std::move(onComplete);
@@ -2220,7 +2223,6 @@ void StartModuleGraphLoad(
22202223
22212224
TNS_DEBUG(Esm, "[graph][start] root=%s key=%s", root.c_str(), load->rootKey.c_str());
22222225
2223-
const ModuleResolution rootResolution = ResolutionForRoot(root);
22242226
if (rootResolution.kind != ModuleResolution::Kind::kUnresolved) {
22252227
AsyncGraphEnqueue(load, rootResolution);
22262228
}
@@ -3462,7 +3464,7 @@ static void FinishHttpDynamicImport(v8::Isolate* isolate, v8::Local<v8::Context>
34623464
// Security: HttpFetchModule gates remote module access centrally.
34633465
if (dynamicResolution.kind == ModuleResolution::Kind::kHttp) {
34643466
TNS_DEBUG(Esm, "[dyn-import][http-loader] trying URL %s", normalizedSpec.c_str());
3465-
std::string key = CanonicalizeHttpUrlKey(normalizedSpec);
3467+
std::string key = CanonicalizeNormalizedHttpUrlKey(normalizedSpec);
34663468
// Volatile pattern check: if the URL matches any configured volatile
34673469
// pattern, evict the cached module so we always re-fetch. The pattern
34683470
// list is policy and is supplied exclusively by the dev client via

‎NativeScript/runtime/ModuleUrl.cpp‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,18 @@ ModuleUrl ParseModuleUrl(const std::string& input, const std::string& base) {
7979
if (authority.empty()) {
8080
return result;
8181
}
82+
const std::string_view href = url->get_href();
83+
const size_t authorityStart = url->get_protocol().size() + 2;
84+
const size_t pathnameStart = url->get_components().pathname_start;
85+
if (href.substr(authorityStart, pathnameStart - authorityStart) ==
86+
authority) {
87+
return result;
88+
}
8289
// Everything after the authority comes from the parser's own href: its
8390
// getters drop an empty query's '?' and an empty fragment's '#'.
84-
const std::string_view href = url->get_href();
85-
const std::string written =
86-
std::string(url->get_protocol()) + "//" + authority +
87-
std::string(href.substr(url->get_components().pathname_start));
91+
const std::string written = std::string(url->get_protocol()) + "//" +
92+
authority +
93+
std::string(href.substr(pathnameStart));
8894
auto reparsed = ada::parse<ada::url_aggregator>(written, nullptr);
8995
if (reparsed && reparsed->get_href() == href) {
9096
result.href = written;
@@ -93,6 +99,16 @@ ModuleUrl ParseModuleUrl(const std::string& input, const std::string& base) {
9399
}
94100

95101
std::string NormalizeHttpModuleUrl(const std::string& input) {
102+
// Fast reject only: the parser drops leading C0 controls and spaces, after
103+
// which an http(s) URL (or an HTTP key) starts with 'h' or 'H'.
104+
size_t first = 0;
105+
while (first < input.size() &&
106+
static_cast<unsigned char>(input[first]) <= 0x20) {
107+
first++;
108+
}
109+
if (first == input.size() || (input[first] != 'h' && input[first] != 'H')) {
110+
return std::string();
111+
}
96112
const ModuleUrl url = ParseModuleUrl(input, std::string());
97113
if (url.parsed) {
98114
return url.http ? url.href : std::string();

‎TestRunner/app/tests/HttpEsmLoaderTests.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,8 @@ describe("HTTP canonical key (ns:module canonicalizeHttpUrlKey)", function () {
25902590
checkKey("HTTP://h/x", "http://h/x");
25912591
checkKey("http:h/x", "http://h/x");
25922592
checkKey(" http://h/x", "http://h/x");
2593+
checkKey("\t\n HTTPS://h/x", "https://h/x");
2594+
checkKey("h\tttp://h/x", "http://h/x");
25932595
checkKey("http://h", "http://h/");
25942596
checkKey("http://h?x", "http://h/?x");
25952597
checkKey("http://h/a/../b.js", "http://h/b.js");

0 commit comments

Comments
 (0)