Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Inefficient codegen for field return

Open arnetheduck opened this issue 1 year ago • 2 comments

Description

type X = object
  v: string

proc fff(input: string): X =
  X(v: input)

proc ggg(input: string): X =
  fff(input)

proc hhh(input: string): string =
  ggg(input).v # inefficient copy

proc hhh2(input: string): string =
  let v = ggg(input)
  v.v

hhh and hhh2 are semantically equivalent and yet in hhh the string is copied - this introduces significant inefficiency for sum type-like constructs.

N_LIB_PRIVATE N_NIMCALL(NimStringV2, _ZN6testit3hhhE6string)(NimStringV2 input_p0) {
	NimStringV2 result;
	tyObject_X__DSqG4yj45rrZSFUFxlmNRw colontmpD_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result.len = 0; result.p = NIM_NIL;
	nimZeroMem((void*)(&colontmpD_), sizeof(tyObject_X__DSqG4yj45rrZSFUFxlmNRw));
	colontmpD_ = _ZN6testit3gggE6string(input_p0);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	_ZN6system7eqcopy_E3varI6stringE6string((&result), colontmpD_.v);
	_ZN6testit10eqdestroy_E3varIN6testit1XEE((&colontmpD_));
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NimStringV2, _ZN6testit4hhh2E6string)(NimStringV2 input_p0) {
	NimStringV2 result;
	tyObject_X__DSqG4yj45rrZSFUFxlmNRw v;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result.len = 0; result.p = NIM_NIL;
	nimZeroMem((void*)(&v), sizeof(tyObject_X__DSqG4yj45rrZSFUFxlmNRw));
	v = _ZN6testit3gggE6string(input_p0);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	result = v.v;
	_ZN6system11eqwasMoved_E3varI6stringE((&v.v));
	_ZN6testit10eqdestroy_E3varIN6testit1XEE((&v));
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NimStringV2, _ZN6testit4hhh3E6string)(NimStringV2 input_p0) {
	NimStringV2 result;
	tyObject_X__DSqG4yj45rrZSFUFxlmNRw v;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result.len = 0; result.p = NIM_NIL;
	nimZeroMem((void*)(&v), sizeof(tyObject_X__DSqG4yj45rrZSFUFxlmNRw));
	v = _ZN6testit3gggE6string(input_p0);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	result = v.v;
	_ZN6system11eqwasMoved_E3varI6stringE((&v.v));
	_ZN6testit10eqdestroy_E3varIN6testit1XEE((&v));
	}BeforeRet_: ;
	return result;

Nim Version

2.0, devel

Current Output

No response

Expected Output

No response

Possible Solution

No response

Additional Information

No response

arnetheduck avatar Jun 19 '24 19:06 arnetheduck

Related to https://github.com/nim-lang/Nim/issues/23395

ringabout avatar Jun 21 '24 13:06 ringabout

Related to https://github.com/nim-lang/Nim/issues/23395

yeah that one is for refc (which would be the priority to fix as far as we're concerned) - this one is for orc.

arnetheduck avatar Jun 24 '24 20:06 arnetheduck