Skip to content
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

style: changed all simple path concatenation by fmt.Sprintf #567

Closed
wants to merge 2 commits into from

Conversation

iamamirsalehi
Copy link

Related Issue or Design Document

Checklist

  • I have read the contributing guidelines and signed the CLA.
  • I have referenced an issue containing the design document if my change introduces a new feature.
  • I have read the security policy.
  • I confirm that this pull request does not address a security vulnerability.
    If this pull request addresses a security vulnerability,
    I confirm that I got approval (please contact [email protected]) from the maintainers to push the changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary documentation within the code base (if appropriate).

Further comments

@CLAassistant
Copy link

CLAassistant commented Feb 17, 2025

CLA assistant check
All committers have signed the CLA.

@iamamirsalehi iamamirsalehi changed the title Changed all simple path concatenation by fmt.Sprintf style: changed all simple path concatenation by fmt.Sprintf Feb 17, 2025
@asahasrabuddhe
Copy link

I am not sure if this change is a step in the right direction. fmt.Sprintf is significantly less performant than simple concatenations. By including this change, we would considerably deteriorate the performance of the code.

Consider the following snippet:

package main

import (
	"fmt"
	"testing"
)


func BenchmarkConcatStringByAdd(b *testing.B) {
	id := "123e4567-e89b-12d3-a456-426614174000"
	path := ""
	b.ReportAllocs()
	for b.Loop() {
		path = "/containers/" + id + "/json"
	}
	_ = path
}

func BenchmarkConcatStringBySprintf(b *testing.B) {
	id := "123e4567-e89b-12d3-a456-426614174000"
	path := ""
	b.ReportAllocs()
	for b.Loop() {
		path = fmt.Sprintf("/containers/%s/json", id)
	}
	_ = path
}

Benchmarking the above code yields the following result:

goos: darwin
goarch: arm64
cpu: Apple M1 Pro
BenchmarkConcatStringByAdd
BenchmarkConcatStringByAdd-10        	43265994	        27.32 ns/op	      64 B/op	       1 allocs/op
BenchmarkConcatStringBySprintf
BenchmarkConcatStringBySprintf-10    	18439422	        64.13 ns/op	      80 B/op	       2 allocs/op
PASS

The fmt.Sprintf concatenation is almost 2x slower than simple concatenations. I hope you consider this and change all the fmt.Sprintf to instead be simple concatenations and improve the performance of this library.

@iamamirsalehi
Copy link
Author

Thanks @asahasrabuddhe for your tip, I applied what you mentioned

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution, however we generally don’t merge style changes in code that have little added value (we merge bug fixes or feature improvements). This code itself is forked from Docker and we generally don’t accept changes there due to the security nature unless it fixes a known issuey

@aeneasr aeneasr closed this Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants