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

StorageReadObjects function optimization #1145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aliciMehmet
Copy link

It will be more efficient in terms of performance to access the elements of the slice and put the values instead of appending it after creating the slice.

@CLAassistant
Copy link

CLAassistant commented Nov 21, 2023

CLA assistant check
All committers have signed the CLA.

@zyro
Copy link
Member

zyro commented Nov 22, 2023

The allocation was already being done once, so resizing wasn't needed during the loop iterations if that's the concern. Do you have a little benchmark test that compares the two approaches?

@aliciMehmet
Copy link
Author

I don't know it is the appropriate test but here is my example benchmark code and results:

import (
	"testing"
)

var simpleObjs = []simpleObj{{
	Collection: "Collection-1",
	Key:        "0",
	UserId:     "309adcaf-9d5e-44b6-89f2-7b48e1f9ccab",
},
	{
		Collection: "Collection-2",
		Key:        "0",
		UserId:     "309adcaf-9d5e-44b6-89f2-7b48e1f9ccab",
	},
	{
		Collection: "Collection-3",
		Key:        "0",
		UserId:     "309adcaf-9d5e-44b6-89f2-7b48e1f9ccab",
	}}

func BenchmarkOriginalApproach(b *testing.B) {
	for i := 0; i < b.N; i++ {
		OriginalApproach(simpleObjs)
	}
}

func BenchmarkMyApproach(b *testing.B) {
	for i := 0; i < b.N; i++ {
		MyApproach(simpleObjs)
	}
}

func OriginalApproach(objs []simpleObj) {
	collectionParam := make([]string, 0, len(objs))
	keyParam := make([]string, 0, len(objs))
	userIdParam := make([]string, 0, len(objs))

	for _, s := range objs {
		collectionParam = append(collectionParam, s.Collection)
		keyParam = append(keyParam, s.Key)
		userIdParam = append(userIdParam, s.UserId)
	}
}

func MyApproach(objs []simpleObj) {
	collectionParam := make([]string, len(objs))
	keyParam := make([]string, len(objs))
	userIdParam := make([]string, len(objs))

	for index, s := range objs {
		collectionParam[index] = s.Collection
		keyParam[index] = s.Key
		userIdParam[index] = s.UserId
	}
}

type simpleObj struct {
	Collection string
	Key        string
	UserId     string
}

I don't know if it is worth changing the code, after testing I couldn't decide if it had much of an effect.

BenchmarkOriginalApproach-20 12865542 92.40 ns/op
BenchmarkOriginalApproach-20 12672062 93.03 ns/op
BenchmarkOriginalApproach-20 12060676 92.77 ns/op
BenchmarkOriginalApproach-20 12616186 91.90 ns/op
BenchmarkOriginalApproach-20 12636925 93.12 ns/op

BenchmarkMyApproach-20 13567692 88.95 ns/op
BenchmarkMyApproach-20 12824156 88.11 ns/op
BenchmarkMyApproach-20 13065788 89.47 ns/op
BenchmarkMyApproach-20 13172916 90.31 ns/op
BenchmarkMyApproach-20 13523168 90.43 ns/op

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.

None yet

3 participants