Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache range and position #940

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 36 additions & 24 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,36 +998,35 @@ export class Util {
};
}

/**
* A cache of `Range` objects. The key is a 52bit integer created from the 4 range integers and leveraging bitshifting.
* The whole point of this cache is to reduce garbage collection churn, so we didn't want to use string concatenation for the key
*/
private rangeCache = new Map<number, Range>();

/**
* Helper for creating `Range` objects. Prefer using this function because vscode-languageserver's `Range.create()` is significantly slower
*/
public createRange(startLine: number, startCharacter: number, endLine: number, endCharacter: number): Range {
return {
start: {
line: startLine,
character: startCharacter
},
end: {
line: endLine,
character: endCharacter
}
};
// eslint-disable-next-line no-bitwise
const key = (startLine << 39) + (startCharacter << 26) + (endLine << 13) + endCharacter;

let range = this.rangeCache.get(key);
if (!range) {
range = {
start: this.createPosition(startLine, startCharacter),
end: this.createPosition(endLine, endCharacter)
};
this.rangeCache.set(key, range);
}
return range;
}

/**
* Create a `Range` from two `Position`s
*/
public createRangeFromPositions(startPosition: Position, endPosition: Position): Range {
return {
start: {
line: startPosition.line,
character: startPosition.character
},
end: {
line: endPosition.line,
character: endPosition.character
}
};
return this.createRange(startPosition.line, startPosition.character, endPosition.line, endPosition.character);
}

/**
Expand Down Expand Up @@ -1067,14 +1066,27 @@ export class Util {
}
}

/**
* A cache of `Position` objects. The key is a 26bit integer created from line and character and leveraging bitshifting
* The whole point of this cache is to reduce garbage collection churn, so we didn't want to use string concatenation for the key
*/
private positionCache = new Map<number, Position>();

/**
* Create a `Position` object. Prefer this over `Position.create` for performance reasons
*/
public createPosition(line: number, character: number) {
return {
line: line,
character: character
};
// eslint-disable-next-line no-bitwise
const key = (line << 13) + character;
let position = this.positionCache.get(key);
if (!position) {
position = {
line: line,
character: character
};
this.positionCache.set(key, position);
}
return position;
}

/**
Expand Down