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

fixes #14126 [backport:1.2] #14390

Merged
merged 2 commits into from
May 19, 2020
Merged

fixes #14126 [backport:1.2] #14390

merged 2 commits into from
May 19, 2020

Conversation

Araq
Copy link
Member

@Araq Araq commented May 18, 2020

No description provided.

@timotheecour
Copy link
Member

timotheecour commented May 18, 2020

although safer, can that cause performance regressions? (refs: #14126 (comment))

@Araq
Copy link
Member Author

Araq commented May 18, 2020

We can do better and only pessimize inside a try statement.

@timotheecour
Copy link
Member

timotheecour commented May 18, 2020

that would just be a rather weird one-off special case and wouldn't help when popping up the try/catch handler 1 level:

type X = object
  a, c: string

proc f(): X =
  result.a = "a"
  raise (ref ValueError)()

proc main(x: var X) =
  x.a = "1"
  x.c = "3"
  x = f()

var x: X
try:
  main(x)
except:
  echo "caught"
echo x
doAssert x.c == "3", "this assert will fail"

even if we were to track exceptions to only disable nrvo when inside some try/catch ancestor, given that try/catch can happen very far from where exception is thrown (eg at application root, say in some main loop), it'd imply that every single x = fun() would have NRVO disabled.

IMO that's potentially a performance killer. I'm honestly not convinced #14126 is a real problem to begin with, once you acknowledge existence of NRVO.

We could instead embrace current behavior as spec, have a nice speed benefit over equivalent C++ code, document it well, and provide a good opt-in option for code that must rely on NRVO-safe behavior; eg:

x = f()
=>
let xAux = f()
x = xAux

@Clyybber
Copy link
Contributor

Clyybber commented May 18, 2020

@timotheecour Simple location analysis will do. Only disable the RVO for raising procs when we are in a try, or the result is assigned to a global, upvalue or var parameter.

@Araq Araq merged commit 16003bf into devel May 19, 2020
@Araq Araq deleted the araq-fixes-14126 branch May 19, 2020 22:42
narimiran pushed a commit that referenced this pull request May 20, 2020
* fixes #14126 [backport:1.2]

* used more logic to optimize it further; updated Nimble version

(cherry picked from commit 16003bf)
@timotheecour timotheecour mentioned this pull request May 22, 2020
1 task
EchoPouet pushed a commit to EchoPouet/Nim that referenced this pull request Jun 13, 2020
* fixes nim-lang#14126 [backport:1.2]

* used more logic to optimize it further; updated Nimble version
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