In-place construction ===================== For example: a = get_a ... # if `a` is always the last reference, then this could # allocate the result "over" the previous allocation. # (note that this is tricky to do if the function also takes # the return value as a parameter, but in some cases it can be # done anyway). a = get_a ABI --- How should this work on the ABI level? Maybe add a new parameter? From this (source): func get_a returns Thing ret code ... end To this (ABI pseudo code): func get_a ?Thing placement returns Thing ret code ... end And with the edge case where the function takes the return value as a parameter: func get_a aliased ?Thing placement aliased Thing existing return Thing ret code if placement == existing # implies placement<>none placement.a += 10 elif placement <> none placement.a = existing.a + 10 else ret = new ret.a = existing.a + 10 return ret end end Opaque and non-opaque types --------------------------- This always works for non-opaque types. Those can even be allocated on the stack (if small enough that there's no risk for eventual stack overflow). For opaque types, it only works if there's an object that can be recycled. Usages of in-place construction ------------------------------- If objects are allowed to have variable length, then in-place construction could be used for sub-arena allocation into an embedded slot type: TreeNode: typeparam T ?TreeNode T left = none ?TreeNode T right = none embed T data constructor embed T data code end # TODO nicer getters? func get_data returns T data code return data end # maybe this: func get_data = data TreeExample: func f code TreeNode Thing tree = new new end Implementation idea: * Have an internal function TreeNode__prealloc * Take a allocation placement parameter in constructors * Optionally take a size parameter (necessary for e.g. embedded arrays), - This would prevent variable-sized data structures though. Pseudo-code: TreeNode tree = TreeNode__prealloc() Thing_new(tree.data) TreeNode_new(tree)