Skip to content

Commit

Permalink
chore(jsii): cache results of case conversions (#3602)
Browse files Browse the repository at this point in the history
The `Case` library appears on top of the heavy weights when profiling
`jsii` compilation runs on `aws-cdk-lib`, accounting for about `9.6%` of
the overall execution time (`6,712` out of `68,935` ms). This is quite
significant.

Faster libraries performing case conversions exist, however they do not
offer the same level of functinoality that `Case` does, and often
require knowing which casing the string is presently at, when `Case`
performs normalization before conversion (this likely explains why these
conversions are somewhat expensive), which is convenient in our
use-case. It does not seem to be possible to replace `Case` with a
faster implementation, as there does not seem to be any.

Running a survey on the usage of all functions from `Case` being used
during usch a compilation resulted in the following results:

```
Case.camel: 1881755 cached out of 1893235 (99.39%) - 11480 entries / 164.9 hits per entry
Case.pascal: 1890795 cached out of 1909959 (99.00%) - 19164 entries / 99.7 hits per entry
Case.snake: 1882773 cached out of 1895359 (99.34%) - 12586 entries / 150.6 hits per entry
```

Since there is a cache hit rate of `99%` and entries are re-used `100`
times each on average, it appears that adding a cache on those functions
will help alleviate that cost. Indeed, after making this change, `Case`
is no longer present above the `100ms` mark in the list of heavy
weights produced by the profiler.

In order to avoid possibly trading performance for out-of-memory errors,
the caches are keyed off of weak references, so that the caches can be
garbage collected when under memory pressure.



---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
RomainMuller authored Jun 16, 2022
1 parent 837f300 commit 8e2eea6
Show file tree
Hide file tree
Showing 73 changed files with 149 additions and 95 deletions.
4 changes: 1 addition & 3 deletions gh-pages/partials/node-support-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

| Release | Status | End-of-Life |
| --------- | ---------------------------- | ------------ |
| `^12.7.0` | :white_check_mark: Supported | `2022-04-30` |
| `^14.5.0` | :white_check_mark: Supported | `2023-04-30` |
| `^14.6.0` | :white_check_mark: Supported | `2023-04-30` |
| `^16.3.0` | :white_check_mark: Supported | `2024-04-30` |
| `^17.3.0` | :test_tube: Best effort | `2022-06-01` |
| `^18.0.0` | :white_check_mark: Supported | `2025-04-30` |

??? question "Status Definitions"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@jest/types": "^28.1.1",
"@types/jest": "^28.1.1",
"@types/node": "^12.20.55",
"@types/node": "^14.18.21",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"all-contributors-cli": "^6.20.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/@fixtures/jsii-calc-bundled/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/@fixtures/jsii-calc-bundled"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "index.js"
}
5 changes: 3 additions & 2 deletions packages/@jsii/benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
"fs-extra": "^10.1.0",
"jsii": "^0.0.0",
"tar": "^6.1.11",
"typescript-3.9": "npm:typescript@~3.9.10",
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/glob": "^7.2.0",
"glob": "^8.0.3"
},
"scripts": {
"build": "tsc --build && npm run lint",
"watch": "tsc -w",
"build": "yarn --silent tsc --build && npm run lint",
"watch": "yarn --silent tsc -w",
"bench": "node bin/benchmark.js",
"snapshot": "node scripts/snapshot-package.js",
"lint": "eslint . --ext .ts --ignore-path=.gitignore",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/check-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/@jsii/check-node"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/check-node/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class NodeRelease {
// Currently active releases
new NodeRelease(14, {
endOfLife: new Date('2023-04-30'),
supportedRange: '^14.5.0',
supportedRange: '^14.6.0',
}),
new NodeRelease(16, {
endOfLife: new Date('2024-04-30'),
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/dotnet-runtime-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"directory": "packages/@jsii/dotnet-runtime-test"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/dotnet-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"directory": "packages/@jsii/dotnet-runtime"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/java-runtime-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"directory": "packages/@jsii/java-runtime-test"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/java-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"directory": "packages/@jsii/java-runtime"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/@jsii/kernel"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/python-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"directory": "packages/@jsii/python-runtime"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/@jsii/runtime"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/@jsii/spec"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@scope/jsii-calc-base-of-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"directory": "packages/@scope/jsii-calc-base-of-base"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "build/lib/index.js",
"types": "build/lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@scope/jsii-calc-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"directory": "packages/@scope/jsii-calc-base"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@scope/jsii-calc-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"directory": "packages/@scope/jsii-calc-lib"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/codemaker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/codemaker"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test"
],
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/jsii-diff"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-pacmak/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/jsii-pacmak"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-reflect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"directory": "packages/jsii-reflect"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { createSourceFile, ScriptKind, ScriptTarget, SyntaxKind } from 'typescript';
import { createSourceFile, ScriptKind, ScriptTarget, SyntaxKind } from 'typescript-3.9';

import { TypeScriptSnippet, SnippetParameters, ApiLocation } from './snippet';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/jsii/jsii-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { inferredTypeOfExpression, BuiltInType, builtInTypeName, mapElementType } from '../typescript/types';
import { hasAnyFlag, analyzeStructType, JsiiSymbol } from './jsii-utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/jsii/jsii-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as spec from '@jsii/spec';
import { symbolIdentifier } from 'jsii';
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { AstRenderer } from '../renderer';
import { typeContainsUndefined } from '../typescript/types';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/csharp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { determineJsiiType, JsiiType, ObjectLiteralStruct } from '../jsii/jsii-types';
import { JsiiSymbol, simpleName, namespaceName } from '../jsii/jsii-utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { analyzeObjectLiteral, ObjectLiteralStruct } from '../jsii/jsii-types';
import { isNamedLikeStruct, isJsiiProtocolType } from '../jsii/jsii-utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/go.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import { JsiiSymbol, simpleName, namespaceName } from '../jsii/jsii-utils';
// import { jsiiTargetParameter } from '../jsii/packages';
import { AssertionError } from 'assert';
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { analyzeObjectLiteral, determineJsiiType, JsiiType, ObjectLiteralStruct } from '../jsii/jsii-types';
import { OTree } from '../o-tree';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/java.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { determineJsiiType, JsiiType, analyzeObjectLiteral, ObjectLiteralStruct } from '../jsii/jsii-types';
import { JsiiSymbol, simpleName, namespaceName } from '../jsii/jsii-utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/python.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { determineJsiiType, JsiiType, ObjectLiteralStruct } from '../jsii/jsii-types';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/record-references.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { lookupJsiiSymbol } from '../jsii/jsii-utils';
import { TargetLanguage } from '../languages/target-language';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/languages/visualize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { OTree } from '../o-tree';
import { AstRenderer, AstHandler, nimpl, CommentSyntax } from '../renderer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ReplaceTypeScriptTransform extends ReplaceCodeTransform {
public constructor(api: ApiLocation, strict: boolean, replacer: TypeScriptReplacer) {
super((block, line) => {
const languageParts = block.language ? block.language.split(' ') : [];
if (languageParts[0] !== 'typescript' && languageParts[0] !== 'ts') {
if (languageParts[0] !== 'typescript-3.9' && languageParts[0] !== 'ts') {
return block;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { TargetLanguage } from './languages';
import { NO_SYNTAX, OTree, UnknownSyntax, Span } from './o-tree';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/rosetta-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class RosettaTabletReader {

const translated = this.translateSnippet(snippet, targetLang);

return translated ?? { language: 'typescript', source: example };
return translated ?? { language: 'typescript-3.9', source: example };
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/tablets/tablets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class TranslatedSnippet {
public get originalSource(): Translation {
return {
source: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,
language: 'typescript',
language: 'typescript-3.9',
didCompile: this.snippet.didCompile,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/translate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';
import { inspect } from 'util';

import { TARGET_LANGUAGES, TargetLanguage } from './languages';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/typescript/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { AstRenderer } from '../renderer';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/typescript/imports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { JsiiSymbol, parentSymbol, lookupJsiiSymbolFromNode } from '../jsii/jsii-utils';
import { AstRenderer } from '../renderer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { Spans } from './visible-spans';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/typescript/ts-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

export class TypeScriptCompiler {
private readonly realHost = ts.createCompilerHost(STANDARD_COMPILER_OPTIONS, true);
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/typescript/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { hasAllFlags, hasAnyFlag, resolveEnumLiteral, resolvedSymbolAtLocation } from '../jsii/jsii-utils';
import { isDefined } from '../util';
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/typescript/visible-spans.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { Span } from '../o-tree';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { RosettaDiagnostic } from './translate';

Expand Down
4 changes: 2 additions & 2 deletions packages/jsii-rosetta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@jsii/spec": "0.0.0",
"commonmark": "^0.30.0",
"fs-extra": "^10.1.0",
"typescript": "~3.9.10",
"typescript-3.9": "npm:typescript@~3.9.10",
"sort-json": "^2.0.1",
"@xmldom/xmldom": "^0.8.2",
"workerpool": "^6.2.1",
Expand All @@ -53,6 +53,6 @@
"directory": "packages/jsii-rosetta"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 14.6.0"
}
}
2 changes: 1 addition & 1 deletion packages/jsii-rosetta/test/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import * as ts from 'typescript-3.9';

import { annotateStrictDiagnostic, hasStrictBranding } from '../lib/util';

Expand Down
Loading

0 comments on commit 8e2eea6

Please sign in to comment.