Skip to content

Commit 923aee9

Browse files
committed
improved performance of pointers
1 parent e89b87d commit 923aee9

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func WritePkgCode(pkg *Archive, minify bool, w *SourceMapFilter) error {
175175
if _, err := w.Write(removeWhitespace([]byte(fmt.Sprintf("$packages[\"%s\"] = (function() {\n", pkg.ImportPath)), minify)); err != nil {
176176
return err
177177
}
178-
vars := []string{"$pkg = {}", "$ptr = {}", "$init"}
178+
vars := []string{"$pkg = {}", "$init"}
179179
var filteredDecls []*Decl
180180
for _, d := range pkg.Declarations {
181181
if d.DceObjectFilter == "" && d.DceMethodFilter == "" {

compiler/expressions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
212212
if c.p.escapingVars[obj] {
213213
return c.formatExpr("(%1s.$ptr || (%1s.$ptr = new %2s(function() { return this.$target[0]; }, function($v) { this.$target[0] = $v; }, %1s)))", c.p.objectVars[obj], c.typeName(exprType))
214214
}
215-
return c.formatExpr("($ptr.%1s || ($ptr.%1s = new %2s(function() { return %1s; }, function($v) { %3s })))", c.p.objectVars[obj], c.typeName(exprType), c.translateAssign(x, "$v", exprType, false))
215+
return c.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, c.varPtrName(obj), c.typeName(exprType), c.objectName(obj), c.translateAssign(x, "$v", exprType, false))
216216
case *ast.SelectorExpr:
217217
newSel := &ast.SelectorExpr{X: c.newIdent("this.$target", c.p.Types[x.X].Type), Sel: x.Sel}
218218
c.p.Selections[newSel] = c.p.Selections[x]

compiler/package.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type pkgContext struct {
2020
typeNames []*types.TypeName
2121
pkgVars map[string]string
2222
objectVars map[types.Object]string
23+
varPtrNames map[types.Object]string
2324
anonTypes []*types.TypeName
2425
anonTypeMap typeutil.Map
2526
escapingVars map[*types.Var]bool
@@ -142,6 +143,7 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
142143
Info: pkgInfo,
143144
pkgVars: make(map[string]string),
144145
objectVars: make(map[types.Object]string),
146+
varPtrNames: make(map[types.Object]string),
145147
escapingVars: make(map[*types.Var]bool),
146148
indentation: 1,
147149
dependencies: make(map[types.Object]bool),
@@ -721,8 +723,6 @@ func translateFunction(typ *ast.FuncType, initStmts []ast.Stmt, body *ast.BlockS
721723
prefix = prefix + " $deferred = []; $deferred.index = $curGoroutine.deferStack.length; $curGoroutine.deferStack.push($deferred);"
722724
}
723725

724-
prefix = prefix + " $ptr = {};"
725-
726726
if prefix != "" {
727727
bodyOutput = strings.Repeat("\t", c.p.indentation+1) + "/* */" + prefix + "\n" + bodyOutput
728728
}

compiler/utils.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ func (c *funcContext) objectName(o types.Object) string {
278278
}
279279
}
280280

281-
name, found := c.p.objectVars[o]
282-
if !found {
281+
name, ok := c.p.objectVars[o]
282+
if !ok {
283283
name = c.newVariableWithLevel(o.Name(), o.Parent() == c.p.Pkg.Scope())
284284
c.p.objectVars[o] = name
285285
}
@@ -290,6 +290,15 @@ func (c *funcContext) objectName(o types.Object) string {
290290
return name
291291
}
292292

293+
func (c *funcContext) varPtrName(o types.Object) string {
294+
name, ok := c.p.varPtrNames[o]
295+
if !ok {
296+
name = c.newVariableWithLevel(o.Name()+"$ptr", o.Parent() == c.p.Pkg.Scope())
297+
c.p.varPtrNames[o] = name
298+
}
299+
return name
300+
}
301+
293302
func (c *funcContext) typeName(ty types.Type) string {
294303
switch t := ty.(type) {
295304
case *types.Basic:

0 commit comments

Comments
 (0)