Skip to content

Inefficient codegen when extracting field from temporary object #23395

Open
@arnetheduck

Description

@arnetheduck

Description

The following snippet, of critical performance importance when working with sum types, generates an inefficient copy, even though it can be seen that lifetimes are bounded in such a way that the copy can become a move:

type X = object
  s*: seq[byte]
proc f(): X = X(s: newSeq[byte](1024*1024*1024))
let v = f().s
	nimZeroMem((void*)(&T1_), sizeof(tyObject_X__4CKW0EgTl4SNofakrtBaFw));	_ZN6testit1fE((&T1_));
	genericSeqAssign((&v__testit_u35), T1_.s, (&NTIseqLbyteT__6H5Oh5UUvVCLiakt9aTwtUQ_));}

Notably, the same applies to any "last use" of a variable:

let 
  fv = f()
  v = fv.s # no need to copy if this is the last use of fv

Of course, in the real world, this applies most significantly to Option and its ilk:

proc f(): Option[seq[byte]] = ...
let
  x = f()
if x.isNone():
  return
let y = x.get() # last use of x - we can move value out of x
...

Nim Version

1.6, 2.0, devel....

Current Output

No response

Expected Output

No response

Possible Solution

No response

Additional Information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions