Skip to content

Commit 7e12a3e

Browse files
committed
WIP using strong typed collection types for better perf
1 parent c39c7c7 commit 7e12a3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+759
-1208
lines changed

src.compiler/csharp/CSharpAstTransformer.ts

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,28 +2808,7 @@ export default class CSharpAstTransformer {
28082808

28092809
protected visitArrayLiteralExpression(parent: cs.Node, expression: ts.ArrayLiteralExpression) {
28102810
if (this.isMapInitializer(expression)) {
2811-
const csExpr = {
2812-
parent: parent,
2813-
tsNode: expression,
2814-
nodeType: cs.SyntaxKind.InvocationExpression,
2815-
arguments: [],
2816-
expression: {} as cs.Expression
2817-
} as cs.InvocationExpression;
2818-
2819-
csExpr.expression = this.makeMemberAccess(
2820-
csExpr,
2821-
this._context.makeTypeName('alphaTab.core.TypeHelper'),
2822-
this._context.toPascalCase('createMapEntry')
2823-
);
2824-
2825-
expression.elements.forEach(e => {
2826-
const ex = this.visitExpression(csExpr, e);
2827-
if (ex) {
2828-
csExpr.arguments.push(ex);
2829-
}
2830-
});
2831-
2832-
return csExpr;
2811+
return this.createMapEntry(parent, expression);
28332812
} else if (this.isSetInitializer(expression)) {
28342813
const csExpr = {
28352814
parent: parent,
@@ -2876,6 +2855,31 @@ export default class CSharpAstTransformer {
28762855
return csExpr;
28772856
}
28782857
}
2858+
protected createMapEntry(parent: cs.Node, expression: ts.ArrayLiteralExpression): cs.Expression {
2859+
const csExpr = {
2860+
parent: parent,
2861+
tsNode: expression,
2862+
nodeType: cs.SyntaxKind.InvocationExpression,
2863+
arguments: [],
2864+
expression: {} as cs.Expression
2865+
} as cs.InvocationExpression;
2866+
2867+
csExpr.expression = this.makeMemberAccess(
2868+
csExpr,
2869+
this._context.makeTypeName('alphaTab.core.TypeHelper'),
2870+
this._context.toPascalCase('createMapEntry')
2871+
);
2872+
2873+
expression.elements.forEach(e => {
2874+
const ex = this.visitExpression(csExpr, e);
2875+
if (ex) {
2876+
csExpr.arguments.push(ex);
2877+
}
2878+
});
2879+
2880+
return csExpr;
2881+
}
2882+
28792883
protected isMapInitializer(expression: ts.ArrayLiteralExpression) {
28802884
const isCandidate =
28812885
expression.elements.length === 2 &&
@@ -2962,6 +2966,13 @@ export default class CSharpAstTransformer {
29622966
return 'TrimStart';
29632967
}
29642968
break;
2969+
case 'NumberConstructor':
2970+
case 'Number':
2971+
switch (symbol.name) {
2972+
case 'toString':
2973+
return 'toInvariantString';
2974+
}
2975+
break;
29652976
}
29662977
return null;
29672978
}
@@ -3069,7 +3080,7 @@ export default class CSharpAstTransformer {
30693080
} as cs.InvocationExpression;
30703081
const memberAccess = (callExpr.expression = {
30713082
expression: null!,
3072-
member: this._context.toPascalCase('toString'),
3083+
member: this._context.toPascalCase('toInvariantString'),
30733084
parent: callExpr,
30743085
tsNode: expr.tsNode,
30753086
nodeType: cs.SyntaxKind.MemberAccessExpression
@@ -3085,22 +3096,6 @@ export default class CSharpAstTransformer {
30853096
expr.parent = par;
30863097
memberAccess.expression = par;
30873098

3088-
const invariantCultureInfo = {
3089-
parent: callExpr,
3090-
nodeType: cs.SyntaxKind.MemberAccessExpression,
3091-
tsNode: expr.tsNode,
3092-
expression: null!,
3093-
member: this._context.toPascalCase('invariantCulture')
3094-
} as cs.MemberAccessExpression;
3095-
3096-
invariantCultureInfo.expression = {
3097-
parent: invariantCultureInfo,
3098-
tsNode: expr.tsNode,
3099-
nodeType: cs.SyntaxKind.Identifier,
3100-
text: this._context.makeTypeName('system.globalization.CultureInfo')
3101-
} as cs.Identifier;
3102-
callExpr.arguments.push(invariantCultureInfo);
3103-
31043099
return callExpr;
31053100
}
31063101

@@ -3203,6 +3198,13 @@ export default class CSharpAstTransformer {
32033198
nodeType: cs.SyntaxKind.InvocationExpression
32043199
} as cs.InvocationExpression;
32053200

3201+
// number.ToString
3202+
const isNumberToString =
3203+
ts.isPropertyAccessExpression(expression.expression) &&
3204+
this._context.typeChecker.getTypeAtLocation(expression.expression.expression).flags & ts.TypeFlags.Number &&
3205+
(expression.expression.name as ts.Identifier).text === 'toString' &&
3206+
expression.arguments.length === 0;
3207+
32063208
callExpression.expression = this.visitExpression(callExpression, expression.expression)!;
32073209
if (!callExpression.expression) {
32083210
return null;
@@ -3222,31 +3224,6 @@ export default class CSharpAstTransformer {
32223224
}
32233225
});
32243226

3225-
// number.ToString
3226-
const isNumberToString =
3227-
ts.isPropertyAccessExpression(expression.expression) &&
3228-
this._context.typeChecker.getTypeAtLocation(expression.expression.expression).flags & ts.TypeFlags.Number &&
3229-
(expression.expression.name as ts.Identifier).text === 'toString' &&
3230-
expression.arguments.length === 0;
3231-
3232-
if (isNumberToString) {
3233-
const invariantCultureInfo = {
3234-
parent: parent,
3235-
nodeType: cs.SyntaxKind.MemberAccessExpression,
3236-
tsNode: expression,
3237-
expression: null!,
3238-
member: this._context.toPascalCase('invariantCulture')
3239-
} as cs.MemberAccessExpression;
3240-
3241-
invariantCultureInfo.expression = {
3242-
parent: invariantCultureInfo,
3243-
tsNode: expression.expression,
3244-
nodeType: cs.SyntaxKind.Identifier,
3245-
text: this._context.makeTypeName('system.globalization.CultureInfo')
3246-
} as cs.Identifier;
3247-
3248-
callExpression.arguments.push(invariantCultureInfo);
3249-
}
32503227

32513228
if (expression.typeArguments) {
32523229
callExpression.typeArguments = [];

src.compiler/kotlin/KotlinAstPrinter.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -619,23 +619,20 @@ export default class KotlinAstPrinter extends AstPrinterBase {
619619
} else {
620620
var elementTypeName = this.getContainerTypeName(arrayType.elementType);
621621
if (elementTypeName) {
622-
this.write('alphaTab.core.');
623-
if (!forNew) {
624-
this.write('I');
625-
}
622+
this.write('alphaTab.collections.');
626623
this.write(elementTypeName);
627624
this.write('List');
628625
} else {
629626
const isDynamicArray =
630627
cs.isPrimitiveTypeNode(arrayType.elementType) &&
631628
arrayType.elementType.type === cs.PrimitiveType.Dynamic;
632629
if (isDynamicArray && !forNew) {
633-
this.write('alphaTab.core.IList<*>');
630+
this.write('alphaTab.collections.List<*>');
634631
} else {
635632
if (forNew) {
636-
this.write('alphaTab.core.List<');
633+
this.write('alphaTab.collections.List<');
637634
} else {
638-
this.write('alphaTab.core.IList<');
635+
this.write('alphaTab.collections.List<');
639636
}
640637
this.writeType(arrayType.elementType);
641638
this.write('>');
@@ -650,11 +647,7 @@ export default class KotlinAstPrinter extends AstPrinterBase {
650647
var keyTypeName = this.getContainerTypeName(mapType.keyType);
651648
var valueTypeName = this.getContainerTypeName(mapType.valueType);
652649

653-
this.write('alphaTab.core.');
654-
if (!forNew) {
655-
this.write('I');
656-
}
657-
650+
this.write('alphaTab.collections.');
658651
if (keyTypeName && valueTypeName) {
659652

660653
this.write(keyTypeName);
@@ -948,11 +941,12 @@ export default class KotlinAstPrinter extends AstPrinterBase {
948941
}
949942

950943
let type = elementType ? this.getContainerTypeName(elementType) : null;
944+
this.write('alphaTab.collections.')
951945
if (type) {
952-
this.write(type.substr(0, 1).toLowerCase() + type.substr(1));
953-
this.write('ListOf')
946+
this.write(type);
947+
this.write('List')
954948
} else {
955-
this.write('objectListOf');
949+
this.write('List');
956950
if (expr.type && cs.isArrayTypeNode(expr.type)) {
957951
this.write('<');
958952
this.writeType(expr.type.elementType);
@@ -977,7 +971,8 @@ export default class KotlinAstPrinter extends AstPrinterBase {
977971
}
978972
} else if (expr.values && expr.values.length > 0) {
979973
// TODO: check for typed array creation
980-
this.write('objectListOf(');
974+
this.write('alphaTab.collections.')
975+
this.write('List(');
981976
this.writeCommaSeparated(expr.values, v => {
982977
if (expr.values!.length > 10) {
983978
this.writeLine();

src.compiler/kotlin/KotlinAstTransformer.ts

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ export default class KotlinAstTransformer extends CSharpAstTransformer {
1111
}
1212

1313
public get extension(): string {
14-
return '.kt'
14+
return '.kt';
1515
}
1616

17-
1817
private _paramReferences: Map<string, cs.Identifier[]>[] = [];
1918
private _paramsWithAssignment: Set<string>[] = [];
2019

@@ -98,7 +97,8 @@ export default class KotlinAstTransformer extends CSharpAstTransformer {
9897
// a == this or this == a
9998
// within an equals method needs to have the operator ===
10099

101-
if (bin &&
100+
if (
101+
bin &&
102102
cs.isBinaryExpression(bin) &&
103103
(expression.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
104104
expression.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken) &&
@@ -265,12 +265,10 @@ export default class KotlinAstTransformer extends CSharpAstTransformer {
265265
switch (symbol.name) {
266266
case 'length':
267267
if (
268-
expression.parent && (
269-
cs.isReturnStatement(expression.parent) ||
270-
cs.isVariableDeclaration(expression.parent) ||
271-
(cs.isBinaryExpression(expression.parent) &&
272-
expression.parent.operator === '=')
273-
)
268+
expression.parent &&
269+
(cs.isReturnStatement(expression.parent) ||
270+
cs.isVariableDeclaration(expression.parent) ||
271+
(cs.isBinaryExpression(expression.parent) && expression.parent.operator === '='))
274272
) {
275273
return 'length.toDouble()';
276274
}
@@ -286,6 +284,14 @@ export default class KotlinAstTransformer extends CSharpAstTransformer {
286284
return 'lowercase';
287285
case 'toUpperCase':
288286
return 'uppercase';
287+
case 'split':
288+
return 'splitBy';
289+
}
290+
break;
291+
case 'Number':
292+
switch (symbol.name) {
293+
case 'toString':
294+
return 'toInvariantString';
289295
}
290296
break;
291297
}
@@ -355,7 +361,7 @@ export default class KotlinAstTransformer extends CSharpAstTransformer {
355361
} as cs.MemberAccessExpression;
356362

357363
let expr = this.visitExpression(methodAccess, expression.expression);
358-
if(!expr) {
364+
if (!expr) {
359365
return null;
360366
}
361367
methodAccess.expression = expr;
@@ -386,4 +392,65 @@ export default class KotlinAstTransformer extends CSharpAstTransformer {
386392
let targetType = this._context.typeChecker.getTypeFromTypeNode(expression.type);
387393
return targetType.flags & ts.TypeFlags.Enum || targetType.flags & ts.TypeFlags.EnumLiteral;
388394
}
395+
396+
protected createMapEntry(parent: cs.Node, expression: ts.ArrayLiteralExpression): cs.Expression {
397+
const csExpr = {
398+
parent: parent,
399+
tsNode: expression,
400+
nodeType: cs.SyntaxKind.InvocationExpression,
401+
arguments: [],
402+
expression: {} as cs.Expression
403+
} as cs.InvocationExpression;
404+
405+
let mapEntryTypeName = 'MapEntry';
406+
if (expression.elements.length === 2) {
407+
const keyType = this._context.getType(expression.elements[0]);
408+
let keyTypeContainerName = this.getContainerTypeName(keyType);
409+
410+
const valueType = this._context.getType(expression.elements[1]);
411+
let valueTypeContainerName = this.getContainerTypeName(valueType);
412+
413+
if (keyTypeContainerName || valueTypeContainerName) {
414+
keyTypeContainerName = keyTypeContainerName || 'Object';
415+
valueTypeContainerName = valueTypeContainerName || 'Object';
416+
mapEntryTypeName = keyTypeContainerName + valueTypeContainerName + mapEntryTypeName;
417+
}
418+
}
419+
420+
csExpr.expression = {
421+
nodeType: cs.SyntaxKind.Identifier,
422+
text: this._context.makeTypeName(`alphaTab.collections.${mapEntryTypeName}`),
423+
parent: csExpr,
424+
tsNode: expression
425+
} as cs.Identifier;
426+
427+
expression.elements.forEach(e => {
428+
const ex = this.visitExpression(csExpr, e);
429+
if (ex) {
430+
csExpr.arguments.push(ex);
431+
}
432+
});
433+
434+
return csExpr;
435+
}
436+
437+
private getContainerTypeName(tsType: ts.Type): string | null {
438+
if (this._context.isNullableType(tsType)) {
439+
return null;
440+
}
441+
if (
442+
(tsType.flags & ts.TypeFlags.Enum) !== 0 ||
443+
(tsType.flags & ts.TypeFlags.EnumLike) !== 0 ||
444+
(tsType.flags & ts.TypeFlags.EnumLiteral) !== 0
445+
) {
446+
return null;
447+
}
448+
if ((tsType.flags & ts.TypeFlags.Number) !== 0 || (tsType.flags & ts.TypeFlags.NumberLiteral) !== 0) {
449+
return 'Double';
450+
}
451+
if ((tsType.flags & ts.TypeFlags.Boolean) !== 0 || (tsType.flags & ts.TypeFlags.BooleanLiteral) !== 0) {
452+
return 'Boolean';
453+
}
454+
return null;
455+
}
389456
}

src.kotlin/alphaTab/build.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ buildscript {
22
repositories {
33
gradlePluginPortal()
44
google()
5-
jcenter()
65
mavenCentral()
76
}
87
dependencies {
@@ -14,7 +13,6 @@ buildscript {
1413
allprojects {
1514
repositories {
1615
google()
17-
jcenter()
1816
mavenCentral()
1917
maven("https://packages.jetbrains.team/maven/p/skija/maven")
2018
}

src.kotlin/alphaTab/settings.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pluginManagement {
22
repositories {
33
google()
4-
jcenter()
54
gradlePluginPortal()
65
mavenCentral()
76
}

0 commit comments

Comments
 (0)