-
Notifications
You must be signed in to change notification settings - Fork 616
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
stringArray := make([]string, len(array)) | ||
var sb strings.Builder | ||
sb.Grow(len(array) * 22) | ||
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. why 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. The number 22 is based on an estimated average length of the string representation of the big.Int numbers in the array. |
||
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 | ||
|
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.
can you add unit test for your 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.
I only modified the internal structure of the function and ran the TestConvertBigIntArrayToString test. The result passed successfully.
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.
the code path running through this function (i.e. parsing
BatchWithdrawERC1155
andBatchDepositERC1155
) is not the major path. thus it's likely thatConvertBigIntArrayToString
won't affect the total perf.