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

chore: enable clippy::trivially_copy_pass_by_ref lint and address it #6724

Conversation

SanchithHegde
Copy link
Member

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

This PR enables and addresses the trivially_copy_pass_by_ref clippy lint. The lint helps identify places in code where types which implement the Copy trait are passed by reference, while it would be cheaper to pass them by value instead. In our case, most of these are enums, which are only 1 or 2 bytes. A similar reasoning applies to Option<T> parameters where T: Copy.

Note that the lint only identifies function parameters, but not fields in structs. For instance, some of our Kafka-related types, say KafkaRefund, still have fields that hold references to bools and enums which implement Copy.

Motivation and Context

The lint helps identify places in code where types which implement the Copy trait are passed by reference, while it would be cheaper to pass them by value instead.

How did you test it?

I'd expect this PR to have no behavior changes, all our existing CI checks should pass, as before.

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@SanchithHegde SanchithHegde added S-waiting-on-review Status: This PR has been implemented and needs to be reviewed C-refactor Category: Refactor labels Dec 2, 2024
@SanchithHegde SanchithHegde added this to the December 2024 Release milestone Dec 2, 2024
@SanchithHegde SanchithHegde self-assigned this Dec 2, 2024
@SanchithHegde SanchithHegde requested review from a team as code owners December 2, 2024 19:16
Copy link

