Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/wshrpc/wshremote/wshremote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -57,6 +58,17 @@ func (impl *ServerImpl) remoteStreamFileDir(ctx context.Context, path string, by
if err != nil {
return fmt.Errorf("cannot open dir %q: %w", path, err)
}
sort.Slice(innerFilesEntries, func(i, j int) bool {
iInfo, iErr := innerFilesEntries[i].Info()
Copy link
Contributor

Choose a reason for hiding this comment

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

WARNING: Performance concern with sorting large directories

Calling .Info() on every entry during each comparison can be expensive. With the new MaxDirSize of 5000, sorting could trigger up to ~10,000 system calls (each comparison calls Info() twice, and sorting requires O(n log n) comparisons).

Consider caching the FileInfo results before sorting:

type entryWithInfo struct {
    entry os.DirEntry
    info  fs.FileInfo
    err   error
}

// Pre-fetch all Info() calls
entriesWithInfo := make([]entryWithInfo, len(innerFilesEntries))
for i, entry := range innerFilesEntries {
    info, err := entry.Info()
    entriesWithInfo[i] = entryWithInfo{entry, info, err}
}

// Sort using cached info
sort.Slice(entriesWithInfo, func(i, j int) bool {
    if entriesWithInfo[i].err != nil || entriesWithInfo[j].err != nil {
        return false
    }
    return entriesWithInfo[i].info.ModTime().After(entriesWithInfo[j].info.ModTime())
})

// Extract sorted entries
for i, ewi := range entriesWithInfo {
    innerFilesEntries[i] = ewi.entry
}

jInfo, jErr := innerFilesEntries[j].Info()
if iErr != nil {
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

WARNING: Inconsistent error handling in sort comparator

When iErr != nil, the function returns false (treating i as "less than" j), but when jErr != nil, it returns true (treating j as "greater than" i). This asymmetric handling means entries with stat errors will be sorted to the beginning of the list.

Consider handling both error cases consistently, such as:

  • Return false for both error cases (keep original order)
  • Or skip entries with errors entirely before sorting

}
if jErr != nil {
return true
}
return iInfo.ModTime().After(jInfo.ModTime())
})
if byteRange.All {
if len(innerFilesEntries) > wshrpc.MaxDirSize {
innerFilesEntries = innerFilesEntries[:wshrpc.MaxDirSize]
Expand Down
2 changes: 1 addition & 1 deletion pkg/wshrpc/wshrpctypes_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
// MaxFileSize is the maximum file size that can be read
MaxFileSize = 50 * 1024 * 1024 // 50M
// MaxDirSize is the maximum number of entries that can be read in a directory
MaxDirSize = 1024
MaxDirSize = 5000
// FileChunkSize is the size of the file chunk to read
FileChunkSize = 64 * 1024
// DirChunkSize is the size of the directory chunk to read
Expand Down