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

perf: use strings.Builder #1571

Closed
wants to merge 4 commits into from
Closed
Changes from 3 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: 7 additions & 5 deletions bridge-history-api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ func GetBlocksInRange(ctx context.Context, cli *ethclient.Client, start, end uin

// ConvertBigIntArrayToString convert the big int array to string
func ConvertBigIntArrayToString(array []*big.Int) string {
Copy link
Member

Choose a reason for hiding this comment

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

can you add unit test for your changes?

Copy link
Author

Choose a reason for hiding this comment

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

I only modified the internal structure of the function and ran the TestConvertBigIntArrayToString test. The result passed successfully.

Copy link
Member

Choose a reason for hiding this comment

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

the code path running through this function (i.e. parsing BatchWithdrawERC1155 and BatchDepositERC1155) is not the major path. thus it's likely that ConvertBigIntArrayToString won't affect the total perf.

stringArray := make([]string, len(array))
var sb strings.Builder
sb.Grow(len(array) * 22)
Copy link
Member

Choose a reason for hiding this comment

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

why use 22 here?

Copy link
Author

Choose a reason for hiding this comment

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

The number 22 is based on an estimated average length of the string representation of the big.Int numbers in the array.
A big.Int can have varying lengths depending on the size of the integer. However, for typical use cases, a 22-character length is likely a reasonable estimate to account for common values (including any digits and the potential minus sign for negative numbers). In the worst case, big.Int values can be much larger, but 22 is likely a safe upper bound for common integers.

for i, num := range array {
stringArray[i] = num.String()
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(num.String())
}

result := strings.Join(stringArray, ", ")
return result
return sb.String()
}

// ConvertStringToStringArray takes a string with values separated by commas and returns a slice of strings
Expand Down