-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
strkey: reduce allocs in Encode (#4107)
Replace bytes.Buffer with working with a byte array directly. Also, contains the crc16 improvements by @2opremio that were in #4105. Reduce the allocations in strkey.Encode from 6 to 1, and reduce the heap use from 244 B/op to 64 B/op. In the past I have seen impact to performance of Starlight caused by the strkey.Encode functions. @2opremio also highlighted in #4105 there is an impact there. The use of a bytes.Buffer in this code is rather unnecessary given we know the size of everything, and given that the size of strkeys are small encouraging their data onto the stack is fine. It is worth noting that there is a functional impact by this change. Attempts to strkey.Encode a data value greater than the max payload size as defined by SEP-23 will now error. We've been wanting to add length checks (#1769) for sometime, so I think this is fine, and we can continue to improve those length checks in the future. Before: BenchmarkDecode_accountID-8 2576450 414.2 ns/op 130 B/op 3 allocs/op BenchmarkEncode_accountID-8 3657649 319.6 ns/op 244 B/op 6 allocs/op After: BenchmarkDecode_accountID-8 3563737 334.9 ns/op 128 B/op 2 allocs/op BenchmarkEncode_accountID-8 7365306 165.4 ns/op 64 B/op 1 allocs/op Co-authored-by: Alfonso Acosta <2362916+2opremio@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
83 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package strkey_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stellar/go/strkey" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkDecode_accountID(b *testing.B) { | ||
accountID, err := strkey.Encode(strkey.VersionByteAccountID, make([]byte, 32)) | ||
require.NoError(b, err) | ||
for i := 0; i < b.N; i++ { | ||
_, _ = strkey.Decode(strkey.VersionByteAccountID, accountID) | ||
} | ||
} | ||
|
||
func BenchmarkEncode_accountID(b *testing.B) { | ||
accountID := make([]byte, 32) | ||
for i := 0; i < b.N; i++ { | ||
_, _ = strkey.Encode(strkey.VersionByteAccountID, accountID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters