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

Removes unnecessary Accounts constructors #34471

Merged
merged 4 commits into from
Dec 15, 2023

Conversation

brooksprumo
Copy link
Contributor

@brooksprumo brooksprumo commented Dec 14, 2023

Problem

There are so many Accounts and AccountsDb constructors... Lots of overlap/duplication.

Now that #34466 has merged, we can more easily see how almost all of the Accounts constructors are unnecessary. Instead, construct AccountsDb and pass that into the one remaining Accounts constructor1.

Summary of Changes

  • Removes new_with_config()
  • Removes default_for_tests()
  • Removes new_with_config_for_benches()
  • Removes new_with_config_for_tests()

Notes

Reviewing this PR will likely be easier by going commit-by-commit.

Each commit basically removes one constructor for Accounts, and updates all its old callers.

Footnotes

  1. Some fancy software peoples may call this Dependency Injection (TM)

@brooksprumo brooksprumo self-assigned this Dec 14, 2023
@brooksprumo brooksprumo changed the title Removes unused Accounts constructors Removes unnecessary Accounts constructors Dec 14, 2023
@brooksprumo brooksprumo marked this pull request as ready for review December 14, 2023 22:14
@brooksprumo brooksprumo requested a review from steviez December 14, 2023 22:15
Copy link

codecov bot commented Dec 14, 2023

Codecov Report

Merging #34471 (e7f48d0) into master (aaccbdd) will increase coverage by 0.0%.
The diff coverage is 87.5%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #34471   +/-   ##
=======================================
  Coverage    81.8%    81.8%           
=======================================
  Files         819      819           
  Lines      220894   220907   +13     
=======================================
+ Hits       180753   180820   +67     
+ Misses      40141    40087   -54     

vec![path],
&ClusterType::Testnet,
AccountSecondaryIndexes::default(),
AccountShrinkThreshold::default(),
);
let accounts = Accounts::new(Arc::new(accounts_db));
Copy link
Contributor

Choose a reason for hiding this comment

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

Almost all of the cases you've touched look something like this one:

let accounts = Accounts::new(Arc::new(accounts_db))

Wondering if we should make Accounts::new() take an AccountsDb instead of Arc<AccountsDb>, and then we can shift the repeated Arc::new() inside Accounts::new()?

Also, @apfitzge had a clever suggestion on one of my PR's the other day that might be applicable here if we want to accept Arc<AccountsDb> or AccountsDb: #34449 (comment)

Copy link
Contributor Author

@brooksprumo brooksprumo Dec 15, 2023

Choose a reason for hiding this comment

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

Wondering if we should make Accounts::new() take an AccountsDb instead of Arc<AccountsDb>, and then we can shift the repeated Arc::new() inside Accounts::new()?

Yeah, I'd prefer if Accounts::new() took AccountsDb instead of Arc<AccountsDb>. But, Bank::_new_from_parent() clones the AccountsDb into the new bank's Accounts, so there must be a ctor that takes an Arc<AccountsDb>.

accounts: Arc::new(Accounts::new(accounts_db)),

We could keep a second ctor around, but I'm really trying to reduce ctors where possible.

Also, @apfitzge had a clever suggestion on one of my PR's the other day that might be applicable here if we want to accept Arc<AccountsDb> or AccountsDb: #34449 (comment)

Clever, indeed! Since Arc already has implements From<T> for Arc<T>, this allows the caller to use .into() already. This still requires the caller to do something special. I understand the Into<Arc<AccountsDb>> as a function parameter would remove the burden from the caller, which may be nice. I'm on the fence. Want me to do the Into way? I'll do basically anything to avoid adding more constructors heh.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll do basically anything to avoid adding more constructors heh.

😆 😆 😆

I'm a little on the fence as well ha and don't have too strong of an option; I just saw the repeated code so figured I'd mention it.

That being said, I think this PR is still valuable as-is; Arc::new() is pretty simple and having some of these repeated is probably better than someone having to choose from a handful of constructors. So, I think we could defer a decision to another PR and ship this one as-is

Copy link
Contributor Author

@brooksprumo brooksprumo Dec 15, 2023

Choose a reason for hiding this comment

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

I think I've come around to the Into<> idea. Here's a PR for it: #34476.

(I'm going to merge this PR as-is, since it feels like a complete thought, and the Into<> one is a different thought.)

@brooksprumo brooksprumo requested a review from steviez December 15, 2023 03:33
@brooksprumo brooksprumo merged commit 45eaa4c into solana-labs:master Dec 15, 2023
34 checks passed
@brooksprumo brooksprumo deleted the accounts/ctor-refactor2 branch December 15, 2023 03:50
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