libdrv icon indicating copy to clipboard operation
libdrv copied to clipboard

Performance analysis: Document inefficiencies and fix resource leaks

Open Copilot opened this issue 2 months ago • 0 comments

Analyzed codebase for performance bottlenecks. Documented 60+ inefficiencies across 7 categories without changing functional behavior. Fixed critical MDL resource leaks in file copy operations.

Performance Issues Documented

High impact:

  • BCrypt algorithm providers opened/closed per hash operation instead of cached at module level
  • Memory allocated/freed on every enumeration loop iteration (Registry, system handles)

Medium impact:

  • 60+ instances of RtlZeroMemory immediately before buffer is overwritten
  • NonPagedPool used at PASSIVE_LEVEL where PagedPool is appropriate
  • Linear memory growth (+4096) instead of using returned required size

Low impact:

  • 16KB file I/O buffers where 64KB would reduce syscalls

Bug Fixes

Fixed MDL leaks in KfcCopyFile:

// Before: MDL never freed
PMDL mdl = IoAllocateMdl(buffer, size, FALSE, TRUE, nullptr);
if (error) {
    ExFreePoolWithTag(buffer, TAG);
    return error;  // MDL leaked
}

// After: MDL freed on all paths
PMDL mdl = IoAllocateMdl(buffer, size, FALSE, TRUE, nullptr);
if (!mdl) {
    ExFreePoolWithTag(buffer, TAG);
    return STATUS_INSUFFICIENT_RESOURCES;
}
if (error) {
    IoFreeMdl(mdl);
    ExFreePoolWithTag(buffer, TAG);
    return error;
}

Documentation

  • PERFORMANCE.md - Technical analysis with file locations, line numbers, and code examples
  • PERFORMANCE_SUMMARY.md - Executive summary with prioritization and implementation roadmap
  • Inline comments at key locations reference detailed documentation

Recommendations can be implemented separately based on priority. All changes maintain compatibility with Windows XP+ across x86/x64/ARM/ARM64.

Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Dec 13 '25 03:12 Copilot