mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Add a simple queue implementation with better performance than Array.shift (#49623)
* Add a simple queue implementation with better performance than `Array.shift` This lets us clean up the hack introduced in #49581 * Correct typo Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
This commit is contained in:
1 parent
b24b6a1125
commit
020ef41543
7 files changed
+81
-44
No files matched your search
@@ -3509,10 +3509,11 @@ namespace ts {
|
||||
|
||||
export function isExportsOrModuleExportsOrAlias(sourceFile: SourceFile, node: Expression): boolean {
|
||||
let i = 0;
|
||||
const q = [node];
|
||||
while (q.length && i < 100) {
|
||||
const q = createQueue<Expression>();
|
||||
q.enqueue(node);
|
||||
while (!q.isEmpty() && i < 100) {
|
||||
i++;
|
||||
node = q.shift()!;
|
||||
node = q.dequeue();
|
||||
if (isExportsIdentifier(node) || isModuleExportsAccessExpression(node)) {
|
||||
return true;
|
||||
}
|
||||
@@ -3520,10 +3521,10 @@ namespace ts {
|
||||
const symbol = lookupSymbolForName(sourceFile, node.escapedText);
|
||||
if (!!symbol && !!symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration) && !!symbol.valueDeclaration.initializer) {
|
||||
const init = symbol.valueDeclaration.initializer;
|
||||
q.push(init);
|
||||
q.enqueue(init);
|
||||
if (isAssignmentExpression(init, /*excludeCompoundAssignment*/ true)) {
|
||||
q.push(init.left);
|
||||
q.push(init.right);
|
||||
q.enqueue(init.left);
|
||||
q.enqueue(init.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1492,6 +1492,47 @@ namespace ts {
|
||||
return createMultiMap() as UnderscoreEscapedMultiMap<T>;
|
||||
}
|
||||
|
||||
export function createQueue<T>(items?: readonly T[]): Queue<T> {
|
||||
const elements: (T | undefined)[] = items?.slice() || [];
|
||||
let headIndex = 0;
|
||||
|
||||
function isEmpty() {
|
||||
return headIndex === elements.length;
|
||||
}
|
||||
|
||||
function enqueue(...items: T[]) {
|
||||
elements.push(...items);
|
||||
}
|
||||
|
||||
function dequeue(): T {
|
||||
if (isEmpty()) {
|
||||
throw new Error("Queue is empty");
|
||||
}
|
||||
|
||||
const result = elements[headIndex] as T;
|
||||
elements[headIndex] = undefined; // Don't keep referencing dequeued item
|
||||
headIndex++;
|
||||
|
||||
// If more than half of the queue is empty, copy the remaining elements to the
|
||||
// front and shrink the array (unless we'd be saving fewer than 100 slots)
|
||||
if (headIndex > 100 && headIndex > (elements.length >> 1)) {
|
||||
const newLength = elements.length - headIndex;
|
||||
elements.copyWithin(/*target*/ 0, /*start*/ headIndex);
|
||||
|
||||
elements.length = newLength;
|
||||
headIndex = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
enqueue,
|
||||
dequeue,
|
||||
isEmpty,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Set with custom equality and hash code functionality. This is useful when you
|
||||
* want to use something looser than object identity - e.g. "has the same span".
|
||||
|
||||
@@ -8998,4 +8998,11 @@ namespace ts {
|
||||
negative: boolean;
|
||||
base10Value: string;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export interface Queue<T> {
|
||||
enqueue(...items: T[]): void;
|
||||
dequeue(): T;
|
||||
isEmpty(): boolean;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user