Skip to content

fix: replace os.ReadDir with fs.ReadDir(1) to avoid extra memory usage#2959

Merged
andyzhangx merged 1 commit intomasterfrom
readdir-fix
Feb 4, 2026
Merged

fix: replace os.ReadDir with fs.ReadDir(1) to avoid extra memory usage#2959
andyzhangx merged 1 commit intomasterfrom
readdir-fix

Conversation

@andyzhangx
Copy link
Copy Markdown
Member

What type of PR is this?
/kind cleanup

What this PR does / why we need it:
fix: replace os.ReadDir with fs.ReadDir(1) to avoid extra memory usage

Which issue(s) this PR fixes:

Fixes #

Requirements:

Special notes for your reviewer:

Release note:

none

@k8s-ci-robot k8s-ci-robot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Feb 4, 2026
@andyzhangx andyzhangx requested a review from Copilot February 4, 2026 01:24
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 4, 2026
@k8s-ci-robot k8s-ci-robot requested review from cvvz and gnufied February 4, 2026 01:24
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: andyzhangx

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 4, 2026
Copy link
Copy Markdown

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 optimizes memory usage in the ensureMountPoint function by replacing os.ReadDir(target) with f.ReadDir(1). The original implementation read all directory entries just to verify mount point accessibility, which could be expensive for directories with many files. The new implementation reads only a single entry, achieving the same validation purpose with significantly reduced memory footprint.

Changes:

  • Added io import to handle io.EOF for empty directories
  • Replaced os.ReadDir(target) with os.Open(target) followed by f.ReadDir(1)
  • Added proper handling for empty directories (io.EOF case)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@andyzhangx
Copy link
Copy Markdown
Member Author

/retest

@andyzhangx andyzhangx merged commit 183b9ac into master Feb 4, 2026
33 of 34 checks passed
@andyzhangx andyzhangx deleted the readdir-fix branch February 27, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants