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

stream_base: use MallocedBuffer abstraction for buffers #23543

Closed
wants to merge 1 commit into from

Conversation

codyhazelwood
Copy link
Contributor

Drop Free and std::unique_ptr in favor of Node's MallocedBuffer
for char[] buffer memory mangement.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.
@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Oct 12, 2018
@targos targos added the code-and-learn Issues related to the Code-and-Learn events and PRs submitted during the events. label Oct 12, 2018
@addaleax addaleax added lib / src Issues and PRs related to general changes in the lib or src directory. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Oct 12, 2018
@addaleax
Copy link
Member

@Trott
Copy link
Member

Trott commented Oct 13, 2018

@Trott

This comment has been minimized.

@refack
Copy link
Contributor

refack commented Oct 13, 2018

refack
refack previously requested changes Oct 13, 2018
Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO in this case std::unique_ptr is a better abstraction then MallocedBuffer.
As I see it, since MallocedBuffer does not use a std::unique_ptr, it's best for RAII of memory that is taken from an outside API, and when size should be tracked as well.

What makes this distinction noticeable to me is the continued use of data_size instead of storage.size

@refack
Copy link
Contributor

refack commented Oct 13, 2018

Suggesting a possible tweak, something like:

Template<typename T>
struct Free {
  void operator()(unique_ptr<T, Deleter>::pointer ptr) const { free(ptr); }
};
Template<typename T>
std::unique_ptr<T, Free<T>> make_malloced_unique(size_t size) {
  return std::unique_ptr<T, Free<T>>(Malloc<T>(size));
}

@addaleax
Copy link
Member

@refack We have MallocedBuffer exactly for this use case – a buffer that’s allocated by malloc().

As I see it, since MallocedBuffer does not use a std::unique_ptr, it's best for RAII of memory that is taken from an outside API, and when size should be tracked as well.

Why would it be better? It’s just as good as std::unique_ptr to declare ownership. Where the memory comes from is irrelevant.

What makes this distinction noticeable to me is the continued use of data_size instead of storage.size

Yes, because they can be different. data_size is not necessarily the actual length of the malloc’ed memory.

@refack
Copy link
Contributor

refack commented Oct 13, 2018

@refack We have MallocedBuffer exactly for this use case – a buffer that’s allocated by malloc().

As I see it, since MallocedBuffer does not use a std::unique_ptr, it's best for RAII of memory that is taken from an outside API, and when size should be tracked as well.

Why would it be better? It’s just as good as std::unique_ptr to declare ownership. Where the memory comes from is irrelevant.

@addaleax:

  1. MallocedBuffer is a new utility and is node specific, so it's semantics are not clear to me, so I assume to others as well. It's maturity is unknown (Ref: src: fix bug in MallocedBuffer #23434)
  2. std::unique_ptr is free of runtime cost (except for the dtor), while MallocedBuffer is unknown at minimum carries the runtime cost of construction, and the memory cost of MallocedBuffer::size.

P.S. and probably most important to me - readability. std::unique_ptr standardized and well documented. MallocedBuffer is only documented by it's code

@refack refack removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 13, 2018
refack added a commit to refack/node that referenced this pull request Oct 13, 2018
@codyhazelwood
Copy link
Contributor Author

Not sure what changes I need to make here? If @refack’s review stands, then I should probably close the PR. However, as someone new to the project and not the most experienced C++ developer, I find the MallocedBuffer implementation to be much more readable, and I think I reduces the chances of someone like me introducing a memory leak.

@refack refack dismissed their stale review October 13, 2018 22:26

Dismissing in favor of status quo

@refack
Copy link
Contributor

refack commented Oct 13, 2018

Not sure what changes I need to make here? If @refack’s review stands, then I should probably close the PR. However, as someone new to the project and not the most experienced C++ developer, I find the MallocedBuffer implementation to be much more readable, and I think I reduces the chances of someone like me introducing a memory leak.

Hello @codyhazelwood and thank you for the contribution.
The discussion whether MallocedBuffer is the right abstraction for this case can continue in other PRs. So I dismissed my review.
As a side note std::unique_ptr does RAII, so there was no change in memory semantics in this case.

P.S. CI is as green as it's going to get for the time being.

@refack refack added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 13, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Oct 15, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: nodejs#23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Oct 15, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: nodejs#23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
@BridgeAR
Copy link
Member

Landed in 810c0d9 🎉

@codyhazelwood congratulations on your commit to Node.js!

@BridgeAR BridgeAR closed this Oct 15, 2018
jasnell pushed a commit that referenced this pull request Oct 17, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: #23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
addaleax pushed a commit that referenced this pull request Oct 20, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: #23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
refack added a commit to refack/node that referenced this pull request Oct 20, 2018
MylesBorins pushed a commit that referenced this pull request Oct 30, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: #23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
@codebytere codebytere mentioned this pull request Nov 27, 2018
rvagg pushed a commit that referenced this pull request Nov 28, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: #23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
MylesBorins pushed a commit that referenced this pull request Nov 29, 2018
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: #23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
@codebytere codebytere mentioned this pull request Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. code-and-learn Issues related to the Code-and-Learn events and PRs submitted during the events. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.