-
Notifications
You must be signed in to change notification settings - Fork 13
perf: eliminate redundant syscalls in JSON serialization and filesystem operations #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
codspeed-hq
wants to merge
1
commit into
main
Choose a base branch
from
codspeed/optim-eliminate-redundant-syscalls-in-json-serialization-1781211259953
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−29
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -292,8 +292,9 @@ func (fs *Filesystem) ReadFile(path string) (*FileWithContentByte, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| // Get owner and group | ||
| owner, group, err := fs.getFileOwnerAndGroup(absPath) | ||
| // Get owner and group from the already-obtained file info, | ||
| // avoiding a redundant os.Lstat call | ||
| owner, group, err := getOwnerAndGroupFromInfo(info) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -386,20 +387,26 @@ func (fs *Filesystem) ListDirectory(path string) (*Directory, error) { | |
| for _, entry := range entries { | ||
| // Use displayPath for the entry paths too | ||
| entryPath := filepath.Join(displayPath, entry.Name()) | ||
| absEntryPath := filepath.Join(absPath, entry.Name()) | ||
|
|
||
| // Use os.Lstat to get info about the symlink itself, not its target | ||
| // This prevents errors when symlinks point to non-existent targets | ||
| info, err := os.Lstat(absEntryPath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if info.IsDir() { | ||
| // Use entry.IsDir() from DirEntry which uses the file type cached | ||
| // by os.ReadDir (from getdents64 on Linux), avoiding an os.Lstat syscall | ||
| // for directory entries entirely. | ||
| if entry.IsDir() { | ||
| dir.AddSubdirectory(&Subdirectory{Path: entryPath, Name: entry.Name()}) | ||
| } else { | ||
| // It's a file or symlink | ||
| owner, group, err := fs.getFileOwnerAndGroup(absEntryPath) | ||
| // For files and symlinks, we need stat info for metadata | ||
| absEntryPath := filepath.Join(absPath, entry.Name()) | ||
|
|
||
| // Use os.Lstat to get info about the symlink itself, not its target | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, nevermind, I didn't realize that |
||
| // This prevents errors when symlinks point to non-existent targets | ||
| info, err := os.Lstat(absEntryPath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Extract owner and group from the already-obtained FileInfo, | ||
| // avoiding a redundant os.Lstat call in getFileOwnerAndGroup | ||
| owner, group, err := getOwnerAndGroupFromInfo(info) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -508,14 +515,10 @@ func (fs *Filesystem) MoveFile(src, dst string) error { | |
| return os.Rename(srcAbs, dstAbs) | ||
| } | ||
|
|
||
| // getFileOwnerAndGroup returns the owner and group of a file | ||
| func (fs *Filesystem) getFileOwnerAndGroup(path string) (string, string, error) { | ||
| // Use Lstat to get info about the symlink itself, not its target | ||
| info, err := os.Lstat(path) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
|
|
||
| // getOwnerAndGroupFromInfo extracts the owner and group from an already-obtained | ||
| // os.FileInfo, avoiding a redundant os.Lstat syscall. This is used by callers | ||
| // that have already stat'd the file (ListDirectory, ReadFile, GetFileInfo). | ||
| func getOwnerAndGroupFromInfo(info os.FileInfo) (string, string, error) { | ||
| stat, ok := info.Sys().(*syscall.Stat_t) | ||
| if !ok { | ||
| return "", "", errors.New("failed to get file stat") | ||
|
|
@@ -541,6 +544,17 @@ func (fs *Filesystem) getFileOwnerAndGroup(path string) (string, string, error) | |
| return ownerName, groupName, nil | ||
| } | ||
|
|
||
| // getFileOwnerAndGroup returns the owner and group of a file | ||
| func (fs *Filesystem) getFileOwnerAndGroup(path string) (string, string, error) { | ||
| // Use Lstat to get info about the symlink itself, not its target | ||
| info, err := os.Lstat(path) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
|
|
||
| return getOwnerAndGroupFromInfo(info) | ||
| } | ||
|
|
||
| // GetFileInfo returns file information without reading its content | ||
| func (fs *Filesystem) GetFileInfo(path string) (*FileByte, error) { | ||
| absPath, err := fs.GetAbsolutePath(path) | ||
|
|
@@ -557,7 +571,9 @@ func (fs *Filesystem) GetFileInfo(path string) (*FileByte, error) { | |
| return nil, errors.New("path points to a directory, not a file") | ||
| } | ||
|
|
||
| owner, group, err := fs.getFileOwnerAndGroup(absPath) | ||
| // Get owner and group from the already-obtained file info, | ||
| // avoiding a redundant os.Lstat call | ||
| owner, group, err := getOwnerAndGroupFromInfo(info) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug (P2): Switching from
ShouldBindJSONto rawjson.NewDecoder().Decode()silently drops Gin's struct validation. Any request struct withbinding:"required"tags (e.g.,ProcessRequest.Command,DriveMountRequest.DriveName) will no longer have those constraints enforced, allowing invalid requests through.Suggested change
Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's break these JSON-related changes into a separate PR. They seem completely unrelated to the Lstat-related changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fair point. The JSON serialization changes (switching to
jsoniter, addingwriteJSONResponse(), updatingBindJSON()inbase.go) and the Lstat optimizations (eliminating redundant syscalls infilesystem.go) are independent optimizations targeting different layers — HTTP serialization vs. filesystem I/O. They don't share any functional dependency.Splitting them would make each PR easier to review, benchmark independently, and revert if needed.