Skip to content

Commit 36ca10f

Browse files
authored
Merge pull request #19609 from jrose-apple/your-code-is-programmed-not-to-love-me-but-you-cant-pretend
[Stats] Track NumASTBytesAllocated continuously
2 parents 2ddc92a + 9e56a8e commit 36ca10f

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

include/swift/AST/ASTContext.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ class ASTContext final {
208208
DiagnosticEngine &Diags);
209209
~ASTContext();
210210

211+
/// Optional table of counters to report, nullptr when not collecting.
212+
///
213+
/// This must be initialized early so that Allocate() doesn't try to access
214+
/// it before being set to null.
215+
UnifiedStatsReporter *Stats = nullptr;
216+
211217
/// \brief The language options used for translation.
212218
LangOptions &LangOpts;
213219

@@ -261,12 +267,6 @@ class ASTContext final {
261267
/// Cache of remapped types (useful for diagnostics).
262268
llvm::StringMap<Type> RemappedTypes;
263269

264-
/// Optional table of counters to report, nullptr when not collecting.
265-
UnifiedStatsReporter *Stats = nullptr;
266-
267-
/// Set a new stats reporter.
268-
void setStatsReporter(UnifiedStatsReporter *stats);
269-
270270
private:
271271
/// \brief The current generation number, which reflects the number of
272272
/// times that external modules have been loaded.
@@ -287,11 +287,11 @@ class ASTContext final {
287287
/// Cache of module names that fail the 'canImport' test in this context.
288288
llvm::SmallPtrSet<Identifier, 8> FailedModuleImportNames;
289289

290-
public:
291290
/// \brief Retrieve the allocator for the given arena.
292291
llvm::BumpPtrAllocator &
293292
getAllocator(AllocationArena arena = AllocationArena::Permanent) const;
294293

294+
public:
295295
/// Allocate - Allocate memory from the ASTContext bump pointer.
296296
void *Allocate(unsigned long bytes, unsigned alignment,
297297
AllocationArena arena = AllocationArena::Permanent) const {
@@ -300,7 +300,9 @@ class ASTContext final {
300300

301301
if (LangOpts.UseMalloc)
302302
return AlignedAlloc(bytes, alignment);
303-
303+
304+
if (arena == AllocationArena::Permanent && Stats)
305+
Stats->getFrontendCounters().NumASTBytesAllocated += bytes;
304306
return getAllocator(arena).Allocate(bytes, alignment);
305307
}
306308

@@ -395,6 +397,9 @@ class ASTContext final {
395397
/// Retrive the syntax node memory manager for this context.
396398
llvm::IntrusiveRefCntPtr<syntax::SyntaxArena> getSyntaxArena() const;
397399

400+
/// Set a new stats reporter.
401+
void setStatsReporter(UnifiedStatsReporter *stats);
402+
398403
/// Retrieve the lazy resolver for this context.
399404
LazyResolver *getLazyResolver() const;
400405

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,11 @@ llvm::BumpPtrAllocator &ASTContext::getAllocator(AllocationArena arena) const {
562562
void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
563563
Stats = stats;
564564
evaluator.setStatsReporter(stats);
565+
566+
if (stats) {
567+
stats->getFrontendCounters().NumASTBytesAllocated =
568+
getAllocator().getBytesAllocated();
569+
}
565570
}
566571

567572
RC<syntax::SyntaxArena> ASTContext::getSyntaxArena() const {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ static void countStatsPostSema(UnifiedStatsReporter &Stats,
499499
auto const &AST = Instance.getASTContext();
500500
C.NumLoadedModules = AST.LoadedModules.size();
501501
C.NumImportedExternalDefinitions = AST.ExternalDefinitions.size();
502-
C.NumASTBytesAllocated = AST.getAllocator().getBytesAllocated();
503502

504503
if (auto *D = Instance.getDependencyTracker()) {
505504
C.NumDependencies = D->getDependencies().size();

0 commit comments

Comments
 (0)