Skip to content

Commit b74ff12

Browse files
committed
Cache line column tables
This shows up *heavily* in profiles. What really needs to happen is necessary info is precomputed and parsed once after we recieve edits for files and then reused everywhere *until the next edit*. That’s a larger refactor so this is a stop gap measure until then.
1 parent e586933 commit b74ff12

File tree

1 file changed

+14
-2
lines changed
  • packages/tailwindcss-language-service/src/util

1 file changed

+14
-2
lines changed

packages/tailwindcss-language-service/src/util/find.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Range, Position } from 'vscode-languageserver'
1+
import { type Range, type Position, LRUCache } from 'vscode-languageserver'
22
import type { TextDocument } from 'vscode-languageserver-textdocument'
33
import type { DocumentClassName, DocumentClassList, State, DocumentHelperFunction } from './state'
44
import lineColumn from 'line-column'
@@ -591,8 +591,20 @@ export function findHelperFunctionsInRange(
591591
return fns
592592
}
593593

594+
let lineTableCache = new LRUCache<string, ReturnType<typeof lineColumn>>(20)
595+
596+
function createLineTable(str: string) {
597+
let existing = lineTableCache.get(str)
598+
if (existing) return existing
599+
600+
let table = lineColumn(str + '\n')
601+
lineTableCache.set(str, table)
602+
return table
603+
}
604+
594605
export function indexToPosition(str: string, index: number): Position {
595-
const { line, col } = lineColumn(str + '\n').fromIndex(index) ?? { line: 1, col: 1 }
606+
let table = createLineTable(str)
607+
let { line, col } = table.fromIndex(index) ?? { line: 1, col: 1 }
596608
return { line: line - 1, character: col - 1 }
597609
}
598610

0 commit comments

Comments
 (0)