-
Notifications
You must be signed in to change notification settings - Fork 10
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
Use Nan::Utf8String over String::Utf8Value #54
Comments
Awesome, thanks for this writeup @springmeyer . QvestionCurrently the skel does this...
My real question is...is |
Also trying to figure out why |
Sorry, made a copy/paste error. You are right - I've fixed the post above to note what we are currently doing and should avoid.
Really insightful! That is exactly the kind of question that will help you be cautious and write secure and robust code. In this case Nan has all the smarts needed internally to validate, in a fast way, that the string is valid, see https://github.com/nodejs/nan/blob/7291008d755f658343bcb61f456cb0a6744a6235/nan.h#L911. So if any of those fail then the length will be |
GREAT question. You are thinking like a performance engineer!!!. So the answer is |
Ah, wasnt saying you were wrong. It tied into my initial comment which was digging into the comparison between the old way and the Nan way (the strike-through above), but then I altered my qvestion. The reason I asked about whether or not |
👍
Right, While we are thinking about bypassing allocation, another alternative would be to pass the pointer that references a V8 object directly into the threadpool (instead of converting to a std::string). This is the ideal. I've not tried this with |
Nan string PR merged at #55 @springmeyer Good to close this and carry future improvement convo per your last comment to separate ticket? |
Good to close 👍 As far as last convo about moving to a |
* Add support for node 12 See mapbox/node-cpp-skel#54 nodejs/nan#849 * updates * Only need this * Use compatible versions * use dockerfile * updates * caching * Run commands from docker * Fix cov report * Fixes * lowercase * re-add cov check * change back to yarn run
A common way to get data from a v8::String to pass to C++ is:
While concise (by v8/c++ terms) this can be improved. Instead of
v8::String::Utf8Value
we can useNan::Utf8String
and we should validate the input before trying to create astd::string
by:info[0]
actually is a stringThe
Nan::Utf8String
api both validates strings internally and is fast. It has an optimized conversion routine similar to node core. And it works across all recent node versions.So we should convert all usage of
String::Utf8Value
toNan::Utf8String
. Like theString::Utf8Value
interface, theNan::Utf8String
API exposes is the ability to check the length of the string before accessing in the value: this is a very good idea for safety and robustness. We don't want to risk passing a null string around, which might create unpredictable behavior or a crash like in mapbox/node-fontnik#124.To keep 100% test coverage, after adding lines that check the line, we will need to add a test that tries to pass an empty string, and will need to assert reasonable behavior in this case. So, let's move to code like this:
An additional (albeit minor unless the string is massive) performance win of getting the
len
of the string is that we can pass it to thestd::string
constructor. Doingstd::string string_copy(*utf8_value);
would work, but providing the length allows thestd::string
constructor to avoid calculating the length internally and should be faster since it skips an operation. Thestd::string
class has a bewildering number of different constructors. This one we are using that accepts aconst char *
as the first arg and anint
as the second argument is the(4)
from http://en.cppreference.com/w/cpp/string/basic_string/basic_string./cc @GretaCB
The text was updated successfully, but these errors were encountered: