Skip to content

Commit

Permalink
runtime: remove "__profiler" flag
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 6, 2021
1 parent 18694cd commit 488fe7c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 90 deletions.
47 changes: 0 additions & 47 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2163,20 +2163,12 @@ func (b *Bundle) generateMetadataJSON(results []graph.OutputFile, allReachableFi
type runtimeCacheKey struct {
MangleSyntax bool
MinifyIdentifiers bool
ProfilerNames bool
ES6 bool
}

type definesCacheKey struct {
profilerNames bool
}

type runtimeCache struct {
astMutex sync.Mutex
astMap map[runtimeCacheKey]js_ast.AST

definesMutex sync.Mutex
definesMap map[definesCacheKey]*config.ProcessedDefines
}

var globalRuntimeCache runtimeCache
Expand All @@ -2186,7 +2178,6 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.
// All configuration options that the runtime code depends on must go here
MangleSyntax: options.MangleSyntax,
MinifyIdentifiers: options.MinifyIdentifiers,
ProfilerNames: options.ProfilerNames,
ES6: runtime.CanUseES6(options.UnsupportedJSFeatures),
}

Expand Down Expand Up @@ -2221,9 +2212,6 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.
// These configuration options must only depend on the key
MangleSyntax: key.MangleSyntax,
MinifyIdentifiers: key.MinifyIdentifiers,
Defines: cache.processedDefines(definesCacheKey{
profilerNames: key.ProfilerNames,
}),
UnsupportedJSFeatures: compat.UnsupportedJSFeatures(
map[compat.Engine][]int{compat.ES: {constraint}}),

Expand All @@ -2250,38 +2238,3 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.
}
return
}

func (cache *runtimeCache) processedDefines(key definesCacheKey) (defines *config.ProcessedDefines) {
ok := false

// Cache hit?
(func() {
cache.definesMutex.Lock()
defer cache.definesMutex.Unlock()
if cache.definesMap != nil {
defines, ok = cache.definesMap[key]
}
})()
if ok {
return
}

// Cache miss
result := config.ProcessDefines(map[string]config.DefineData{
"__profiler": {
DefineFunc: func(da config.DefineArgs) js_ast.E {
return &js_ast.EBoolean{Value: key.profilerNames}
},
},
})
defines = &result

// Cache for next time
cache.definesMutex.Lock()
defer cache.definesMutex.Unlock()
if cache.definesMap == nil {
cache.definesMap = make(map[definesCacheKey]*config.ProcessedDefines)
}
cache.definesMap[key] = defines
return
}
34 changes: 20 additions & 14 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type linkerContext struct {
// We may need to refer to the CommonJS "module" symbol for exports
unboundModuleRef js_ast.Ref

// We may need to refer to the "__esm" and/or "__commonJS" runtime symbols
cjsRuntimeRef js_ast.Ref
esmRuntimeRef js_ast.Ref

// This represents the parallel computation of source map related data.
// Calling this will block until the computation is done. The resulting value
// is shared between threads and must be treated as immutable.
Expand Down Expand Up @@ -228,6 +232,16 @@ func link(
}
timer.End("Clone linker graph")

// Use a smaller version of these functions if we don't need profiler names
runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr)
if c.options.ProfilerNames {
c.cjsRuntimeRef = runtimeRepr.AST.NamedExports["__commonJS"].Ref
c.esmRuntimeRef = runtimeRepr.AST.NamedExports["__esm"].Ref
} else {
c.cjsRuntimeRef = runtimeRepr.AST.NamedExports["__commonJSMin"].Ref
c.esmRuntimeRef = runtimeRepr.AST.NamedExports["__esmMin"].Ref
}

for _, entryPoint := range entryPoints {
if repr, ok := c.graph.Files[entryPoint.SourceIndex].InputFile.Repr.(*graph.JSRepr); ok {
// Loaders default to CommonJS when they are the entry point and the output
Expand Down Expand Up @@ -1838,8 +1852,7 @@ func (c *linkerContext) createWrapperForFile(sourceIndex uint32) {
// of it.
case graph.WrapCJS:
runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr)
commonJSRef := runtimeRepr.AST.NamedExports["__commonJS"].Ref
commonJSParts := runtimeRepr.TopLevelSymbolToParts(commonJSRef)
commonJSParts := runtimeRepr.TopLevelSymbolToParts(c.cjsRuntimeRef)

// Generate the dummy part
dependencies := make([]js_ast.Dependency, len(commonJSParts))
Expand All @@ -1861,7 +1874,7 @@ func (c *linkerContext) createWrapperForFile(sourceIndex uint32) {
Dependencies: dependencies,
})
repr.Meta.WrapperPartIndex = ast.MakeIndex32(partIndex)
c.graph.GenerateSymbolImportAndUse(sourceIndex, partIndex, commonJSRef, 1, runtime.SourceIndex)
c.graph.GenerateSymbolImportAndUse(sourceIndex, partIndex, c.cjsRuntimeRef, 1, runtime.SourceIndex)

// If this is a lazily-initialized ESM file, we're going to need to
// generate a wrapper for the ESM closure. That will end up looking
Expand All @@ -1875,8 +1888,7 @@ func (c *linkerContext) createWrapperForFile(sourceIndex uint32) {
// for similar reasons to the CommonJS closure above.
case graph.WrapESM:
runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr)
esmRef := runtimeRepr.AST.NamedExports["__esm"].Ref
esmParts := runtimeRepr.TopLevelSymbolToParts(esmRef)
esmParts := runtimeRepr.TopLevelSymbolToParts(c.esmRuntimeRef)

// Generate the dummy part
dependencies := make([]js_ast.Dependency, len(esmParts))
Expand All @@ -1896,7 +1908,7 @@ func (c *linkerContext) createWrapperForFile(sourceIndex uint32) {
Dependencies: dependencies,
})
repr.Meta.WrapperPartIndex = ast.MakeIndex32(partIndex)
c.graph.GenerateSymbolImportAndUse(sourceIndex, partIndex, esmRef, 1, runtime.SourceIndex)
c.graph.GenerateSymbolImportAndUse(sourceIndex, partIndex, c.esmRuntimeRef, 1, runtime.SourceIndex)
}
}

Expand Down Expand Up @@ -3523,8 +3535,6 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
partRange partRange,
entryBits helpers.BitSet,
chunkAbsDir string,
commonJSRef js_ast.Ref,
esmRef js_ast.Ref,
toModuleRef js_ast.Ref,
runtimeRequireRef js_ast.Ref,
result *compileResultJS,
Expand Down Expand Up @@ -3616,7 +3626,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
cjsArgs = []js_ast.Expr{{Data: &js_ast.EArrow{Args: args, Body: js_ast.FnBody{Stmts: stmts}}}}
}
value := js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: commonJSRef}},
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: c.cjsRuntimeRef}},
Args: cjsArgs,
}}

Expand Down Expand Up @@ -3684,7 +3694,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
esmArgs = []js_ast.Expr{{Data: &js_ast.EArrow{Body: js_ast.FnBody{Stmts: stmts}, IsAsync: isAsync}}}
}
value := js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: esmRef}},
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: c.esmRuntimeRef}},
Args: esmArgs,
}}

