@@ -10,18 +10,14 @@ type TokenCountsCache = {
1010 [ workspace_root : string ] : {
1111 modified_at : number
1212 files : {
13- [ file_path : string ] : {
14- modified_at : number
15- token_count : number
16- compact_token_count ?: number
17- }
13+ [ file_path : string ] : [ number , number , number ]
1814 }
1915 }
2016}
2117
2218const SHOW_COUNTING_NOTIFICATION_DELAY_MS = 3000
2319
24- const TOKEN_CACHE_FILE_NAME = 'token-count -cache.json'
20+ const TOKEN_CACHE_FILE_NAME = 'token-counts -cache.json'
2521
2622export class TokenCalculator implements vscode . Disposable {
2723 private _file_token_counts : Map < string , number > = new Map ( )
@@ -32,6 +28,7 @@ export class TokenCalculator implements vscode.Disposable {
3228 private _directory_selected_compact_token_counts : Map < string , number > =
3329 new Map ( )
3430 private _token_cache : TokenCountsCache = { }
31+ private _session_cache : TokenCountsCache = { }
3532 private _token_cache_update_timeout : NodeJS . Timeout | null = null
3633 private _has_token_counts_cache_updated_once = false
3734
@@ -76,8 +73,8 @@ export class TokenCalculator implements vscode.Disposable {
7673 }
7774
7875 for ( const root of this . _provider . get_workspace_roots ( ) ) {
79- if ( this . _token_cache [ root ] ) {
80- current_global_cache [ root ] = this . _token_cache [ root ]
76+ if ( this . _session_cache [ root ] ) {
77+ current_global_cache [ root ] = this . _session_cache [ root ]
8178 }
8279 }
8380
@@ -107,7 +104,29 @@ export class TokenCalculator implements vscode.Disposable {
107104 data : error
108105 } )
109106 }
110- } , 10000 )
107+ } , 5000 )
108+ }
109+
110+ private _update_session_cache (
111+ workspace_root : string ,
112+ relative_path : string ,
113+ mtime : number ,
114+ token_count : number ,
115+ compact_token_count : number
116+ ) : void {
117+ if ( ! this . _session_cache [ workspace_root ] ) {
118+ this . _session_cache [ workspace_root ] = {
119+ modified_at : Date . now ( ) ,
120+ files : { }
121+ }
122+ }
123+ this . _session_cache [ workspace_root ] . files [ relative_path ] = [
124+ mtime ,
125+ token_count ,
126+ compact_token_count
127+ ]
128+ this . _session_cache [ workspace_root ] . modified_at = Date . now ( )
129+ this . _update_token_counts_cache ( )
111130 }
112131
113132 public async with_token_counting_notification < T > (
@@ -219,19 +238,32 @@ export class TokenCalculator implements vscode.Disposable {
219238 if ( workspace_root && ! range ) {
220239 try {
221240 const stats = await fs . promises . stat ( file_path )
222- mtime = stats . mtimeMs
241+ mtime = Math . floor ( stats . mtimeMs )
242+ const relative_path = path
243+ . relative ( workspace_root , file_path )
244+ . replace ( / \\ / g, '/' )
245+
246+ const cached_file =
247+ this . _token_cache [ workspace_root ] ?. files ?. [ relative_path ]
248+
223249 if (
224- this . _token_cache [ workspace_root ] ?. files ?. [ file_path ] ?. modified_at ==
225- mtime
250+ cached_file &&
251+ Array . isArray ( cached_file ) &&
252+ cached_file [ 0 ] == mtime
226253 ) {
227- const tokens =
228- this . _token_cache [ workspace_root ] . files [ file_path ] . token_count
229- const compact_tokens =
230- this . _token_cache [ workspace_root ] . files [ file_path ]
231- . compact_token_count || tokens // Fallback for old cache
254+ const tokens = cached_file [ 1 ]
255+ const compact_tokens = cached_file [ 2 ]
232256
233257 this . _file_token_counts . set ( file_path , tokens )
234258 this . _file_compact_token_counts . set ( file_path , compact_tokens )
259+
260+ this . _update_session_cache (
261+ workspace_root ,
262+ relative_path ,
263+ mtime ,
264+ tokens ,
265+ compact_tokens
266+ )
235267 return { total : tokens , compact : compact_tokens }
236268 }
237269 } catch {
@@ -285,13 +317,25 @@ export class TokenCalculator implements vscode.Disposable {
285317 files : { }
286318 }
287319 }
288- this . _token_cache [ workspace_root ] . files [ file_path ] = {
289- modified_at : mtime ,
290- token_count : token_count ,
291- compact_token_count : compact_token_count
292- }
320+
321+ const relative_path = path
322+ . relative ( workspace_root , file_path )
323+ . replace ( / \\ / g, '/' )
324+
325+ this . _token_cache [ workspace_root ] . files [ relative_path ] = [
326+ mtime ,
327+ token_count ,
328+ compact_token_count
329+ ]
293330 this . _token_cache [ workspace_root ] . modified_at = Date . now ( )
294- this . _update_token_counts_cache ( )
331+
332+ this . _update_session_cache (
333+ workspace_root ,
334+ relative_path ,
335+ mtime ,
336+ token_count ,
337+ compact_token_count
338+ )
295339 }
296340
297341 return { total : token_count , compact : compact_token_count }
0 commit comments