-
-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Some internal functions used to validate arguments, like validateString, seem to be called all over the place throughout the codebase, so making these calls as fast as possible may be worth it.
IMO the way they are being used today is probably a lot less efficient than it could be, though without measuring the impact of the change it's hard to say what it will be.
Let's take a look at a scenario, and at how to make it faster:
- For example
validateStringis being called here to validate our argument when doingcrypto.createHash('sha1'). validateString's definition is basically a function wrapped byhideStackFrameshideStackFramesbasically makes a function that calls our function, viaReflectApply, and if an error is thrown it does something with it and re-throws it.- Finally the actual body we want to execute of the
validateStringfunction just does a typeof check, and if it fails an error is thrown.
That seems a pretty convoluted way to do a typeof check basically. And I think it could be made faster, though unclear by how much, like this:
- Instead of writing this:
I guess we could just write this:
const validateString = hideStackFrames((value, name) => { if (typeof value !== 'string') throw new ERR_INVALID_ARG_TYPE(name, 'string', value); });
Basically the idea is that unless the error is actually throw, we pay absolutely 0 cost for whateverconst validateString = (value, name) => { if (typeof value !== 'string') throw hideStackFramesFromError(new ERR_INVALID_ARG_TYPE(name, 'string', value)); };
hideStackFramesdoes, and instead we manually call a function that does what's needed on the error. Doing it this way would be a bit more error prone, but these validation functions seem to be fairly simple, and called all over the place, so manually inserting thehideStackFramesFromErrorcalls in the right spots shouldn't be a problem. - Lastly it may be best to inline these tiny functions that are called a million times at build time, perhaps defining a dedicated function for their erroring branch, to keep the emitted code to parse minimal. So for example the definition of our
validateStringwould become something like this instead:And when inlined this code:const validateString = (value, name) => { if (typeof value !== 'string') validateStringFailure(value, name); };
Will be transformed automatically into this:if (!(algorithm instanceof _Hash)) validateString(algorithm, 'algorithm');
if (!(algorithm instanceof _Hash)) if(typeof algorithm !== 'string') validateStringFailure(algorithm, 'algorithm');
Basically when errors won't actually be thrown, which presumably is almost always the case, we would go from 3 function calls to 1 without the inline step, and to 0 function calls with the inline step. Also the try..catch block would get deleted, that may be ~zero cost though if not triggered, I'm not sure.
I would guess that we could see a low single-digit percentage performance improvement with this optimization applied to all the validation functions, at least in some cases, because pretty much every Node API seems to call some of these, often more than once, so it seems probably a worthwhile optimization to me.