a more effective implementation of Pane::LayoutSizeNode::operator= #16164

Closed
opened 2026-01-31 04:59:27 +00:00 by claunia · 4 comments
Owner

Originally created by @ltimaginea on GitHub (Dec 16, 2021).

fb597ed304/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp (L35-L46)

Actually, we don't need Pane::LayoutSizeNode::_AssignChildNode to help define Pane::LayoutSizeNode::operator= . It makes things more complicated. Like LayoutSizeNode's copy constructor, LayoutSizeNode's copy assignment operator can be defined more effectively as follows:

Pane::LayoutSizeNode& Pane::LayoutSizeNode::operator=(const LayoutSizeNode& other)
{
	size = other.size;
	isMinimumSize = other.isMinimumSize;

	firstChild = other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr;
	secondChild = other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr;
	nextFirstChild = other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr;
	nextSecondChild = other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr;

	return *this;
}
Originally created by @ltimaginea on GitHub (Dec 16, 2021). https://github.com/microsoft/terminal/blob/fb597ed304ec6eef245405c9652e9b8a029b821f/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp#L35-L46 Actually, we don't need `Pane::LayoutSizeNode::_AssignChildNode` to help define `Pane::LayoutSizeNode::operator=` . It makes things more complicated. Like LayoutSizeNode's copy constructor, LayoutSizeNode's copy assignment operator can be defined more effectively as follows: ```cpp Pane::LayoutSizeNode& Pane::LayoutSizeNode::operator=(const LayoutSizeNode& other) { size = other.size; isMinimumSize = other.isMinimumSize; firstChild = other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr; secondChild = other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr; nextFirstChild = other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr; nextSecondChild = other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr; return *this; } ```
claunia added the Help WantedIssue-TaskIn-PRProduct-TerminalArea-CodeHealth labels 2026-01-31 04:59:27 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Jan 3, 2022):

So, if we did this, would we be able to just entirely get rid of _AssignChildNode (and then not even need to do #11963)?

@zadjii-msft commented on GitHub (Jan 3, 2022): So, if we did this, would we be able to just entirely get rid of `_AssignChildNode` (and then not even need to do #11963)?
Author
Owner

@ltimaginea commented on GitHub (Jan 4, 2022):

So, if we did this, would we be able to just entirely get rid of _AssignChildNode (and then not even need to do #11963)?

@zadjii-msft Yes!

fb597ed304/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp (L71)
_AssignChildNode is redundant and error-prone. Currently, at _AssignChildNode Line71, the memory leak has been occured because nodeField.release(); doesn't destroy the managed object and we lose the pointer to the managed object. The correct way is nodeField.reset(); rather than nodeField.release(); . See unique_ptr::release - (cplusplus.com) .

A more effective implementation of Pane::LayoutSizeNode::operator= is what I wrote above. If we did this, we can delete the _AssignChildNode code block.

There is a similar technique to define a class copy assignment operator in Google tensorflow source code:
3d30ef6f49/tensorflow/compiler/xla/service/spmd/spmd_partitioner_util.cc (L731-L732)

@ltimaginea commented on GitHub (Jan 4, 2022): > So, if we did this, would we be able to just entirely get rid of `_AssignChildNode` (and then not even need to do #11963)? @zadjii-msft Yes! https://github.com/microsoft/terminal/blob/fb597ed304ec6eef245405c9652e9b8a029b821f/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp#L71 `_AssignChildNode` is redundant and error-prone. Currently, at `_AssignChildNode` Line71, the memory leak has been occured because `nodeField.release();` doesn't destroy the managed object and we lose the pointer to the managed object. The correct way is `nodeField.reset();` rather than `nodeField.release();` . See [unique_ptr::release - (cplusplus.com)](http://www.cplusplus.com/reference/memory/unique_ptr/release/) . A more effective implementation of `Pane::LayoutSizeNode::operator=` is what I wrote above. If we did this, we can delete the `_AssignChildNode` code block. There is a similar technique to define a class copy assignment operator in Google tensorflow source code: https://github.com/tensorflow/tensorflow/blob/3d30ef6f49285b9e08a7b3492a902c67678a94d7/tensorflow/compiler/xla/service/spmd/spmd_partitioner_util.cc#L731-L732
Author
Owner

@ltimaginea commented on GitHub (Feb 23, 2022):

@zadjii-msft @DHowett
Any update on this?

@ltimaginea commented on GitHub (Feb 23, 2022): @zadjii-msft @DHowett Any update on this?
Author
Owner

@zadjii-msft commented on GitHub (Feb 24, 2022):

Sorry, been quite distracted the last few weeks. If you'd like to open a PR, feel free to! We'd love to take a look.

@zadjii-msft commented on GitHub (Feb 24, 2022): Sorry, been quite distracted the last few weeks. If you'd like to open a PR, feel free to! We'd love to take a look.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#16164