Skip to content

feat: Build incremental AST caching layer to eliminate redundant re-parsing #3

Description

@Nanle-code

Overview

Every invocation of scan() re-parses all target .sol files from scratch, even when files have not changed since the last scan. In the VS Code extension use case (scan-on-save), this means the entire workspace is re-parsed on every keystroke-triggered save, which is wasteful and adds perceptible latency on large projects.

Problem

For a project with 50 Solidity files, a full workspace scan re-parses all 50 files even if only 1 changed. The @solidity-parser/parser is fast, but at scale (e.g. a large DeFi protocol with 100+ contracts and inheritance chains), this becomes the dominant cost in the scan pipeline.

Measured impact in benchmarks:

  • 10 files: ~40ms parse time (acceptable)
  • 50 files: ~200ms parse time (noticeable)
  • 100+ files: ~500ms+ parse time (degrades UX)

Proposed Solution

Implement a content-addressed AST cache with the following design:

interface ASTCacheEntry {
  contentHash: string;      // SHA-256 of file contents
  ast: ASTNode;
  parsedAt: number;         // timestamp
  filePath: string;
}
  1. Before parsing, compute SHA-256 of file content
  2. Check an in-memory LRU cache (bounded to 200 entries) for a matching hash
  3. On cache hit, return cached AST — skip @solidity-parser/parser entirely
  4. On cache miss, parse and store in cache
  5. In the VS Code extension, persist cache to globalStorageUri across sessions
  6. Expose a clearCache() export from @chainproof/core for testing and forced refresh

Performance Target

  • Re-scan of unchanged workspace: >80% reduction in parse time
  • Single changed file scan: cache hit for all other files, only changed file re-parsed

Acceptance Criteria

  • ASTCache class implemented in packages/core/src/ast/cache.ts
  • LRU eviction policy with configurable max size (default: 200 entries)
  • VS Code extension persists cache to context.globalStorageUri
  • CLI does not persist cache between runs (in-memory only)
  • clearCache() exported from @chainproof/core
  • Benchmark results documented in PR description showing parse time reduction

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions