Skip to content

Commit 0cad7e3

Browse files
Support reusing source library builders
Change-Id: Idc884e9c733bad0cb3e15be3cbaf248e8371196a Reviewed-on: https://dart-review.googlesource.com/28663 Reviewed-by: Jens Johansen <[email protected]>
1 parent 891e591 commit 0cad7e3

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
lines changed

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class KernelTarget extends TargetImplementation {
187187
List<ClassBuilder> result = <ClassBuilder>[];
188188
loader.builders.forEach((Uri uri, LibraryBuilder library) {
189189
library.forEach((String name, Builder member) {
190-
if (member is KernelClassBuilder) {
190+
if (member is ClassBuilder) {
191191
result.add(member);
192192
}
193193
});
@@ -380,20 +380,22 @@ class KernelTarget extends TargetImplementation {
380380
void installDefaultSupertypes() {
381381
Class objectClass = this.objectClass;
382382
loader.builders.forEach((Uri uri, LibraryBuilder library) {
383-
library.forEach((String name, Builder builder) {
384-
if (builder is SourceClassBuilder) {
385-
Class cls = builder.target;
386-
if (cls != objectClass) {
387-
cls.supertype ??= objectClass.asRawSupertype;
388-
builder.supertype ??= new KernelNamedTypeBuilder("Object", null)
389-
..bind(objectClassBuilder);
383+
if (library.loader == loader) {
384+
library.forEach((String name, Builder builder) {
385+
if (builder is SourceClassBuilder) {
386+
Class cls = builder.target;
387+
if (cls != objectClass) {
388+
cls.supertype ??= objectClass.asRawSupertype;
389+
builder.supertype ??= new KernelNamedTypeBuilder("Object", null)
390+
..bind(objectClassBuilder);
391+
}
392+
if (builder.isMixinApplication) {
393+
cls.mixedInType = builder.mixedInType
394+
.buildSupertype(library, builder.charOffset, builder.fileUri);
395+
}
390396
}
391-
if (builder.isMixinApplication) {
392-
cls.mixedInType = builder.mixedInType
393-
.buildSupertype(library, builder.charOffset, builder.fileUri);
394-
}
395-
}
396-
});
397+
});
398+
}
397399
});
398400
ticker.logMs("Installed Object as implicit superclass");
399401
}

pkg/front_end/lib/src/fasta/loader.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ abstract class Loader<L> {
142142
Future<Null> buildBodies() async {
143143
assert(coreLibrary != null);
144144
for (LibraryBuilder library in builders.values) {
145-
currentUriForCrashReporting = library.uri;
146-
await buildBody(library);
145+
if (library.loader == this) {
146+
currentUriForCrashReporting = library.uri;
147+
await buildBody(library);
148+
}
147149
}
148150
currentUriForCrashReporting = null;
149151
logSummary(templateSourceBodySummary);

pkg/front_end/lib/src/fasta/source/source_loader.dart

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,21 @@ class SourceLoader<L> extends Loader<L> {
209209
void resolveParts() {
210210
List<Uri> parts = <Uri>[];
211211
builders.forEach((Uri uri, LibraryBuilder library) {
212-
if (library is SourceLibraryBuilder) {
213-
if (library.isPart) {
214-
library.validatePart();
212+
if (library.loader == this) {
213+
SourceLibraryBuilder sourceLibrary = library;
214+
if (sourceLibrary.isPart) {
215+
sourceLibrary.validatePart();
215216
parts.add(uri);
216217
} else {
217-
library.includeParts();
218+
sourceLibrary.includeParts();
218219
}
219220
}
220221
});
221222
parts.forEach(builders.remove);
222223
ticker.logMs("Resolved parts");
223224

224225
builders.forEach((Uri uri, LibraryBuilder library) {
225-
if (library is SourceLibraryBuilder) {
226+
if (library.loader == this) {
226227
library.applyPatches();
227228
}
228229
});
@@ -233,8 +234,9 @@ class SourceLoader<L> extends Loader<L> {
233234
Set<LibraryBuilder> exporters = new Set<LibraryBuilder>();
234235
Set<LibraryBuilder> exportees = new Set<LibraryBuilder>();
235236
builders.forEach((Uri uri, LibraryBuilder library) {
236-
if (library is SourceLibraryBuilder) {
237-
library.buildInitialScopes();
237+
if (library.loader == this) {
238+
SourceLibraryBuilder sourceLibrary = library;
239+
sourceLibrary.buildInitialScopes();
238240
}
239241
if (library.exporters.isNotEmpty) {
240242
exportees.add(library);
@@ -266,10 +268,18 @@ class SourceLoader<L> extends Loader<L> {
266268
}
267269
} while (wasChanged);
268270
builders.forEach((Uri uri, LibraryBuilder library) {
269-
if (library is SourceLibraryBuilder) {
270-
library.addImportsToScope();
271+
if (library.loader == this) {
272+
SourceLibraryBuilder sourceLibrary = library;
273+
sourceLibrary.addImportsToScope();
271274
}
272275
});
276+
for (LibraryBuilder exportee in exportees) {
277+
// TODO(ahe): Change how we track exporters. Currently, when a library
278+
// (exporter) exports another library (exportee) we add a reference to
279+
// exporter to exportee. This creates a reference in the wrong direction
280+
// and can lead to memory leaks.
281+
exportee.exporters.clear();
282+
}
273283
ticker.logMs("Computed library scopes");
274284
// debugPrintExports();
275285
}
@@ -314,47 +324,59 @@ class SourceLoader<L> extends Loader<L> {
314324
void finishDeferredLoadTearoffs() {
315325
int count = 0;
316326
builders.forEach((Uri uri, LibraryBuilder library) {
317-
count += library.finishDeferredLoadTearoffs();
327+
if (library.loader == this) {
328+
count += library.finishDeferredLoadTearoffs();
329+
}
318330
});
319331
ticker.logMs("Finished deferred load tearoffs $count");
320332
}
321333

322334
void finishStaticInvocations() {
323335
int count = 0;
324336
builders.forEach((Uri uri, LibraryBuilder library) {
325-
count += library.finishStaticInvocations();
337+
if (library.loader == this) {
338+
count += library.finishStaticInvocations();
339+
}
326340
});
327341
ticker.logMs("Finished static invocations $count");
328342
}
329343

330344
void resolveConstructors() {
331345
int count = 0;
332346
builders.forEach((Uri uri, LibraryBuilder library) {
333-
count += library.resolveConstructors(null);
347+
if (library.loader == this) {
348+
count += library.resolveConstructors(null);
349+
}
334350
});
335351
ticker.logMs("Resolved $count constructors");
336352
}
337353

338354
void finishTypeVariables(ClassBuilder object) {
339355
int count = 0;
340356
builders.forEach((Uri uri, LibraryBuilder library) {
341-
count += library.finishTypeVariables(object);
357+
if (library.loader == this) {
358+
count += library.finishTypeVariables(object);
359+
}
342360
});
343361
ticker.logMs("Resolved $count type-variable bounds");
344362
}
345363

346364
void finishNativeMethods() {
347365
int count = 0;
348366
builders.forEach((Uri uri, LibraryBuilder library) {
349-
count += library.finishNativeMethods();
367+
if (library.loader == this) {
368+
count += library.finishNativeMethods();
369+
}
350370
});
351371
ticker.logMs("Finished $count native methods");
352372
}
353373

354374
void finishPatchMethods() {
355375
int count = 0;
356376
builders.forEach((Uri uri, LibraryBuilder library) {
357-
count += library.finishPatchMethods();
377+
if (library.loader == this) {
378+
count += library.finishPatchMethods();
379+
}
358380
});
359381
ticker.logMs("Finished $count patch methods");
360382
}
@@ -497,8 +519,9 @@ class SourceLoader<L> extends Loader<L> {
497519

498520
void buildProgram() {
499521
builders.forEach((Uri uri, LibraryBuilder library) {
500-
if (library is SourceLibraryBuilder) {
501-
L target = library.build(coreLibrary);
522+
if (library.loader == this) {
523+
SourceLibraryBuilder sourceLibrary = library;
524+
L target = sourceLibrary.build(coreLibrary);
502525
if (!library.isPatch) {
503526
libraries.add(target);
504527
}
@@ -517,7 +540,9 @@ class SourceLoader<L> extends Loader<L> {
517540
void checkOverrides(List<SourceClassBuilder> sourceClasses) {
518541
assert(hierarchy != null);
519542
for (SourceClassBuilder builder in sourceClasses) {
520-
builder.checkOverrides(hierarchy);
543+
if (builder.library.loader == this) {
544+
builder.checkOverrides(hierarchy);
545+
}
521546
}
522547
ticker.logMs("Checked overrides");
523548
}
@@ -539,7 +564,7 @@ class SourceLoader<L> extends Loader<L> {
539564
instrumentation,
540565
target.strongMode);
541566
builders.forEach((Uri uri, LibraryBuilder library) {
542-
if (library is SourceLibraryBuilder) {
567+
if (library.loader == this) {
543568
library.prepareTopLevelInference(library, null);
544569
}
545570
});
@@ -574,7 +599,7 @@ class SourceLoader<L> extends Loader<L> {
574599
typeInferenceEngine.finishTopLevelInitializingFormals();
575600
if (instrumentation != null) {
576601
builders.forEach((Uri uri, LibraryBuilder library) {
577-
if (library is SourceLibraryBuilder) {
602+
if (library.loader == this) {
578603
library.instrumentTopLevelInference(instrumentation);
579604
}
580605
});

0 commit comments

Comments
 (0)