-
Notifications
You must be signed in to change notification settings - Fork 10.2k
perf(core): parallelize memory discovery file operations performance gain #5751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jacob314
merged 9 commits into
google-gemini:main
from
mag123c:perf/parallelize-memory-discovery
Aug 21, 2025
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad61737
perf(core): parallelize memory discovery file operations- Convert seqβ¦
mag123c 94dc034
test(core): add performance benchmarks for memory discovery
mag123c 853a4e6
fix(core): add concurrency limits to prevent EMFILE errors
mag123c 9037942
test: Replace performance test with functional tests for parallel proβ¦
mag123c 7cfbbf7
fix: Add error handling for directory processing
mag123c 16c4f5b
refactor: use Promise.allSettled() for better error isolation
mag123c 158e5da
Merge branch 'main' into perf/parallelize-memory-discovery
mag123c 78841b3
fix: resolve CI formatting and TypeScript errors
mag123c fbef860
Merge branch 'main' into perf/parallelize-memory-discovery
jacob314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
201 changes: 201 additions & 0 deletions
201
packages/core/src/utils/memoryDiscovery.performance.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, beforeEach, afterEach, expect } from 'vitest'; | ||
| import * as fs from 'fs/promises'; | ||
| import * as path from 'path'; | ||
| import { tmpdir } from 'os'; | ||
| import { loadServerHierarchicalMemory } from './memoryDiscovery.js'; | ||
| import { FileDiscoveryService } from '../services/fileDiscoveryService.js'; | ||
| import { processImports } from './memoryImportProcessor.js'; | ||
|
|
||
| // Helper to create test content | ||
| function createTestContent(index: number): string { | ||
| return `# GEMINI Configuration ${index} | ||
| ## Project Instructions | ||
| This is test content for performance benchmarking. | ||
| The content should be substantial enough to simulate real-world usage. | ||
| ### Code Style Guidelines | ||
| - Use TypeScript for type safety | ||
| - Follow functional programming patterns | ||
| - Maintain high test coverage | ||
| - Keep functions pure when possible | ||
| ### Architecture Principles | ||
| - Modular design with clear boundaries | ||
| - Clean separation of concerns | ||
| - Efficient resource usage | ||
| - Scalable and maintainable codebase | ||
| ### Development Guidelines | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
| Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris. | ||
| `.repeat(3); // Make content substantial | ||
| } | ||
|
|
||
| // Sequential implementation for comparison | ||
| async function readFilesSequential( | ||
| filePaths: string[], | ||
| ): Promise<Array<{ path: string; content: string | null }>> { | ||
| const results = []; | ||
| for (const filePath of filePaths) { | ||
| try { | ||
| const content = await fs.readFile(filePath, 'utf-8'); | ||
| const processedResult = await processImports( | ||
| content, | ||
| path.dirname(filePath), | ||
| false, | ||
| undefined, | ||
| undefined, | ||
| 'flat', | ||
| ); | ||
| results.push({ path: filePath, content: processedResult.content }); | ||
| } catch { | ||
| results.push({ path: filePath, content: null }); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| // Parallel implementation | ||
| async function readFilesParallel( | ||
| filePaths: string[], | ||
| ): Promise<Array<{ path: string; content: string | null }>> { | ||
| const promises = filePaths.map(async (filePath) => { | ||
| try { | ||
| const content = await fs.readFile(filePath, 'utf-8'); | ||
| const processedResult = await processImports( | ||
| content, | ||
| path.dirname(filePath), | ||
| false, | ||
| undefined, | ||
| undefined, | ||
| 'flat', | ||
| ); | ||
| return { path: filePath, content: processedResult.content }; | ||
| } catch { | ||
| return { path: filePath, content: null }; | ||
| } | ||
| }); | ||
| return Promise.all(promises); | ||
| } | ||
|
|
||
| describe('memoryDiscovery performance', () => { | ||
| let testDir: string; | ||
| let fileService: FileDiscoveryService; | ||
|
|
||
| beforeEach(async () => { | ||
| testDir = path.join(tmpdir(), `memoryDiscovery-perf-${Date.now()}`); | ||
| await fs.mkdir(testDir, { recursive: true }); | ||
| fileService = new FileDiscoveryService(testDir); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(testDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('should demonstrate significant performance improvement with parallel processing', async () => { | ||
| // Create test structure | ||
| const numFiles = 20; | ||
| const filePaths: string[] = []; | ||
|
|
||
| for (let i = 0; i < numFiles; i++) { | ||
| const dirPath = path.join(testDir, `project-${i}`); | ||
| await fs.mkdir(dirPath, { recursive: true }); | ||
|
|
||
| const filePath = path.join(dirPath, 'GEMINI.md'); | ||
| await fs.writeFile(filePath, createTestContent(i)); | ||
| filePaths.push(filePath); | ||
| } | ||
|
|
||
| // Measure sequential processing | ||
| const seqStart = performance.now(); | ||
| const seqResults = await readFilesSequential(filePaths); | ||
| const seqTime = performance.now() - seqStart; | ||
|
|
||
| // Measure parallel processing | ||
| const parStart = performance.now(); | ||
| const parResults = await readFilesParallel(filePaths); | ||
| const parTime = performance.now() - parStart; | ||
|
|
||
| // Verify results are equivalent | ||
| expect(seqResults.length).toBe(parResults.length); | ||
| expect(seqResults.length).toBe(numFiles); | ||
|
|
||
| // Verify parallel is faster | ||
| expect(parTime).toBeLessThan(seqTime); | ||
|
|
||
| // Calculate improvement | ||
| const improvement = ((seqTime - parTime) / seqTime) * 100; | ||
| const speedup = seqTime / parTime; | ||
|
|
||
| // Log results for visibility | ||
| console.log(`\n Performance Results (${numFiles} files):`); | ||
| console.log(` Sequential: ${seqTime.toFixed(2)}ms`); | ||
| console.log(` Parallel: ${parTime.toFixed(2)}ms`); | ||
| console.log(` Improvement: ${improvement.toFixed(1)}%`); | ||
| console.log(` Speedup: ${speedup.toFixed(2)}x\n`); | ||
|
|
||
| // Expect significant improvement | ||
| expect(improvement).toBeGreaterThan(50); // At least 50% improvement | ||
| }); | ||
|
|
||
| it('should handle the actual loadServerHierarchicalMemory function efficiently', async () => { | ||
| // Create multiple directories with GEMINI.md files | ||
| const dirs: string[] = []; | ||
| const numDirs = 10; | ||
|
|
||
| for (let i = 0; i < numDirs; i++) { | ||
| const dirPath = path.join(testDir, `workspace-${i}`); | ||
| await fs.mkdir(dirPath, { recursive: true }); | ||
| dirs.push(dirPath); | ||
|
|
||
| // Create GEMINI.md file | ||
| const content = createTestContent(i); | ||
| await fs.writeFile(path.join(dirPath, 'GEMINI.md'), content); | ||
|
|
||
| // Create nested structure | ||
| const nestedPath = path.join(dirPath, 'src', 'components'); | ||
| await fs.mkdir(nestedPath, { recursive: true }); | ||
| await fs.writeFile(path.join(nestedPath, 'GEMINI.md'), content); | ||
| } | ||
|
|
||
| // Measure performance | ||
| const startTime = performance.now(); | ||
|
|
||
| const result = await loadServerHierarchicalMemory( | ||
| dirs[0], | ||
| dirs.slice(1), | ||
| false, // debugMode | ||
| fileService, | ||
| [], // extensionContextFilePaths | ||
| 'flat', // importFormat | ||
| undefined, // fileFilteringOptions | ||
| 200, // maxDirs | ||
| ); | ||
|
|
||
| const duration = performance.now() - startTime; | ||
|
|
||
| // Verify results | ||
| expect(result.fileCount).toBeGreaterThan(0); | ||
| expect(result.memoryContent).toBeTruthy(); | ||
|
|
||
| // Log performance | ||
| console.log(`\n Real-world Performance:`); | ||
| console.log( | ||
| ` Processed ${result.fileCount} files in ${duration.toFixed(2)}ms`, | ||
| ); | ||
| console.log( | ||
| ` Rate: ${(result.fileCount / (duration / 1000)).toFixed(2)} files/second\n`, | ||
| ); | ||
|
|
||
| // Performance should be reasonable | ||
| expect(duration).toBeLessThan(1000); // Should complete within 1 second | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.