src: clean up MaybeStackBuffer usage in i18n#11464
src: clean up MaybeStackBuffer usage in i18n#11464TimothyGu wants to merge 5 commits intonodejs:masterfrom
Conversation
|
/cc @nodejs/intl |
src/node_internals.h
Outdated
There was a problem hiding this comment.
I think I would prefer it if we did the SwapBytes16() at the call side and kept Buffer::New()s job restricted to turning the memory block into a Buffer instance?
src/util.h
Outdated
There was a problem hiding this comment.
Right now, if the buffer wasn’t allocated before, the previous contents are lost (but not if it was malloc()’ed). I guess this okay and intended behaviour, but maybe note that in the comment, too?
There was a problem hiding this comment.
PR updated. Now contents up to length_ - 1 are preserved if buf_ was on the stack.
srl295
left a comment
There was a problem hiding this comment.
the ICU changes seem good to me. (Side note: I misread MaybeStackBuffer as ICU's MaybeStackArray at first… )
|
Rebased and changed Updated CI: https://ci.nodejs.org/job/node-test-pull-request/6535/ |
src/node_i18n.cc
Outdated
There was a problem hiding this comment.
Tiny style nit: space before <.
src/node_internals.h
Outdated
src/node_internals.h
Outdated
There was a problem hiding this comment.
sizeof(buf[0]) should work too and is arguably a little more Obviously Correct. Here, you don't know what T is without looking at the surrounding code.
src/util.h
Outdated
There was a problem hiding this comment.
Somewhat superfluous comment.
src/util.h
Outdated
There was a problem hiding this comment.
Can you punctuate the comment?
src/util.h
Outdated
There was a problem hiding this comment.
s/are/is/? 'Contents' is indefinite and therefore singular (I think? Not 100% sure.)
src/util.h
Outdated
There was a problem hiding this comment.
The explicit <T> shouldn't be necessary. Not a big deal, though.
src/util.h
Outdated
src/util.h
Outdated
There was a problem hiding this comment.
Can you punctuate the comments here and below?
test/cctest/util.cc
Outdated
There was a problem hiding this comment.
Consider putting this in a template function rather than a macro. Rule of thumb: template when you can, macro when you must.
jasnell
left a comment
There was a problem hiding this comment.
Overall LGTM with @bnoordhuis' comments addressed
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer
- Templatize AsBuffer() and create a generic version for inclusion in the Buffer class - Use MaybeStackBuffer::storage() - If possible, avoid double conversion in ToASCII()/ToUnicode() - More descriptive assertion error in tests
|
@bnoordhuis, comments addressed. PTAL. Updated CI: https://ci.nodejs.org/job/node-test-pull-request/6560/ |
| return ret; | ||
|
|
||
| if (buf->IsAllocated()) | ||
| buf->Release(); |
There was a problem hiding this comment.
Can this be done in the if block above?
There was a problem hiding this comment.
The ret.IsEmpty() check has to happen before calling buf->Release(), so unfortunately not.
src/util.h
Outdated
| } | ||
|
|
||
| void SetLengthAndZeroTerminate(size_t length) { | ||
| // length_ stores how much memory was allocated. |
There was a problem hiding this comment.
This comment also needs to be updated?
PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Templatize AsBuffer() and create a generic version for inclusion in the Buffer class - Use MaybeStackBuffer::storage() - If possible, avoid double conversion in ToASCII()/ToUnicode() - More descriptive assertion error in tests PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
|
Landed in 289e532...d4e1eaf. |
PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Templatize AsBuffer() and create a generic version for inclusion in the Buffer class - Use MaybeStackBuffer::storage() - If possible, avoid double conversion in ToASCII()/ToUnicode() - More descriptive assertion error in tests PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
|
Parts of this should likely be backported to v6.x (and possibly v4.x). |
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer PR-URL: nodejs#11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer Backport-PR-URL: #17365 PR-URL: #11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer Backport-PR-URL: #17365 PR-URL: #11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer Backport-PR-URL: #17365 PR-URL: #11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Add IsInvalidated() method - Add capacity() method for finding out the actual capacity, not the current size, of the buffer - Make IsAllocated() work for invalidated buffers - Allow multiple calls to AllocateSufficientStorage() and Invalidate() - Assert buffer is malloc'd in Release() - Assert buffer has not been invalidated in AllocateSufficientStorage() - Add more descriptive comments describing the purpose of the methods - Add cctest for MaybeStackBuffer Backport-PR-URL: #17365 PR-URL: #11464 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
This PR can roughly be divided into two parts. It first cleans up
MaybeStackBufferitself:IsAllocated()work for invalidated bufferscapacity()method for finding out the actual capacity, not the current size, of the bufferAllocateSufficientStorage()andInvalidate()Release()Then, it aims to make the usage of
MaybeStackBufferinnode_i18nmore orthodox:MaybeStackBuffer<T>-toBufferconversion function (AsBuffer()) and move it toBuffernamespace as a private constructorMaybeStackBuffer, but there are multiple problems in doing that due to header/class dependency. On the other hand,Bufferis as good as a home for the function as any.MaybeStackBuffer::storage()instead of defining its own version ofkStackStorageSize(which may get out of sync with the official one inMaybeStackBuffer)lenparameter toAsBuffer(), useMaybeStackBuffer::SetLength()uniformlyToASCII()/ToUnicode()That last bullet point brings a nice performance enhancement for
url.domainToASCII()/domainToUnicode()with short strings (nreduced to 1×106 since this method is not as susceptible to JIT fluctuations):Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
src