Skip to content

Implement BufferCollection for Document caching#64

Merged
ultmaster merged 2 commits intomainfrom
codex/add-buffercollection-for-caching-documents
Jul 30, 2025
Merged

Implement BufferCollection for Document caching#64
ultmaster merged 2 commits intomainfrom
codex/add-buffercollection-for-caching-documents

Conversation

@ultmaster
Copy link
Contributor

Summary

  • add BufferCollection singleton for caching buffers
  • use the buffer cache when reading <Document> contents
  • clear the buffer cache on file save in the VS Code server
  • test buffer caching behaviour
  • compute object size recursively in BufferCollection and skip oversize values
  • add test for recursive calculateSize calculation

Testing

  • npm run build-webview
  • npm run build-cli (with warnings)
  • npm run lint (with warnings)
  • npm test
  • python -m pytest python/tests
  • xvfb-run -a npm run compile
  • npm run build-extension
  • xvfb-run -a npm run test-vscode

https://chatgpt.com/codex/tasks/task_e_68887d1df1e0832eaacfa92e007b2bce

Copilot AI review requested due to automatic review settings July 29, 2025 09:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a BufferCollection singleton for caching document buffers to improve performance when reading document contents multiple times. The caching system includes size limits and automatic eviction to prevent memory issues.

  • Adds BufferCollection class with size-aware caching and eviction logic
  • Integrates buffer caching into document reading functions (readDocx, readPdf, readTxt)
  • Clears cache on file save in VS Code server to ensure fresh content

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/poml/base.tsx Implements BufferCollection singleton with size calculation and cache management
packages/poml/components/document.tsx Adds readBufferCached function and integrates caching into document readers
packages/poml-vscode/lsp/server.ts Clears buffer cache on document save to ensure fresh content
packages/poml/tests/components.test.tsx Tests buffer caching behavior and size limit enforcement
packages/poml/tests/base.test.tsx Tests recursive size calculation functionality

if (t === 'number' || t === 'boolean' || t === 'bigint') {
return 8;
}
if (t === 'object') {
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding objects to the visited set before checking if they're arrays could cause issues. Arrays should be handled separately from generic objects to avoid incorrect size calculations.

Suggested change
if (t === 'object') {
if (t === 'object') {
if (Array.isArray(value)) {
visited.add(value);
let size = 0;
for (const element of value) {
try {
size += calculateSize(element, visited);
} catch {
// ignore
}
}
visited.delete(value);
return size;
}

Copilot uses AI. Check for mistakes.
Comment on lines +340 to +341
} catch {
// ignore
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try-catch block silently ignores all errors when calculating property sizes. This could mask important issues. Consider logging errors or being more specific about which exceptions to catch.

Suggested change
} catch {
// ignore
} catch (error) {
console.error(`Error calculating size for key "${key}":`, error);
// Continue processing other keys despite the error

Copilot uses AI. Check for mistakes.
return cached.value;
}
const buf = fs.readFileSync(abs);
BufferCollection.set(key, buf, buf.length, stat.mtimeMs);
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing buf.length as the size parameter is redundant since calculateSize() will be called anyway when size is provided. Either remove the size parameter or optimize to avoid double calculation.

Suggested change
BufferCollection.set(key, buf, buf.length, stat.mtimeMs);
BufferCollection.set(key, buf, stat.mtimeMs);

Copilot uses AI. Check for mistakes.

test('skip cache when over limit', () => {
BufferCollection.clear();
const inst = (BufferCollection as any).instance as any;
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 'as any' to access private members breaks encapsulation. Consider adding a public method for testing or using a test-specific subclass to avoid casting to any.

Copilot uses AI. Check for mistakes.
BufferCollection.clear();
const obj = { data: { arr: [1, 2, 'abc'] }, extra: Buffer.alloc(4) };
BufferCollection.set('obj', obj);
const inst = (BufferCollection as any).instance as any;
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 'as any' to access private members breaks encapsulation. Consider adding a public method for testing or using a test-specific subclass to avoid casting to any.

Copilot uses AI. Check for mistakes.
@ultmaster ultmaster merged commit b68bf33 into main Jul 30, 2025
3 checks passed
@ultmaster ultmaster deleted the codex/add-buffercollection-for-caching-documents branch August 27, 2025 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants