Skip to content

Commit

Permalink
⏪️ Revert "🐛 Fix hash of binary files in cache service (#1707)"
Browse files Browse the repository at this point in the history
This reverts commit c18a593.
  • Loading branch information
misode committed Jan 31, 2025
1 parent 0fd9f26 commit 18c363f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
34 changes: 11 additions & 23 deletions packages/core/src/service/CacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ interface ValidateResult {
export class CacheService {
checksums = Checksums.create()
errors: ErrorCache = {}
dirtyFiles = new Set<string>()
#hasValidatedFiles = false

/**
Expand All @@ -70,9 +69,15 @@ export class CacheService {
if (!this.#hasValidatedFiles) {
return
}
this.dirtyFiles.add(doc.uri)
if (this.dirtyFiles.size > 100) {
await this.flushDirtyFiles()
try {
// TODO: Don't update this for every single change.
this.checksums.files[doc.uri] = await this.project.externals.crypto.getSha1(
doc.getText(),
)
} catch (e) {
if (!this.project.externals.error.isKind(e, 'EISDIR')) {
this.project.logger.error(`[CacheService#hash-file] ${doc.uri}`)
}
}
})
this.project.on('rootsUpdated', async ({ roots }) => {
Expand All @@ -84,7 +89,7 @@ export class CacheService {
this.checksums.roots[root] = await this.project.fs.hash(root)
} catch (e) {
if (!this.project.externals.error.isKind(e, 'EISDIR')) {
this.project.logger.error(`[CacheService#hash-root] ${root}`, e)
this.project.logger.error(`[CacheService#hash-root] ${root}`)
}
}
}
Expand All @@ -109,19 +114,6 @@ export class CacheService {
return this.#cacheFilePath
}

private async flushDirtyFiles() {
for (const uri of this.dirtyFiles) {
try {
this.checksums.files[uri] = await this.project.fs.hash(uri)
} catch (e) {
if (!this.project.externals.error.isKind(e, 'EISDIR')) {
this.project.logger.error(`[CacheService#flushDirtyFiles] ${uri}`, e)
}
}
}
this.dirtyFiles.clear()
}

async load(): Promise<LoadResult> {
const ans: LoadResult = { symbols: {} }
if (this.project.projectRoots.length === 0) {
Expand Down Expand Up @@ -171,7 +163,7 @@ export class CacheService {
}
} catch (e) {
if (!this.project.externals.error.isKind(e, 'EISDIR')) {
this.project.logger.error(`[CacheService#hash-file] ${uri}`, e)
this.project.logger.error(`[CacheService#hash-file] ${uri}`)
}
// Failed calculating hash. Assume the root has changed.
}
Expand Down Expand Up @@ -230,10 +222,6 @@ export class CacheService {
let filePath: string | undefined
try {
filePath = await this.getCacheFileUri()

await this.flushDirtyFiles()
__profiler.task('Hash Files')

const cache: CacheFile = {
version: LatestCacheVersion,
projectRoots: this.project.projectRoots,
Expand Down
15 changes: 6 additions & 9 deletions packages/core/src/service/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { fileUtil } from './fileUtil.js'
import { MetaRegistry } from './MetaRegistry.js'
import { ProfilerFactory } from './Profiler.js'

const CacheAutoSaveInterval = 300_000 // 5 Minutes.
const CacheAutoSaveInterval = 600_000 // 10 Minutes.

export type ProjectInitializerContext = Pick<
Project,
Expand Down Expand Up @@ -160,7 +160,7 @@ export class Project implements ExternalEventEmitter {

/** Prevent circular binding. */
readonly #bindingInProgressUris = new Set<string>()
#cacheSaverIntervalId: IntervalId | undefined = undefined
readonly #cacheSaverIntervalId: IntervalId
readonly cacheService: CacheService
/** URI of files that are currently managed by the language client. */
readonly #clientManagedUris = new Set<string>()
Expand Down Expand Up @@ -341,6 +341,10 @@ export class Project implements ExternalEventEmitter {

this.setInitPromise()
this.setReadyPromise()
this.#cacheSaverIntervalId = setInterval(
() => this.cacheService.save(),
CacheAutoSaveInterval,
)

this.on('documentUpdated', ({ doc, node }) => {
// if (!this.#isReady) {
Expand Down Expand Up @@ -582,13 +586,6 @@ export class Project implements ExternalEventEmitter {

__profiler.finalize()
this.emit('ready', {})

// Save the cache after ready and start the auto-cache interval
await this.cacheService.save()
this.#cacheSaverIntervalId = setInterval(
() => this.cacheService.save(),
CacheAutoSaveInterval,
)
}
this.#isReady = false
this.#readyPromise = ready()
Expand Down

0 comments on commit 18c363f

Please sign in to comment.