Skip to content

Commit eb15a7d

Browse files
committed
perf: inline getStructNameRange into getStructNameFromType
getStructNameRange searches bounds of type name, but to do this it returns array of 2 elements: start + end. But as it invoked oftenly this can create lots of intermediate arrays and waste memory. This function was inlined into getStructNameFromType and now substituteStructName just invokes getStructNameFromType and then 'replace' with given typename. This is more optimal, because substituteStructName invoked more rare that first.
1 parent 04289cc commit eb15a7d

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

src/utils.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function isValidIdentifier(value: string) {
2020
return identifierRegex.test(value);
2121
}
2222

23-
/* Get start-end indexes range for given type */
24-
function getStructNameRange(type: string) {
23+
export function getStructNameFromType(type: string) {
24+
/* [const] [struct] NAME [*]+ */
2525
/*
2626
* Start locating from end, because we can use '*' as the boundary of
2727
* typename end.
@@ -71,13 +71,7 @@ function getStructNameRange(type: string) {
7171
startOfIndentifier++;
7272
}
7373

74-
return [startOfIndentifier, endOfIdentifier + 1] as const;
75-
}
76-
77-
export function getStructNameFromType(type: string) {
78-
/* [const] [struct] NAME [*]+ */
79-
const [start, end] = getStructNameRange(type);
80-
return type.substring(start, end);
74+
return type.substring(startOfIndentifier, endOfIdentifier + 1);
8175
}
8276

8377
/**
@@ -89,19 +83,8 @@ export function getStructNameFromType(type: string) {
8983
* @returns Result type name
9084
*/
9185
export function substituteStructName(type: string, target: string) {
92-
const [start, end] = getStructNameRange(type);
93-
94-
/* Add some optimized paths to reduce number of allocations */
95-
if (start === 0) {
96-
return `${target}${type.substring(end)}`;
97-
}
98-
99-
if (end === type.length) {
100-
return `${type.substring(0, start)}${target}`;
101-
}
102-
103-
return `${type.substring(0, start)}${target}${type.substring(end)}`;
104-
}
86+
const typename = getStructNameFromType(type);
87+
return type.replace(typename, target);}
10588

10689
/*
10790
* Check that 'type' contains exact count of pointers in it

0 commit comments

Comments
 (0)