print heap stats instead of memory usage (#203959)

* print heap stats instead of memory usage

* ensure no hanging on to service

---------

Co-authored-by: João Moreno <joao.moreno@microsoft.com>
This commit is contained in:
Johannes Rieken 2024-02-01 11:37:19 +01:00 committed by GitHub
parent 442c133fe9
commit 8ba046adbe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 14 deletions

View file

@ -5,6 +5,7 @@
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mangler = void 0;
const v8 = require("node:v8");
const fs = require("fs");
const path = require("path");
const process_1 = require("process");
@ -293,19 +294,17 @@ const skippedExportMangledSymbols = [
class DeclarationData {
fileName;
node;
service;
replacementName;
constructor(fileName, node, service, fileIdents) {
constructor(fileName, node, fileIdents) {
this.fileName = fileName;
this.node = node;
this.service = service;
// Todo: generate replacement names based on usage count, with more used names getting shorter identifiers
this.replacementName = fileIdents.next();
}
get locations() {
getLocations(service) {
if (ts.isVariableDeclaration(this.node)) {
// If the const aliases any types, we need to rename those too
const definitionResult = this.service.getDefinitionAndBoundSpan(this.fileName, this.node.name.getStart());
const definitionResult = service.getDefinitionAndBoundSpan(this.fileName, this.node.name.getStart());
if (definitionResult?.definitions && definitionResult.definitions.length > 1) {
return definitionResult.definitions.map(x => ({ fileName: x.fileName, offset: x.textSpan.start }));
}
@ -404,7 +403,7 @@ class Mangler {
if (isInAmbientContext(node)) {
return;
}
this.allExportedSymbols.add(new DeclarationData(node.getSourceFile().fileName, node, service, fileIdents));
this.allExportedSymbols.add(new DeclarationData(node.getSourceFile().fileName, node, fileIdents));
}
}
ts.forEachChild(node, visit);
@ -528,7 +527,7 @@ class Mangler {
continue;
}
const newText = data.replacementName;
for (const { fileName, offset } of data.locations) {
for (const { fileName, offset } of data.getLocations(service)) {
queueRename(fileName, offset, newText);
}
}
@ -615,7 +614,7 @@ class Mangler {
}
service.dispose();
this.renameWorkerPool.terminate();
this.log(`Done: ${savedBytes / 1000}kb saved, memory-usage: ${JSON.stringify(process.memoryUsage())}`);
this.log(`Done: ${savedBytes / 1000}kb saved, memory-usage: ${JSON.stringify(v8.getHeapStatistics())}`);
return result;
}
}

View file

@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as v8 from 'node:v8';
import * as fs from 'fs';
import * as path from 'path';
import { argv } from 'process';
@ -338,17 +339,16 @@ class DeclarationData {
constructor(
readonly fileName: string,
readonly node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.EnumDeclaration | ts.VariableDeclaration,
private readonly service: ts.LanguageService,
fileIdents: ShortIdent,
) {
// Todo: generate replacement names based on usage count, with more used names getting shorter identifiers
this.replacementName = fileIdents.next();
}
get locations(): Iterable<{ fileName: string; offset: number }> {
getLocations(service: ts.LanguageService): Iterable<{ fileName: string; offset: number }> {
if (ts.isVariableDeclaration(this.node)) {
// If the const aliases any types, we need to rename those too
const definitionResult = this.service.getDefinitionAndBoundSpan(this.fileName, this.node.name.getStart());
const definitionResult = service.getDefinitionAndBoundSpan(this.fileName, this.node.name.getStart());
if (definitionResult?.definitions && definitionResult.definitions.length > 1) {
return definitionResult.definitions.map(x => ({ fileName: x.fileName, offset: x.textSpan.start }));
}
@ -471,7 +471,7 @@ export class Mangler {
return;
}
this.allExportedSymbols.add(new DeclarationData(node.getSourceFile().fileName, node, service, fileIdents));
this.allExportedSymbols.add(new DeclarationData(node.getSourceFile().fileName, node, fileIdents));
}
}
@ -620,7 +620,7 @@ export class Mangler {
}
const newText = data.replacementName;
for (const { fileName, offset } of data.locations) {
for (const { fileName, offset } of data.getLocations(service)) {
queueRename(fileName, offset, newText);
}
}
@ -723,7 +723,8 @@ export class Mangler {
service.dispose();
this.renameWorkerPool.terminate();
this.log(`Done: ${savedBytes / 1000}kb saved, memory-usage: ${JSON.stringify(process.memoryUsage())}`);
this.log(`Done: ${savedBytes / 1000}kb saved, memory-usage: ${JSON.stringify(v8.getHeapStatistics())}`);
return result;
}
}