Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: pkg/compiler/lib/src/js_emitter/full_emitter/emitter.dart

Issue 1575603002: dart2js: lazily load fields in their own deferred parts (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_emitter/program_builder/program_builder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library dart2js.js_emitter.full_emitter; 5 library dart2js.js_emitter.full_emitter;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 import 'dart:collection' show HashMap; 8 import 'dart:collection' show HashMap;
9 9
10 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; 10 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames;
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 reporter.withCurrentElement(element, () { 667 reporter.withCurrentElement(element, () {
668 parts.add(buildInitialization(element, jsAst.number(0))); 668 parts.add(buildInitialization(element, jsAst.number(0)));
669 }); 669 });
670 } 670 }
671 }); 671 });
672 } 672 }
673 673
674 return new jsAst.Block(parts); 674 return new jsAst.Block(parts);
675 } 675 }
676 676
677 jsAst.Statement buildLazilyInitializedStaticFields() { 677 jsAst.Statement buildLazilyInitializedStaticFields(Fragment fragment) {
678 JavaScriptConstantCompiler handler = backend.constants; 678 List<StaticField> lazyFields = fragment.staticLazilyInitializedFields;
679 List<VariableElement> lazyFields =
680 handler.getLazilyInitializedFieldsForEmission();
681 if (lazyFields.isNotEmpty) { 679 if (lazyFields.isNotEmpty) {
682 needsLazyInitializer = true; 680 needsLazyInitializer = true;
683 List<jsAst.Expression> laziesInfo = buildLaziesInfo(lazyFields); 681 List<jsAst.Expression> laziesInfo =
682 buildLaziesInfo(lazyFields, fragment.isMainFragment);
684 return js.statement(''' 683 return js.statement('''
685 (function(lazies) { 684 (function(lazies) {
686 for (var i = 0; i < lazies.length; ) { 685 for (var i = 0; i < lazies.length; ) {
687 var fieldName = lazies[i++]; 686 var fieldName = lazies[i++];
688 var getterName = lazies[i++]; 687 var getterName = lazies[i++];
689 if (#notMinified) { 688 if (#notMinified) {
690 var staticName = lazies[i++]; 689 var staticName = lazies[i++];
691 } 690 }
692 var lazyValue = lazies[i++]; 691 var lazyValue = lazies[i++];
693 692 if (#isDeferredFragment) {
693 var fieldHolder = lazies[i++];
694 }
694 // We build the lazy-check here: 695 // We build the lazy-check here:
695 // lazyInitializer(fieldName, getterName, lazyValue, staticName); 696 // lazyInitializer(fieldName, getterName, lazyValue, staticName);
696 // 'staticName' is used for error reporting in non-minified mode. 697 // 'staticName' is used for error reporting in non-minified mode.
697 // 'lazyValue' must be a closure that constructs the initial value. 698 // 'lazyValue' must be a closure that constructs the initial value.
698 if (#notMinified) { 699 if (#isMainFragment) {
699 #lazy(fieldName, getterName, lazyValue, staticName); 700 if (#notMinified) {
701 #lazy(fieldName, getterName, lazyValue, staticName);
702 } else {
703 #lazy(fieldName, getterName, lazyValue);
704 }
700 } else { 705 } else {
701 #lazy(fieldName, getterName, lazyValue); 706 if (#notMinified) {
707 #lazy(fieldName, getterName, lazyValue, staticName, fieldHolder);
708 } else {
709 #lazy(fieldName, getterName, lazyValue, null, fieldHolder);
710 }
702 } 711 }
703 } 712 }
704 })(#laziesInfo) 713 })(#laziesInfo)
705 ''', {'notMinified': !compiler.enableMinification, 714 ''', {'notMinified': !compiler.enableMinification,
706 'laziesInfo': new jsAst.ArrayInitializer(laziesInfo), 715 'laziesInfo': new jsAst.ArrayInitializer(laziesInfo),
707 'lazy': js(lazyInitializerName)}); 716 'lazy': js(lazyInitializerName),
717 'isMainFragment': fragment.isMainFragment,
718 'isDeferredFragment': !fragment.isMainFragment});
708 } else { 719 } else {
709 return js.comment("No lazy statics."); 720 return js.comment("No lazy statics.");
710 } 721 }
711 } 722 }
712 723
713 List<jsAst.Expression> buildLaziesInfo(List<VariableElement> lazies) { 724 List<jsAst.Expression> buildLaziesInfo(
725 List<StaticField> lazies, bool isMainFragment) {
714 List<jsAst.Expression> laziesInfo = <jsAst.Expression>[]; 726 List<jsAst.Expression> laziesInfo = <jsAst.Expression>[];
715 for (VariableElement element in Elements.sortedByPosition(lazies)) { 727 for (StaticField field in lazies) {
716 jsAst.Expression code = backend.generatedCode[element]; 728 laziesInfo.add(js.quoteName(field.name));
717 // The code is null if we ended up not needing the lazily 729 laziesInfo.add(js.quoteName(namer.deriveLazyInitializerName(field.name)));
Siggi Cherem (dart-lang) 2016/01/08 22:50:37 just to make sure I follow - we now emit null code
Harry Terkelsen 2016/01/11 18:01:21 We don't emit null code. The code here that filter
Siggi Cherem (dart-lang) 2016/01/08 22:50:37 so this no longer makes the name globally unique,
Harry Terkelsen 2016/01/11 18:01:21 It seems to be working properly in my simple test
718 // initialized field after all because of constant folding
719 // before code generation.
720 if (code == null) continue;
721 laziesInfo.add(js.quoteName(namer.globalPropertyName(element)));
722 laziesInfo.add(js.quoteName(namer.lazyInitializerName(element)));
723 if (!compiler.enableMinification) { 730 if (!compiler.enableMinification) {
724 laziesInfo.add(js.string(element.name)); 731 laziesInfo.add(js.quoteName(field.name));
725 } 732 }
726 laziesInfo.add(code); 733 laziesInfo.add(field.code);
734 if (!isMainFragment) {
735 laziesInfo.add(js('#', field.holder.name));
736 }
727 } 737 }
728 return laziesInfo; 738 return laziesInfo;
729 } 739 }
730 740
731 // TODO(sra): Remove this unused function. 741 // TODO(sra): Remove this unused function.
732 jsAst.Expression buildLazilyInitializedStaticField( 742 jsAst.Expression buildLazilyInitializedStaticField(
733 VariableElement element, {String isolateProperties}) { 743 VariableElement element, {String isolateProperties}) {
734 jsAst.Expression code = backend.generatedCode[element]; 744 jsAst.Expression code = backend.generatedCode[element];
735 // The code is null if we ended up not needing the lazily 745 // The code is null if we ended up not needing the lazily
736 // initialized field after all because of constant folding 746 // initialized field after all because of constant folding
(...skipping 826 matching lines...) Expand 10 before | Expand all | Expand 10 after
1563 "oneShotInterceptors": interceptorEmitter.buildOneShotInterceptors(), 1573 "oneShotInterceptors": interceptorEmitter.buildOneShotInterceptors(),
1564 "makeConstantList": 1574 "makeConstantList":
1565 buildMakeConstantList(program.outputContainsConstantList), 1575 buildMakeConstantList(program.outputContainsConstantList),
1566 "compileTimeConstants": buildCompileTimeConstants(mainFragment.constants, 1576 "compileTimeConstants": buildCompileTimeConstants(mainFragment.constants,
1567 isMainFragment: true), 1577 isMainFragment: true),
1568 "deferredBoilerPlate": buildDeferredBoilerPlate(deferredLoadHashes), 1578 "deferredBoilerPlate": buildDeferredBoilerPlate(deferredLoadHashes),
1569 "staticNonFinalInitializers": buildStaticNonFinalFieldInitializations( 1579 "staticNonFinalInitializers": buildStaticNonFinalFieldInitializations(
1570 mainOutputUnit), 1580 mainOutputUnit),
1571 "typeToInterceptorMap": 1581 "typeToInterceptorMap":
1572 interceptorEmitter.buildTypeToInterceptorMap(program), 1582 interceptorEmitter.buildTypeToInterceptorMap(program),
1573 "lazyStaticFields": buildLazilyInitializedStaticFields(), 1583 "lazyStaticFields": buildLazilyInitializedStaticFields(mainFragment),
1574 "metadata": buildMetadata(program, mainOutputUnit), 1584 "metadata": buildMetadata(program, mainOutputUnit),
1575 "convertToFastObject": buildConvertToFastObjectFunction(), 1585 "convertToFastObject": buildConvertToFastObjectFunction(),
1576 "convertToSlowObject": buildConvertToSlowObjectFunction(), 1586 "convertToSlowObject": buildConvertToSlowObjectFunction(),
1577 "convertGlobalObjectsToFastObjects": 1587 "convertGlobalObjectsToFastObjects":
1578 buildConvertGlobalObjectToFastObjects(), 1588 buildConvertGlobalObjectToFastObjects(),
1579 "debugFastObjects": buildDebugFastObjectCode(), 1589 "debugFastObjects": buildDebugFastObjectCode(),
1580 "init": buildInitFunction(program.outputContainsConstantList), 1590 "init": buildInitFunction(program.outputContainsConstantList),
1581 "main": buildMain(mainFragment.invokeMain) 1591 "main": buildMain(mainFragment.invokeMain)
1582 })); 1592 }));
1583 1593
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
1989 js.statement('$setupProgramName(dart, ${typesAccess}.length);')); 1999 js.statement('$setupProgramName(dart, ${typesAccess}.length);'));
1990 } 2000 }
1991 2001
1992 body..add(buildMetadata(program, outputUnit)) 2002 body..add(buildMetadata(program, outputUnit))
1993 ..add(js.statement('${typesAccess}.push.apply(${typesAccess}, ' 2003 ..add(js.statement('${typesAccess}.push.apply(${typesAccess}, '
1994 '${namer.deferredTypesName});')); 2004 '${namer.deferredTypesName});'));
1995 2005
1996 body.add(buildCompileTimeConstants(fragment.constants, 2006 body.add(buildCompileTimeConstants(fragment.constants,
1997 isMainFragment: false)); 2007 isMainFragment: false));
1998 body.add(buildStaticNonFinalFieldInitializations(outputUnit)); 2008 body.add(buildStaticNonFinalFieldInitializations(outputUnit));
2009 body.add(buildLazilyInitializedStaticFields(fragment));
1999 2010
2000 List<jsAst.Statement> statements = <jsAst.Statement>[]; 2011 List<jsAst.Statement> statements = <jsAst.Statement>[];
2001 2012
2002 statements 2013 statements
2003 ..add(buildGeneratedBy()) 2014 ..add(buildGeneratedBy())
2004 ..add(buildDeferredHeader()) 2015 ..add(buildDeferredHeader())
2005 ..add(js.statement('${deferredInitializers}.current = ' 2016 ..add(js.statement('${deferredInitializers}.current = '
2006 """function (#, ${namer.staticStateHolder}) { 2017 """function (#, ${namer.staticStateHolder}) {
2007 # 2018 #
2008 } 2019 }
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
2130 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { 2141 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) {
2131 if (element.isInstanceMember) { 2142 if (element.isInstanceMember) {
2132 cachedClassBuilders.remove(element.enclosingClass); 2143 cachedClassBuilders.remove(element.enclosingClass);
2133 2144
2134 nativeEmitter.cachedBuilders.remove(element.enclosingClass); 2145 nativeEmitter.cachedBuilders.remove(element.enclosingClass);
2135 2146
2136 } 2147 }
2137 } 2148 }
2138 } 2149 }
2139 } 2150 }
OLDNEW
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_emitter/program_builder/program_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698