Skip to content

Commit

Permalink
perfs: speedup Git file last edit lookup (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviortheking committed Nov 5, 2024
1 parent 7ba02ba commit 0e079d1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
Binary file modified bun.lockb
Binary file not shown.
Binary file modified server/bun.lockb
Binary file not shown.
39 changes: 28 additions & 11 deletions server/compiler/utils/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { objectSize } from '@dzeio/object-util'
import Queue from '@dzeio/queue'
import { glob } from 'glob'
import { exec, spawn } from 'node:child_process'
import { writeFileSync } from 'node:fs'
Expand Down Expand Up @@ -126,7 +127,7 @@ function runCommand(command: string, useSpawn = true): Promise<string> {
})
}

let lastEditsCache: Record<string, string> = {}
const lastEditsCache: Record<string, string> = {}
export async function loadLastEdits() {
console.log('Loading Git File Tree...')
const firstCommand = 'git ls-tree -r --name-only HEAD ../data'
Expand All @@ -136,19 +137,35 @@ export async function loadLastEdits() {
console.log('Loaded files tree', files.length, 'files')
console.log('Loading their last edit time')
let processed = 0
for (let file of files) {
const queue = new Queue(1000, 10)
queue.start()

for await (let file of files) {
file = file.replace(/"/g, '').replace("\\303\\251", "é")
try {
// don't really know why but it does not correctly execute the command when using Spawn
lastEditsCache[file] = await runCommand(`git log -1 --pretty="format:%cd" --date=iso-strict "${file}"`, false)
} catch {
await queue.add(runCommand(`git log -1 --pretty="format:%cd" --date=iso-strict "${file}"`, false).then((res) => {
lastEditsCache[file] = res
})
.catch(() => {
console.warn('could not load file', file, 'hope it does not break everything else lol')
}
processed++
if (processed % 1000 === 0) {
console.log('loaded', processed, 'out of', files.length, 'files', `(${(processed / files.length * 100).toFixed(0)}%)`)
}
})
.finally(() => {
processed++
if (processed % 1000 === 0) {
console.log('loaded', processed, 'out of', files.length, 'files', `(${(processed / files.length * 100).toFixed(0)}%)`)
}
}))
// try {
// // don't really know why but it does not correctly execute the command when using Spawn
// lastEditsCache[file] = await runCommand(`git log -1 --pretty="format:%cd" --date=iso-strict "${file}"`, false)
// } catch {
// console.warn('could not load file', file, 'hope it does not break everything else lol')
// }
// processed++
// if (processed % 1000 === 0) {
// console.log('loaded', processed, 'out of', files.length, 'files', `(${(processed / files.length * 100).toFixed(0)}%)`)
// }
}
await queue.waitEnd()
console.log('done loading files', objectSize(lastEditsCache))
}

Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"dependencies": {
"@dzeio/config": "^1",
"@dzeio/object-util": "^1",
"@dzeio/queue": "^1",
"@tcgdex/sdk": "^2",
"apicache": "^1",
"express": "^4",
"graphql": "^15",
"graphql-http": "^1.22.1",
"graphql-http": "^1",
"ruru": "^2.0.0-beta.14",
"swagger-ui-express": "^5.0.1",
"yaml": "^2.5.0"
Expand Down

0 comments on commit 0e079d1

Please sign in to comment.