Skip to content

Conversation

chlins
Copy link
Member

@chlins chlins commented Apr 17, 2025

This pull request introduces several code simplifications and performance enhancements by leveraging Go's built-in utility packages (maps, slices, etc.) and improving iteration logic. The changes primarily focus on replacing manual implementations with standard library functions and refactoring iteration patterns for better readability and maintainability.

Code Simplifications Using Standard Libraries:

  • Replaced manual map copying with maps.Copy in multiple locations, such as peertask_reuse.go and peertask_stream.go, to simplify the code. [1] [2] [3] [4]
  • Replaced slice element removal logic with slices.Delete in storage_manager.go and slices.go. [1] [2]
  • Simplified status code checks by using slices.Contains in http_source_client.go and source_client.go. [1] [2]

Iteration Refactoring:

  • Refactored for loops to use for range where applicable, improving readability and reducing off-by-one errors. This change affects various test files (peertask_manager_test.go, peer_exchange_test.go, etc.) and production code. [1] [2] [3] [4]

Utility Enhancements:

  • Introduced min function calls to replace manual comparisons for calculating minimum values, improving code clarity in files like peertask_stream_resume_test.go and piece_manager.go. [1] [2] [3]

Imports Optimization:

  • Added imports for maps and slices in relevant files to support the new utility functions. [1] [2] [3]

These changes collectively improve the maintainability, readability, and performance of the codebase by leveraging modern Go language features and best practices.

Description

Related Issue

Motivation and Context

Screenshots (if appropriate)

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 (if none of the other choices apply)

Checklist

  • 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.

@chlins chlins requested a review from a team as a code owner April 17, 2025 04:16
@chlins chlins enabled auto-merge (squash) April 17, 2025 04:17
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

Attention: Patch coverage is 44.44444% with 10 lines in your changes missing coverage. Please review.

Project coverage is 34.42%. Comparing base (ea52a84) to head (6fa606c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
client/daemon/peer/peertask_stream.go 0.00% 3 Missing ⚠️
client/daemon/storage/storage_manager.go 0.00% 3 Missing ⚠️
pkg/source/source_client.go 0.00% 2 Missing ⚠️
client/daemon/peer/piece_manager.go 50.00% 1 Missing ⚠️
client/daemon/rpcserver/rpcserver.go 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3963      +/-   ##
==========================================
- Coverage   34.46%   34.42%   -0.04%     
==========================================
  Files         342      342              
  Lines       40091    40076      -15     
==========================================
- Hits        13817    13796      -21     
- Misses      25369    25372       +3     
- Partials      905      908       +3     
Flag Coverage Δ
unittests 34.42% <44.44%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
client/daemon/peer/peertask_reuse.go 63.91% <100.00%> (-0.12%) ⬇️
pkg/retry/retry.go 88.88% <100.00%> (ø)
pkg/slices/slices.go 100.00% <100.00%> (ø)
.../source/clients/httpprotocol/http_source_client.go 58.72% <100.00%> (-0.94%) ⬇️
pkg/strings/strings.go 100.00% <100.00%> (ø)
scheduler/scheduling/evaluator/evaluator_base.go 83.67% <100.00%> (-0.17%) ⬇️
client/daemon/peer/piece_manager.go 48.90% <50.00%> (ø)
client/daemon/rpcserver/rpcserver.go 47.85% <0.00%> (ø)
pkg/source/source_client.go 9.83% <0.00%> (+0.05%) ⬆️
client/daemon/peer/peertask_stream.go 61.18% <0.00%> (+0.56%) ⬆️
... and 1 more

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@Liam-Zhao Liam-Zhao left a comment

Choose a reason for hiding this comment

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

LGTM

…ard library

Signed-off-by: chlins <chlins.zhang@gmail.com>
@chlins chlins merged commit d0bfdd4 into main Apr 17, 2025
27 checks passed
@chlins chlins deleted the refactor/modern branch April 17, 2025 07:48
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.

3 participants