Expand Down Expand Up @@ -4295,8 +4305,6 @@ func (c *linkerContext) generateChunkJS(chunks []chunkInfo, chunkIndex int, chun
chunkRepr := chunk.chunkRepr.(*chunkReprJS)
compileResults := make([]compileResultJS, 0, len(chunkRepr.partsInChunkInOrder))
runtimeMembers := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr).AST.ModuleScope.Members
commonJSRef := js_ast.FollowSymbols(c.graph.Symbols, runtimeMembers["__commonJS"].Ref)
esmRef := js_ast.FollowSymbols(c.graph.Symbols, runtimeMembers["__esm"].Ref)
toModuleRef := js_ast.FollowSymbols(c.graph.Symbols, runtimeMembers["__toModule"].Ref)
runtimeRequireRef := js_ast.FollowSymbols(c.graph.Symbols, runtimeMembers["__require"].Ref)
r := c.renameSymbolsInChunk(chunk, chunkRepr.filesInChunkInOrder, timer)
Expand Down Expand Up @@ -4330,8 +4338,6 @@ func (c *linkerContext) generateChunkJS(chunks []chunkInfo, chunkIndex int, chun
partRange,
chunk.entryBits,
chunkAbsDir,
commonJSRef,
esmRef,
toModuleRef,
runtimeRequireRef,
compileResult,
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@ console.log(shared_default);
================================================================================
TestMinifiedBundleCommonJS
---------- /out.js ----------
var t=r(n=>{n.foo=function(){return 123}});var u=r((l,s)=>{s.exports={test:!0}});var{foo:c}=t();console.log(c(),u());
var n=e(r=>{r.foo=function(){return 123}});var t=e((j,s)=>{s.exports={test:!0}});var{foo:c}=n();console.log(c(),t());

================================================================================
TestMinifiedBundleES6
Expand Down
8 changes: 4 additions & 4 deletions internal/bundler/snapshots/snapshots_importstar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ var foo = 123;
TestExportSelfCommonJSMinified
---------- /out.js ----------
// entry.js
var l = n((c, r) => {
r.exports = { foo: 123 };
console.log(l());
var r = n((t, e) => {
e.exports = { foo: 123 };
console.log(r());
});
module.exports = l();
module.exports = r();

================================================================================
TestExportSelfES6
Expand Down
26 changes: 13 additions & 13 deletions internal/bundler/snapshots/snapshots_splitting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,19 @@ TestSplittingDynamicAndNotDynamicCommonJSIntoES6
import {
__toModule,
require_foo
} from "./chunk-LJOQBRFL.js";
} from "./chunk-NXTNQ63R.js";

// entry.js
var import_foo = __toModule(require_foo());
import("./foo-7CO27JLK.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b));
import("./foo-246JBBCS.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b));

---------- /out/foo-7CO27JLK.js ----------
---------- /out/foo-246JBBCS.js ----------
import {
require_foo
} from "./chunk-LJOQBRFL.js";
} from "./chunk-NXTNQ63R.js";
export default require_foo();

---------- /out/chunk-LJOQBRFL.js ----------
---------- /out/chunk-NXTNQ63R.js ----------
// foo.js
var require_foo = __commonJS({
"foo.js"(exports) {
Expand Down Expand Up @@ -260,9 +260,9 @@ export {
TestSplittingDynamicCommonJSIntoES6
---------- /out/entry.js ----------
// entry.js
import("./foo-KZX24I45.js").then(({ default: { bar } }) => console.log(bar));
import("./foo-W3YX6HCT.js").then(({ default: { bar } }) => console.log(bar));

---------- /out/foo-KZX24I45.js ----------
---------- /out/foo-W3YX6HCT.js ----------
// foo.js
var require_foo = __commonJS({
"foo.js"(exports) {
Expand Down Expand Up @@ -317,7 +317,7 @@ TestSplittingHybridESMAndCJSIssue617
import {
foo,
init_a
} from "./chunk-V7Q4P7WA.js";
} from "./chunk-R3U6QWBM.js";
init_a();
export {
foo
Expand All @@ -327,15 +327,15 @@ export {
import {
a_exports,
init_a
} from "./chunk-V7Q4P7WA.js";
} from "./chunk-R3U6QWBM.js";

// b.js
var bar = (init_a(), a_exports);
export {
bar
};

---------- /out/chunk-V7Q4P7WA.js ----------
---------- /out/chunk-R3U6QWBM.js ----------
// a.js
var a_exports = {};
__export(a_exports, {
Expand Down Expand Up @@ -485,7 +485,7 @@ TestSplittingSharedCommonJSIntoES6
---------- /out/a.js ----------
import {
require_shared
} from "./chunk-YOM27KNJ.js";
} from "./chunk-4T6PTLAJ.js";

// a.js
var { foo } = require_shared();
Expand All @@ -494,13 +494,13 @@ console.log(foo);
---------- /out/b.js ----------
import {
require_shared
} from "./chunk-YOM27KNJ.js";
} from "./chunk-4T6PTLAJ.js";

// b.js
var { foo } = require_shared();
console.log(foo);

---------- /out/chunk-YOM27KNJ.js ----------
---------- /out/chunk-4T6PTLAJ.js ----------
// shared.js
var require_shared = __commonJS({
"shared.js"(exports) {
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ console.log(a, b, c, d, e, real);
================================================================================
TestTSMinifiedBundleCommonJS
---------- /out.js ----------
var t=r(n=>{n.foo=function(){return 123}});var u=r((l,s)=>{s.exports={test:!0}});var{foo:c}=t();console.log(c(),u());
var n=e(r=>{r.foo=function(){return 123}});var t=e((j,s)=>{s.exports={test:!0}});var{foo:c}=n();console.log(c(),t());

================================================================================
TestTSMinifiedBundleES6
Expand Down
18 changes: 8 additions & 10 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,18 @@ func code(isES6 bool) string {
// This is for lazily-initialized ESM code. This has two implementations, a
// compact one for minified code and a verbose one that generates friendly
// names in V8's profiler and in stack traces.
export var __esm = __profiler
&& ((fn, res) => function __init() {
return fn && (res = (0, fn[Object.keys(fn)[0]])(fn = 0)), res
})
|| ((fn, res) => () => (fn && (res = fn(fn = 0)), res))
export var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[Object.keys(fn)[0]])(fn = 0)), res
}
export var __esmMin = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
// Wraps a CommonJS closure and returns a require() function. This has two
// implementations, a compact one for minified code and a verbose one that
// generates friendly names in V8's profiler and in stack traces.
export var __commonJS = __profiler
&& ((cb, mod) => function __require() {
return mod || (0, cb[Object.keys(cb)[0]])((mod = {exports: {}}).exports, mod), mod.exports
})
|| ((cb, mod) => () => (mod || cb((mod = {exports: {}}).exports, mod), mod.exports))
export var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[Object.keys(cb)[0]])((mod = {exports: {}}).exports, mod), mod.exports
}
export var __commonJSMin = (cb, mod) => () => (mod || cb((mod = {exports: {}}).exports, mod), mod.exports)
// Used to implement ES6 exports to CommonJS
export var __export = (target, all) => {
Expand Down

0 comments on commit 488fe7c

Please sign in to comment.