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

src: malloced_unique_ptr & make_malloced_unique #23642

Closed
wants to merge 1 commit into from

Conversation

refack
Copy link
Contributor

@refack refack commented Oct 13, 2018

Alternative to #23641

malloced_unique_ptr is just a specialization of std:unique_ptr using free for deletion, and make_malloced_unique<T> is it's factory using Malloc<T>.

Ref: #23641
Ref: #23543 (review)
Ref: #23434

CI: https://ci.nodejs.org/job/node-test-pull-request/17822/

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

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. labels Oct 13, 2018
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

The crypto changes are not improving the code.

There’s value in grouping all information about a buffer in a single struct in terms of readability, maintaining less individual local variables if they would otherwise only make sense as a pair.

@refack refack added the lib / src Issues and PRs related to general changes in the lib or src directory. label Oct 13, 2018
@refack
Copy link
Contributor Author

refack commented Oct 13, 2018

if they would otherwise only make sense as a pair.

IMHO they are only loosely make sense as a pair. They are not used as a pair, and are used in association only in:
https://github.com/nodejs/node/blob/13b0bbff04545476127b6b7dbc86297b80045670/src/node_crypto.cc#L4243

The cost of pairing them together should be weight against using a non-optimal abstraction to store them together.

@addaleax
Copy link
Member

IMHO they are only loosely make sense as a pair

They define the bounds of array together. Semantically, they are a pair.

They are not used as a pair, and are used in association only in:

I’m not sure what you mean by “in association” – they are used together in multiple calls you are modifying here.

As for the Buffer::New() call, it might even make sense to have a utility for MallocedBuffer → JS Buffer conversion. (But … that opens another can of worms and I’d prefer not to do it at this point. Talking about embedding use cases at the collaborator summit, it might make sense for us to provide a custom memory allocation mechanism for things that end up as ArrayBuffers).

@refack refack force-pushed the add-malloced-unique-ptr branch from 13b0bbf to a583814 Compare October 13, 2018 18:42
@refack
Copy link
Contributor Author

refack commented Oct 13, 2018

They define the bounds of array together. Semantically, they are a pair.

Yes, but as I read it they are not used together, so the cost of keeping them together outweighs the benefits.

As for the Buffer::New() call, it might even make sense to have a utility for MallocedBuffer → JS Buffer conversion. (But … that opens another can of worms and I’d prefer not to do it at this point. Talking about embedding use cases at the collaborator summit, it might make sense for us to provide a custom memory allocation mechanism for things that end up as ArrayBuffers).

Yeah it's tricky. But I do agree that if that existed it would make sense to use a Buffer-like structure all along.
ATM as I read it MallocedBuffer is used mostly for it's RAII properties...

@addaleax
Copy link
Member

ATM as I read it MallocedBuffer is used mostly for it's RAII properties...

It’s better than malloc()/free() because of that, yes. It’s also a sensible abstraction for buffers.

They define the bounds of array together. Semantically, they are a pair.

Yes, but as I read it they are not used together, so the cost of keeping them together outweighs the benefits.

What cost are you referring to? There’s zero runtime overhead over having two separate variables, but on the other hand, there’s readability overhead in splitting the struct up.

Copy link
Member

@fhinkel fhinkel left a comment

Choose a reason for hiding this comment

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

I'd rather we document that MallocedBuffer(T* data, size_t size) assumes that data data was allocated with malloc than delete the method.

@refack refack force-pushed the add-malloced-unique-ptr branch from a583814 to 5bd49eb Compare October 20, 2018 21:28
@refack refack removed the crypto Issues and PRs related to the crypto subsystem. label Oct 20, 2018
@refack
Copy link
Contributor Author

refack commented Oct 20, 2018

Reverted unrelated changes, PTAL.
CI: https://ci.nodejs.org/job/node-test-pull-request/18013/

@refack refack self-assigned this Oct 20, 2018
@refack refack closed this Dec 21, 2018
@refack refack deleted the add-malloced-unique-ptr branch December 21, 2018 01:14
@refack refack removed their assignment Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. 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.

4 participants