Don't read same file multiple times

This commit is contained in:
Andras Schmelczer 2025-01-07 22:32:15 +00:00
commit f8dcca5367
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -115,9 +115,13 @@ export class Syncer {
// If there's no metadata, it must be a new file // If there's no metadata, it must be a new file
if (!metadata) { if (!metadata) {
// Perhaps the file has been moved. Let's check by looking at the deleted files // Perhaps the file has been moved. Let's check by looking at the deleted files
const contentBytes =
await this.operations.read(relativePath);
const contentHash = hash(contentBytes);
const originalFile = const originalFile =
await this.findMatchingFileBasedOnHash( await this.findMatchingFileBasedOnHash(
relativePath, contentHash,
locallyDeletedFiles locallyDeletedFiles
); );
if (originalFile !== undefined) { if (originalFile !== undefined) {
@ -133,7 +137,11 @@ export class Syncer {
updateTime: updateTime:
await this.operations.getModificationTime( await this.operations.getModificationTime(
relativePath relativePath
) ),
optimisations: {
contentBytes,
contentHash
}
}); });
} }
@ -196,15 +204,22 @@ export class Syncer {
private async internalSyncLocallyCreatedFile( private async internalSyncLocallyCreatedFile(
relativePath: RelativePath, relativePath: RelativePath,
updateTime: Date updateTime: Date,
optimisations?: {
contentBytes?: Uint8Array;
contentHash?: string;
}
): Promise<void> { ): Promise<void> {
await this.executeWhileHoldingFileLock( await this.executeWhileHoldingFileLock(
relativePath, relativePath,
SyncType.CREATE, SyncType.CREATE,
SyncSource.PUSH, SyncSource.PUSH,
async () => { async () => {
const contentBytes = await this.operations.read(relativePath); const contentBytes =
let contentHash = hash(contentBytes); optimisations?.contentBytes ??
(await this.operations.read(relativePath));
let contentHash =
optimisations?.contentHash ?? hash(contentBytes);
const localMetadata = this.database.getDocument(relativePath); const localMetadata = this.database.getDocument(relativePath);
if (localMetadata) { if (localMetadata) {
@ -270,11 +285,16 @@ export class Syncer {
private async internalSyncLocallyUpdatedFile({ private async internalSyncLocallyUpdatedFile({
oldPath, oldPath,
relativePath, relativePath,
updateTime updateTime,
optimisations
}: { }: {
oldPath?: RelativePath; oldPath?: RelativePath;
relativePath: RelativePath; relativePath: RelativePath;
updateTime: Date; updateTime: Date;
optimisations?: {
contentBytes?: Uint8Array;
contentHash?: string;
};
}): Promise<void> { }): Promise<void> {
await this.executeWhileHoldingFileLock( await this.executeWhileHoldingFileLock(
relativePath, relativePath,
@ -304,11 +324,15 @@ export class Syncer {
console.log("about to read", relativePath); console.log("about to read", relativePath);
await sleep(1000); await sleep(1000);
const contentBytes = await this.operations.read(relativePath); const contentBytes =
optimisations?.contentBytes ??
(await this.operations.read(relativePath));
console.log("has read", relativePath); console.log("has read", relativePath);
await sleep(1000); await sleep(1000);
let contentHash = hash(contentBytes); let contentHash =
optimisations?.contentHash ?? hash(contentBytes);
console.log("has hashed", relativePath); console.log("has hashed", relativePath);
await sleep(1000); await sleep(1000);
@ -640,11 +664,9 @@ export class Syncer {
} }
private async findMatchingFileBasedOnHash( private async findMatchingFileBasedOnHash(
filePath: RelativePath, contentHash: string,
candidates: [RelativePath, DocumentMetadata][] candidates: [RelativePath, DocumentMetadata][]
): Promise<[RelativePath, DocumentMetadata] | undefined> { ): Promise<[RelativePath, DocumentMetadata] | undefined> {
const contentHash = hash(await this.operations.read(filePath));
if (contentHash != EMPTY_HASH) { if (contentHash != EMPTY_HASH) {
return undefined; return undefined;
} }