66#include " jsg.h"
77#include " setup.h"
88
9+ #include < workerd/util/autogate.h>
10+
911#include < kj/mutex.h>
1012
1113#include < set>
@@ -365,53 +367,46 @@ static CompilationObserver::Option convertOption(ModuleInfoCompileOption option)
365367v8::Local<v8::Module> compileEsmModule (jsg::Lock& js,
366368 kj::StringPtr name,
367369 kj::ArrayPtr<const char > content,
370+ kj::ArrayPtr<const kj::byte> compileCache,
368371 ModuleInfoCompileOption option,
369372 const CompilationObserver& observer) {
370373 // destroy the observer after compilation finished to indicate the end of the process.
371374 auto compilationObserver =
372375 observer.onEsmCompilationStart (js.v8Isolate , name, convertOption (option));
373376
374377 // Must pass true for `is_module`, but we can skip everything else.
375- const int resourceLineOffset = 0 ;
376- const int resourceColumnOffset = 0 ;
377- const bool resourceIsSharedCrossOrigin = false ;
378- const int scriptId = -1 ;
379- const bool resourceIsOpaque = false ;
380- const bool isWasm = false ;
381- const bool isModule = true ;
378+ constexpr int resourceLineOffset = 0 ;
379+ constexpr int resourceColumnOffset = 0 ;
380+ constexpr bool resourceIsSharedCrossOrigin = false ;
381+ constexpr int scriptId = -1 ;
382+ constexpr bool resourceIsOpaque = false ;
383+ constexpr bool isWasm = false ;
384+ constexpr bool isModule = true ;
382385 v8::ScriptOrigin origin (v8StrIntern (js.v8Isolate , name), resourceLineOffset, resourceColumnOffset,
383386 resourceIsSharedCrossOrigin, scriptId, {}, resourceIsOpaque, isWasm, isModule);
384387 v8::Local<v8::String> contentStr;
385- v8::ScriptCompiler::CachedData* existingCacheData = nullptr ;
386- auto compileOptions = v8::ScriptCompiler::kNoCompileOptions ;
387- const auto & compileCache = CompileCache::get ();
388388
389389 if (option == ModuleInfoCompileOption::BUILTIN) {
390390 // TODO(later): Use of newExternalOneByteString here limits our built-in source
391391 // modules (for which this path is used) to only the latin1 character set. We
392392 // may need to revisit that to import built-ins as UTF-16 (two-byte).
393393 contentStr = jsg::newExternalOneByteString (js, content);
394-
395- // We only enable compile cache for built-in modules for now.
396- KJ_IF_SOME (cached, compileCache.find (name)) {
397- compileOptions = v8::ScriptCompiler::kConsumeCodeCache ;
398- existingCacheData = cached.AsCachedData ().release ();
399- }
400394 } else {
401395 contentStr = jsg::v8Str (js.v8Isolate , content);
402396 }
403397
404- v8::ScriptCompiler::Source source (contentStr, origin, existingCacheData);
405- auto module =
406- jsg::check ( v8::ScriptCompiler::CompileModule (js. v8Isolate , &source, compileOptions));
407-
408- if (existingCacheData == nullptr ) {
409- auto cachedData = std::shared_ptr< v8::ScriptCompiler::CachedData> (
410- v8::ScriptCompiler::CreateCodeCache ( module -> GetUnboundModuleScript () ));
411- compileCache. add (name, kj::mv (cachedData));
398+ if ( util::Autogate::isEnabled (util::AutogateKey::COMPILE_CACHE_FOR_BUILTINS)) {
399+ if (compileCache. size () > 0 && compileCache. begin () != nullptr ) {
400+ auto cached = std::make_unique< v8::ScriptCompiler::CachedData>(
401+ compileCache. begin (), compileCache. size ());
402+ v8::ScriptCompiler::Source source (contentStr, origin, cached. release ());
403+ return jsg::check ( v8::ScriptCompiler::CompileModule (
404+ js. v8Isolate , &source, v8::ScriptCompiler::kConsumeCodeCache ));
405+ }
412406 }
413407
414- return module ;
408+ v8::ScriptCompiler::Source source (contentStr, origin);
409+ return jsg::check (v8::ScriptCompiler::CompileModule (js.v8Isolate , &source));
415410}
416411
417412v8::Local<v8::Module> createSyntheticModule (
@@ -438,9 +433,10 @@ ModuleRegistry::ModuleInfo::ModuleInfo(
438433ModuleRegistry::ModuleInfo::ModuleInfo (jsg::Lock& js,
439434 kj::StringPtr name,
440435 kj::ArrayPtr<const char > content,
436+ kj::ArrayPtr<const kj::byte> compileCache,
441437 ModuleInfoCompileOption flags,
442438 const CompilationObserver& observer)
443- : ModuleInfo(js, compileEsmModule(js, name, content, flags, observer)) {}
439+ : ModuleInfo(js, compileEsmModule(js, name, content, compileCache, flags, observer)) {}
444440
445441ModuleRegistry::ModuleInfo::ModuleInfo (jsg::Lock& js,
446442 kj::StringPtr name,
0 commit comments