Skip to content

Commit 8367ec6

Browse files
committed
Improve speed of open() on Chrome
1 parent e638cb0 commit 8367ec6

File tree

5 files changed

+76
-56
lines changed

5 files changed

+76
-56
lines changed

www/src/ast_to_js.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,18 @@ function compiler_error(ast_obj, message, end){
111111
throw exc
112112
}
113113

114+
var uuid = Math.floor(Math.random() * 1000000)
115+
function make_id(){
116+
uuid += 1
117+
return uuid
118+
}
119+
114120
function fast_id(obj){
115121
// faster than calling _b_.id
116122
if(obj.$id !== undefined){
117123
return obj.$id
118124
}
119-
return obj.$id = $B.UUID()
125+
return obj.$id = make_id()
120126
}
121127

122128
function copy_position(target, origin){
@@ -766,7 +772,7 @@ function init_genexpr(comp, scopes){
766772

767773
function make_comp(scopes){
768774
// Code common to list / set / dict comprehensions
769-
var id = $B.UUID(),
775+
var id = make_id(),
770776
type = this.constructor.$name,
771777
symtable_block = scopes.symtable.table.blocks.get(fast_id(this)),
772778
varnames = symtable_block.varnames.map(x => `"${x}"`),
@@ -1067,7 +1073,7 @@ $B.ast.Assign.prototype.to_js = function(scopes){
10671073
break
10681074
}
10691075
}
1070-
var iter_id = 'it_' + $B.UUID()
1076+
var iter_id = 'it_' + make_id()
10711077
js += `var ${iter_id} = $B.unpacker(${value}, ${nb_targets}, ` +
10721078
`${has_starred}`
10731079
if(nb_after_starred !== undefined){
@@ -1100,7 +1106,7 @@ $B.ast.Assign.prototype.to_js = function(scopes){
11001106
return js
11011107
}
11021108
}
1103-
var value_id = 'v' + $B.UUID()
1109+
var value_id = 'v' + make_id()
11041110
js += `var ${value_id} = ${value}\n`
11051111

11061112
var assigns = []
@@ -1171,7 +1177,7 @@ $B.ast.AsyncWith.prototype.to_js = function(scopes){
11711177
}
11721178

11731179
function add_item(item, js){
1174-
var id = $B.UUID()
1180+
var id = make_id()
11751181
var s = `var mgr_${id} = ` +
11761182
$B.js_from_ast(item.context_expr, scopes) + ',\n' +
11771183
`mgr_type_${id} = _b_.type.$factory(mgr_${id}),\n` +
@@ -1504,14 +1510,14 @@ $B.ast.ClassDef.prototype.to_js = function(scopes){
15041510

15051511
var js = '',
15061512
locals_name = make_scope_name(scopes, class_scope),
1507-
ref = this.name + $B.UUID(),
1513+
ref = this.name + make_id(),
15081514
glob = scopes[0].name,
15091515
globals_name = make_scope_name(scopes, scopes[0]),
15101516
decorators = [],
15111517
decorated = false
15121518
for(let dec of this.decorator_list){
15131519
decorated = true
1514-
var dec_id = 'decorator' + $B.UUID()
1520+
var dec_id = 'decorator' + make_id()
15151521
decorators.push(dec_id)
15161522
js += `$B.set_lineno(frame, ${dec.lineno})\n` +
15171523
`var ${dec_id} = ${$B.js_from_ast(dec, scopes)}\n`
@@ -1608,7 +1614,7 @@ $B.ast.ClassDef.prototype.to_js = function(scopes){
16081614
var class_ref = reference(scopes, enclosing_scope, this.name)
16091615

16101616
if(decorated){
1611-
class_ref = `decorated${$B.UUID()}`
1617+
class_ref = `decorated${make_id()}`
16121618
js += 'var '
16131619
}
16141620

@@ -1668,7 +1674,7 @@ $B.ast.Compare.prototype.to_js = function(scopes){
16681674
}
16691675

16701676
$B.ast.comprehension.prototype.to_js = function(scopes){
1671-
var id = $B.UUID(),
1677+
var id = make_id(),
16721678
iter = $B.js_from_ast(this.iter, scopes)
16731679

16741680
var js = `var next_func_${id} = $B.make_js_iterator(${iter}, frame, ${this.lineno})\n` +
@@ -1822,7 +1828,7 @@ $B.ast.For.prototype.to_js = function(scopes){
18221828
// Create a new scope with the same name to avoid binding in the enclosing
18231829
// scope.
18241830
compiler_check(this)
1825-
var id = $B.UUID(),
1831+
var id = make_id(),
18261832
iter = $B.js_from_ast(this.iter, scopes),
18271833
js = `frame.$lineno = ${this.lineno}\n`
18281834
// Create a new scope with the same name to avoid binding in the enclosing
@@ -2464,7 +2470,7 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
24642470
// evaluate decorator in enclosing scope
24652471
for(let dec of this.decorator_list){
24662472
decorated = true
2467-
var dec_id = 'decorator' + $B.UUID()
2473+
var dec_id = 'decorator' + make_id()
24682474
decorators.push(dec_id)
24692475
decs_declare += `$B.set_lineno(frame, ${dec.lineno})\n`
24702476
decs_declare += `var ${dec_id} = ${$B.js_from_ast(dec, scopes)}\n`
@@ -2483,7 +2489,7 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
24832489
kw_defaults = kw_default_names.length == 0 ? '_b_.None' :
24842490
`_b_.dict.$from_js({${kw_defaults.join(', ')}})`
24852491

2486-
var id = $B.UUID(),
2492+
var id = make_id(),
24872493
name2 = this.name + id
24882494

24892495
// Type params (PEP 695)
@@ -2579,7 +2585,9 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
25792585
this.args.kwarg === undefined){
25802586
js += `${locals_name} = locals = {};\n`
25812587
// generate error message
2582-
js += `if(arguments.length !== 0) ${name2}.$args_parser(${parse_args.join(', ')})\n;`
2588+
js += `if(arguments.length !== 0){\n` +
2589+
`${name2}.$args_parser(${parse_args.join(', ')})\n` +
2590+
`}\n`
25832591
}else{
25842592
js += `${locals_name} = locals = ${name2}.$args_parser(${parse_args.join(', ')})\n`
25852593
}
@@ -2701,7 +2709,7 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
27012709
if(in_class){
27022710
js += `${name2}.$is_method = true\n`
27032711
}
2704-
2712+
27052713
// Set admin infos
27062714
js += `$B.make_function_infos(${name2}, ` +
27072715
`'${gname}', ` +
@@ -2731,7 +2739,7 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
27312739
func_ref = `${make_scope_name(scopes, func_name_scope)}.${mangled}`
27322740

27332741
if(decorated){
2734-
func_ref = `decorated${$B.UUID()}`
2742+
func_ref = `decorated${make_id()}`
27352743
js += 'var '
27362744
}
27372745

@@ -2835,7 +2843,7 @@ $B.ast.FunctionDef.prototype._check = function(){
28352843
}
28362844

28372845
$B.ast.GeneratorExp.prototype.to_js = function(scopes){
2838-
var id = $B.UUID(),
2846+
var id = make_id(),
28392847
symtable_block = scopes.symtable.table.blocks.get(fast_id(this)),
28402848
varnames = symtable_block.varnames.map(x => `"${x}"`)
28412849

@@ -3060,7 +3068,7 @@ $B.ast.JoinedStr.prototype.to_js = function(scopes){
30603068

30613069
$B.ast.Lambda.prototype.to_js = function(scopes){
30623070
// Reuse FunctionDef, with a specific name
3063-
var id = $B.UUID(),
3071+
var id = make_id(),
30643072
name = 'lambda_' + $B.lambda_magic + '_' + id
30653073
var f = new $B.ast.FunctionDef(name, this.args, this.body, [])
30663074
f.lineno = this.lineno
@@ -3560,7 +3568,7 @@ $B.ast.Subscript.prototype.to_js = function(scopes){
35603568

35613569
$B.ast.Try.prototype.to_js = function(scopes){
35623570
compiler_check(this)
3563-
var id = $B.UUID(),
3571+
var id = make_id(),
35643572
has_except_handlers = this.handlers.length > 0,
35653573
has_else = this.orelse.length > 0,
35663574
has_finally = this.finalbody.length > 0
@@ -3674,7 +3682,7 @@ $B.ast.Try.prototype.to_js = function(scopes){
36743682

36753683
$B.ast.TryStar.prototype.to_js = function(scopes){
36763684
// PEP 654 try...except*...
3677-
var id = $B.UUID(),
3685+
var id = make_id(),
36783686
has_except_handlers = this.handlers.length > 0,
36793687
has_else = this.orelse.length > 0,
36803688
has_finally = this.finalbody.length > 0
@@ -3876,7 +3884,7 @@ $B.ast.UnaryOp.prototype.to_js = function(scopes){
38763884
}
38773885

38783886
$B.ast.While.prototype.to_js = function(scopes){
3879-
var id = $B.UUID()
3887+
var id = make_id()
38803888

38813889
// Create a new scope with the same name to avoid binding in the enclosing
38823890
// scope.
@@ -3938,7 +3946,7 @@ $B.ast.With.prototype.to_js = function(scopes){
39383946
*/
39393947

39403948
function add_item(item, js){
3941-
var id = $B.UUID()
3949+
var id = make_id()
39423950
var s = `var mgr_${id} = ` +
39433951
$B.js_from_ast(item.context_expr, scopes) + ',\n' +
39443952
`klass = $B.get_class(mgr_${id})\n` +
@@ -4078,7 +4086,7 @@ $B.ast.YieldFrom.prototype.to_js = function(scopes){
40784086
}
40794087
scope.is_generator = true
40804088
var value = $B.js_from_ast(this.value, scopes)
4081-
var n = $B.UUID()
4089+
var n = make_id()
40824090
return `yield* (function* f(){
40834091
var _i${n} = _b_.iter(${value}),
40844092
_r${n}

0 commit comments

Comments
 (0)