semanticdiff-com bot commented Dec 2, 2024

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router_env/src/logger/formatter.rs  99% smaller
  crates/router/src/core/payment_methods/vault.rs  98% smaller
  crates/router/src/services/authorization/permissions.rs  94% smaller
  crates/router/src/core/utils.rs  85% smaller
  crates/router/src/connector/paybox/transformers.rs  84% smaller
  crates/router/src/services/authorization/permission_groups.rs  78% smaller
  crates/analytics/src/query.rs  68% smaller
  crates/hyperswitch_connectors/src/connectors/powertranz/transformers.rs  51% smaller
  crates/router/src/core/payment_link.rs  1% smaller
  crates/router/src/core/payments.rs  1% smaller
  Cargo.toml Unsupported file format
  crates/analytics/src/api_event/core.rs  0% smaller
  crates/analytics/src/api_event/metrics.rs  0% smaller
  crates/analytics/src/api_event/metrics/api_count.rs  0% smaller
  crates/analytics/src/api_event/metrics/latency.rs  0% smaller
  crates/analytics/src/api_event/metrics/status_code_count.rs  0% smaller
  crates/analytics/src/auth_events/core.rs  0% smaller
  crates/analytics/src/auth_events/metrics.rs  0% smaller
  crates/analytics/src/auth_events/metrics/authentication_attempt_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/authentication_success_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/challenge_attempt_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/challenge_flow_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/challenge_success_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/frictionless_flow_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/frictionless_success_count.rs  0% smaller
  crates/analytics/src/auth_events/metrics/three_ds_sdk_count.rs  0% smaller
  crates/analytics/src/disputes/core.rs  0% smaller
  crates/analytics/src/disputes/metrics.rs  0% smaller
  crates/analytics/src/disputes/metrics/dispute_status_metric.rs  0% smaller
  crates/analytics/src/disputes/metrics/sessionized_metrics/dispute_status_metric.rs  0% smaller
  crates/analytics/src/disputes/metrics/sessionized_metrics/total_amount_disputed.rs  0% smaller
  crates/analytics/src/disputes/metrics/sessionized_metrics/total_dispute_lost_amount.rs  0% smaller
  crates/analytics/src/disputes/metrics/total_amount_disputed.rs  0% smaller
  crates/analytics/src/disputes/metrics/total_dispute_lost_amount.rs  0% smaller
  crates/analytics/src/frm/core.rs  0% smaller
  crates/analytics/src/frm/metrics.rs  0% smaller
  crates/analytics/src/frm/metrics/frm_blocked_rate.rs  0% smaller
  crates/analytics/src/frm/metrics/frm_triggered_attempts.rs  0% smaller
  crates/analytics/src/lib.rs  0% smaller
  crates/analytics/src/opensearch.rs  0% smaller
  crates/analytics/src/payment_intents/core.rs  0% smaller
  crates/analytics/src/payment_intents/metrics.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/payment_intent_count.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/payment_processed_amount.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/payments_success_rate.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/payment_intent_count.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/payment_processed_amount.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/payments_distribution.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/payments_success_rate.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/smart_retried_amount.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/successful_smart_retries.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/sessionized_metrics/total_smart_retries.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/smart_retried_amount.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/successful_smart_retries.rs  0% smaller
  crates/analytics/src/payment_intents/metrics/total_smart_retries.rs  0% smaller
  crates/analytics/src/payments/core.rs  0% smaller
  crates/analytics/src/payments/distribution.rs  0% smaller
  crates/analytics/src/payments/distribution/payment_error_message.rs  0% smaller
  crates/analytics/src/payments/metrics.rs  0% smaller
  crates/analytics/src/payments/metrics/avg_ticket_size.rs  0% smaller
  crates/analytics/src/payments/metrics/connector_success_rate.rs  0% smaller
  crates/analytics/src/payments/metrics/payment_count.rs  0% smaller
  crates/analytics/src/payments/metrics/payment_processed_amount.rs  0% smaller
  crates/analytics/src/payments/metrics/payment_success_count.rs  0% smaller
  crates/analytics/src/payments/metrics/retries_count.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/avg_ticket_size.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/connector_success_rate.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/failure_reasons.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/payment_count.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/payment_processed_amount.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/payment_success_count.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/payments_distribution.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/retries_count.rs  0% smaller
  crates/analytics/src/payments/metrics/sessionized_metrics/success_rate.rs  0% smaller
  crates/analytics/src/payments/metrics/success_rate.rs  0% smaller
  crates/analytics/src/refunds/core.rs  0% smaller
  crates/analytics/src/refunds/metrics.rs  0% smaller
  crates/analytics/src/refunds/metrics/refund_count.rs  0% smaller
  crates/analytics/src/refunds/metrics/refund_processed_amount.rs  0% smaller
  crates/analytics/src/refunds/metrics/refund_success_count.rs  0% smaller
  crates/analytics/src/refunds/metrics/refund_success_rate.rs  0% smaller
  crates/analytics/src/refunds/metrics/sessionized_metrics/refund_count.rs  0% smaller
  crates/analytics/src/refunds/metrics/sessionized_metrics/refund_error_message.rs  0% smaller
  crates/analytics/src/refunds/metrics/sessionized_metrics/refund_processed_amount.rs  0% smaller
  crates/analytics/src/refunds/metrics/sessionized_metrics/refund_reason.rs  0% smaller
  crates/analytics/src/refunds/metrics/sessionized_metrics/refund_success_count.rs  0% smaller
  crates/analytics/src/refunds/metrics/sessionized_metrics/refund_success_rate.rs  0% smaller
  crates/analytics/src/sdk_events/core.rs  0% smaller
  crates/analytics/src/sdk_events/metrics.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/average_payment_time.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/load_time.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/payment_attempts.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/payment_data_filled_count.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/payment_method_selected_count.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/payment_methods_call_count.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/sdk_initiated_count.rs  0% smaller
  crates/analytics/src/sdk_events/metrics/sdk_rendered_count.rs  0% smaller
  crates/api_models/src/connector_enums.rs  0% smaller
  crates/common_enums/src/enums.rs  0% smaller
  crates/common_enums/src/transformers.rs  0% smaller
  crates/common_utils/src/id_type.rs  0% smaller
  crates/common_utils/src/id_type/global_id.rs  0% smaller
  crates/common_utils/src/types.rs  0% smaller
  crates/hyperswitch_connectors/src/connectors/cashtocode.rs  0% smaller
  crates/hyperswitch_connectors/src/connectors/cashtocode/transformers.rs  0% smaller
  crates/hyperswitch_connectors/src/connectors/rapyd.rs  0% smaller
  crates/hyperswitch_connectors/src/connectors/worldline.rs  0% smaller
  crates/kgraph_utils/src/mca.rs  0% smaller
  crates/router/src/bin/router.rs  0% smaller
  crates/router/src/connector/adyen/transformers.rs  0% smaller
  crates/router/src/core/admin.rs  0% smaller
  crates/router/src/core/payment_methods.rs  0% smaller
  crates/router/src/core/payment_methods/cards.rs  0% smaller
  crates/router/src/core/payment_methods/utils.rs  0% smaller
  crates/router/src/core/payments/flows/session_flow.rs  0% smaller
  crates/router/src/core/payments/helpers.rs  0% smaller
  crates/router/src/core/payments/operations/payment_approve.rs  0% smaller
  crates/router/src/core/payments/operations/payment_cancel.rs  0% smaller
  crates/router/src/core/payments/operations/payment_complete_authorize.rs  0% smaller
  crates/router/src/core/payments/operations/payment_confirm.rs  0% smaller
  crates/router/src/core/payments/operations/payment_reject.rs  0% smaller
  crates/router/src/core/payments/operations/payment_session.rs  0% smaller
  crates/router/src/core/payments/operations/payment_start.rs  0% smaller
  crates/router/src/core/payments/operations/payment_status.rs  0% smaller
  crates/router/src/core/payments/operations/payment_update.rs  0% smaller
  crates/router/src/core/payments/operations/payments_incremental_authorization.rs  0% smaller
  crates/router/src/core/payments/operations/tax_calculation.rs  0% smaller
  crates/router/src/core/payments/transformers.rs  0% smaller
  crates/router/src/core/payout_link.rs  0% smaller
  crates/router/src/core/payouts.rs  0% smaller
  crates/router/src/core/payouts/helpers.rs  0% smaller
  crates/router/src/core/routing.rs  0% smaller
  crates/router/src/core/routing/helpers.rs  0% smaller
  crates/router/src/core/user.rs  0% smaller
  crates/router/src/core/user/dashboard_metadata.rs  0% smaller
  crates/router/src/routes/app.rs  0% smaller
  crates/router/src/routes/metrics/bg_metrics_collector.rs  0% smaller
  crates/router/src/routes/routing.rs  0% smaller
  crates/router/src/services/authentication.rs  0% smaller
  crates/router/src/services/authorization.rs  0% smaller
  crates/router/src/services/authorization/roles.rs  0% smaller
  crates/router/src/types/api.rs  0% smaller
  crates/router/src/utils/user.rs  0% smaller
  crates/router/src/utils/user/dashboard_metadata.rs  0% smaller
  crates/router/tests/connectors/payeezy.rs  0% smaller
  crates/router_derive/src/macros/to_encryptable.rs  0% smaller
  crates/router_env/src/logger/config.rs  0% smaller
  crates/scheduler/src/utils.rs  0% smaller
  crates/storage_impl/src/lib.rs  0% smaller
  crates/storage_impl/src/lookup.rs  0% smaller
  crates/storage_impl/src/payments/payment_attempt.rs  0% smaller
  crates/storage_impl/src/payments/payment_intent.rs  0% smaller
  crates/storage_impl/src/payouts/payout_attempt.rs  0% smaller
  crates/storage_impl/src/payouts/payouts.rs  0% smaller

srujanchikke
srujanchikke previously approved these changes Dec 3, 2024
ShankarSinghC
ShankarSinghC previously approved these changes Dec 3, 2024
@ShivanshMathurJuspay
Copy link
Contributor

Won't this cause memory spike ?

@SanchithHegde
Copy link
Member Author

Won't this cause memory spike ?

Nope, this would actually reduce the memory used, because most of our enums need typically 1 or 2 bytes, while references have the size of usize, which is 64 bits (8 bytes) on 64 bit targets (for amd64 / arm64).

Reference Value
Screenshot of function accepting Currency enum by reference Screenshot of function accepting Currency enum by value

ThisIsMani
ThisIsMani previously approved these changes Dec 3, 2024
Copy link
Contributor

@ThisIsMani ThisIsMani left a comment

Choose a reason for hiding this comment

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

Dashboard specific changes looks fine.

jarnura
jarnura previously approved these changes Dec 3, 2024
@likhinbopanna likhinbopanna added this pull request to the merge queue Dec 4, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 4, 2024
ThisIsMani
ThisIsMani previously approved these changes Dec 5, 2024
ShankarSinghC
ShankarSinghC previously approved these changes Dec 5, 2024
srujanchikke
srujanchikke previously approved these changes Dec 5, 2024
jarnura
jarnura previously approved these changes Dec 5, 2024
@pixincreate pixincreate enabled auto-merge December 5, 2024 10:05
@pixincreate pixincreate added this pull request to the merge queue Dec 5, 2024
@SanchithHegde SanchithHegde removed this pull request from the merge queue due to a manual request Dec 5, 2024
@SanchithHegde SanchithHegde added this pull request to the merge queue Dec 5, 2024
Merged via the queue into main with commit d17d2fe Dec 5, 2024
16 of 18 checks passed
@SanchithHegde SanchithHegde deleted the introduce-and-address-clippy-trivially-copy-pass-by-ref-lint branch December 5, 2024 15:09
@SanchithHegde SanchithHegde removed the S-waiting-on-review Status: This PR has been implemented and needs to be reviewed label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-refactor Category: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants