Repository navigation
Description
The associated forum post URL from https://forum.rclone.org
This change is small and easy to apply I think.
What is your current rclone version (output from rclone version)?
v1.72.1
What problem are you are trying to solve?
Removing unnecessary heap allocation.
How do you think rclone should be changed to solve that?
Just move the location to declare variables.
- 3 variables in
WaitForStateContext
rclone/backend/oracleobjectstorage/waiter.go
Lines 63 to 64 in 28c187b
| notfoundTick := 0 | |
| targetOccurrence := 0 |
| result := Result{} |
Currently, these variables will be allocated in heap, which consumes more memory. They are all used in go func() but not used in other places. So they can be declared in go func().
- change
=to:=, which is a tiny change.
rclone/cmd/serve/sftp/connection.go
Line 294 in 28c187b
| err = req.Reply(ok, reply) |
Because of =, the assignment op, err variable declared in handleChannel will be allocated in heap, which is unnecessary. Change = to := to declare err in the function literal will avoid allocating it in heap. This is really a small change but it does work. 😂
How to use GitHub
- Please use the 👍 reaction to show that you are affected by the same issue.
- Please don't comment if you have no relevant information to add. It's just extra noise for everyone subscribed to this issue.
- Subscribe to receive notifications on status change and new comments.
Activity
[-]small refactoring to reduce heap allocation[/-][+]small refactoring to reduce memory usage[/+]oracleobjectstorage, sftp: eliminate unnecessary heap allocation
ncw commentedon Jan 5, 2026
You can see with
Whether this actually makes a difference. The go compiler is quite capable of working out what escapes and what doesn't so that might be interesting to confirm your code changes are working.
Lslightly commentedon Jan 6, 2026
The escape analysis log before changing is as follows. Only relevant lines are extracted here.
After changing,
notfoundTick,targetOccurrence,result,errare allocated in stack.I think the log proves the tiny improvement.
There is another variable whose type is pointer is allocated in heap and can be allocated in stack through code changing. But I don't change that in PR.
rclone/backend/ftp/ftp.go
Lines 883 to 903 in 28c187b
In the above code,
c, which is moved to heap, can be passed to the goroutine through argument to avoid the pointer objectcitself allocated in heap. To avoidcstill references underlying object, the code can be changed as follows just like what*pc = nilinputFtpConnection(&c, err)does.I should have shown the assembly where heap allocation functionI made it. Details are shown as follows.newobjectcan be found, but the assembly is too large.assembly of "ftp.(*Fs).List" of code before change. c is allocated in heap
assembly of "ftp.(*Fs).List" of code after change. c is allocated in stack.
Pardon the shameless plug, but I've developed a small linter that handles this issue. I come from the static analysis tool CodeQL with escape analysis extension which combines the escape analysis log and CodeQL database. Based on the extension and the heapvar_should_move.ql, I find these opportunities of optimization. For rclone, there are only 5 locations which seems not hot code.
ncw commentedon Jan 6, 2026
Every little bit helps :-)
Thanks for showing the before and after escape analysis - that is great to show it makes a difference.
oracleobjectstorage, sftp: eliminate unnecessary heap allocation
oracleobjectstorage, sftp: eliminate unnecessary heap allocation
oracleobjectstorage, sftp: eliminate unnecessary heap allocation
Sync from rclone/rclone (#1)
oracleobjectstorage, sftp: eliminate unnecessary heap allocation