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

Pass (large) strings by value and move if they are to be consumed #6216

Closed
chriseth opened this issue Mar 7, 2019 · 12 comments
Closed

Pass (large) strings by value and move if they are to be consumed #6216

chriseth opened this issue Mar 7, 2019 · 12 comments

Comments

@chriseth
Copy link
Contributor

chriseth commented Mar 7, 2019

Especially when setting the source input of the compiler, these strings are passed by const reference. This forces a copy which would actually be unnecessary. The recommended way of passing data that is consumed/stored by the function/method is to pass by value and move into the member variable.

There are several instances where solidity sources stored in strings are copied instead of moved like that, for example in CompilerStack::addSource. This should be fixed.

@maxrobot
Copy link

Hi, quick question for further clarification...

The function you mention it is defined as such:

CompilerStack::addSource(string const& _name, ...)

Variable _name is passed as a constant reference, which you say forces an unnecessary copy.

It is my understanding that in cpp passing by constant reference does not cause a copy.

Citing IBM:
Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments.

So my question is where does the unnecessary copy occur?

@chriseth
Copy link
Contributor Author

You are right that passing by const& does not cause a copy when calling the function, but if the function consumes the argument, it has to copy it somewhere, and this is where the copy occurs. If you pass by value instead, it can result in a copy and a move in the worst case, but it two moves in the good case, and this is what we aim for. The article you cite is probably outdated.

You might be able to find more information here: https://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference

@maxrobot
Copy link

Thank you very much!

@gitcoinbot
Copy link

gitcoinbot commented Apr 3, 2019

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work has been started.

These users each claimed they can complete the work by 4 days, 19 hours ago.
Please review their action plans below:

1) g-r-a-n-t has been approved to start work.

Looks pretty straight forward.

AP:

  • modify any functions taking in the source such that it passes by value
  • briefly check if this is happening anywhere else

Learn more on the Gitcoin Issue Details page.

@gitcoinbot
Copy link

@g-r-a-n-t Hello from Gitcoin Core - are you still working on this issue? Please submit a WIP PR or comment back within the next 3 days or you will be removed from this ticket and it will be returned to an ‘Open’ status. Please let us know if you have questions!

  • reminder (3 days)
  • escalation to mods (6 days)

Funders only: Snooze warnings for 1 day | 3 days | 5 days | 10 days | 100 days

@gitcoinbot
Copy link

@g-r-a-n-t Hello from Gitcoin Core - are you still working on this issue? Please submit a WIP PR or comment back within the next 3 days or you will be removed from this ticket and it will be returned to an ‘Open’ status. Please let us know if you have questions!

  • reminder (3 days)
  • escalation to mods (6 days)

Funders only: Snooze warnings for 1 day | 3 days | 5 days | 10 days | 100 days

@g-r-a-n-t
Copy link
Member

Hello @gitcoinbot, this is currently under review.

@gitcoinbot
Copy link

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work for 200.0 DAI (200.0 USD @ $1.0/DAI) has been submitted by:

  1. @g-r-a-n-t

@vs77bb please take a look at the submitted work:


@g-r-a-n-t
Copy link
Member

@vs77bb Could you please take a look at this work?

@gitcoinbot
Copy link

Gitcoin Tree ⚡️ A *Gitcoin Tree* Kudos has been sent to @g-r-a-n-t for this issue from @vs77bb. ⚡️

The sender had the following public comments:

Thanks for your patience, and great work!

Nice work @g-r-a-n-t!
Your Kudos has automatically been sent in the ETH address we have on file.

@gitcoinbot
Copy link

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


The funding of 200.0 DAI (200.0 USD @ $1.0/DAI) attached to this issue has been approved & issued to @g-r-a-n-t.

@chriseth
Copy link
Contributor Author

Solved by #6490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants