Skip to content

refactor: replace []byte(fmt.Sprintf) with fmt.Appendf#1703

Merged
MicBun merged 1 commit into
trufnetwork:mainfrom
box4wangjing:main
May 13, 2026
Merged

refactor: replace []byte(fmt.Sprintf) with fmt.Appendf#1703
MicBun merged 1 commit into
trufnetwork:mainfrom
box4wangjing:main

Conversation

@box4wangjing
Copy link
Copy Markdown
Contributor

@box4wangjing box4wangjing commented May 11, 2026

Description

I modified the return statement to utilize fmt.Appendf instead of fmt.Sprintf for creating the formatted byte slice.

This change enhances performance by directly appending the formatted string to the byte slice, avoiding the need for intermediate memory allocation created by fmt.Sprintf.

The resulting code is cleaner and more efficient, maintaining the same functionality while improving resource usage.

More info can see golang/go#47579

Related Issue

Motivation and Context

How Has This Been Tested?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update (including inline docs)
  • Other (please describe):

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Checklist Explanation:

How to Review this PR:

Additional Information:

Summary by CodeRabbit

  • Refactor
    • Internal code optimizations applied across multiple modules to improve performance and reduce memory allocations.

Review Change Stack

Signed-off-by: box4wangjing <box4wangjing@outlook.com>
@holdex
Copy link
Copy Markdown

holdex Bot commented May 11, 2026

Time Submission Status

Member Status Time Action Last Update
@box4wangjing ❌ Missing - ⚠️ Submit time -
MicBun ✅ Submitted 5min Update time May 13, 2026, 1:46 PM

You can submit time with the command. Example:

@holdex pr submit-time 15m

See available commands to help comply with our Guidelines.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 11, 2026

📝 Walkthrough

Walkthrough

This PR replaces fmt.Sprintf() followed by []byte() conversion with fmt.Appendf(nil, ...) across five functions in core implementations and test fixtures. The pattern optimizes string formatting by building bytes directly on the stack instead of allocating heap memory for intermediate strings.

Changes

String Formatting Optimization

Layer / File(s) Summary
Private Key Marshaling
app/key/cmd.go
PrivateKeyInfo.MarshalText() refactored in both Address != "" and Address == "" branches to use fmt.Appendf(nil, ...) instead of []byte(fmt.Sprintf(...)); output remains identical.
Message and Key Formatting
app/shared/display/message.go, node/snapshotter/protocol.go
RespResolutionBroadcast.MarshalText() and SnapshotMetadata.Key() updated to build output via fmt.Appendf(nil, ...) for efficient byte sequence construction.
Test Helpers
app/shared/display/format_test.go, node/store/store_test.go
Test fixture methods demoFormat.MarshalText and testLargeBlockStore appHash generation switched to fmt.Appendf(nil, ...) pattern.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • MicBun

Poem

🐰 A rabbit hops through sprintf lands,
Where strings once danced in wasteful strands,
Now appendf builds on the stack so neat,
No heap allocs—the refactor's sweet!
Bytes stack-direct, the pattern's clean,
Five functions fixed, a tidy scene! 🎯

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main refactoring change across all modified files: replacing fmt.Sprintf with fmt.Appendf for better performance.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Collaborator

@MicBun MicBun left a comment

Choose a reason for hiding this comment

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

nice catch, lgtm

@MicBun
Copy link
Copy Markdown
Collaborator

MicBun commented May 13, 2026

@holdex pr submit-time 5m

@MicBun MicBun merged commit d917218 into trufnetwork:main May 13, 2026
4 of 5 checks passed
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.

2 participants