How to disable V8 optimization compiler

I am writing a constant-time string comparison function (for node.js) and would like to disable the V8 compiler for this single function; using command line flags is out of the question.

I know that using the with{} block (or try / catch) will now disable the optimizing compiler, but I am afraid that this β€œfunction” (error) will be fixed in future versions.

Is there an immutable (and documented) way to disable the V8-optimizing compiler?




Function example:

 function constantTimeStringCompare( a, b ) { // By adding a `with` block here, we disable v8 optimizing compiler. // Using Object.create(null) ensures we don't have any object prototype properties getting in our way.our way. with ( Object.create( null ) ){ var valid = true, length = Math.max( a.length, b.length ); while ( length-- ) { valid &= a.charCodeAt( length ) === b.charCodeAt( length ); } // returns true if valid == 1, false if valid == 0 return !!valid; } } 

And perf test is just for fun.

+6
compiler-optimization javascript google-chrome v8
Aug 27 '13 at 22:17
source share
2 answers

If you need a reliable way to do this, you need to run node with the --allow-natives-syntax flag and call this:

 %NeverOptimizeFunction(constantTimeStringCompare); 

Note that you must call this before you call constantTimeStringCompare , if the function is already optimized, this violates the statement.

Otherwise, the with statement is your best bet, since optimization will be absolutely insane, while try/catch support will be reasonable. You do not need this to affect your code, but that will be enough:

 function constantTimeStringCompare( a, b ) { with({}); var valid = true, length = Math.max( a.length, b.length ); while ( length-- ) { valid &= a.charCodeAt( length ) === b.charCodeAt( length ); } // returns true if valid == 1, false if valid == 0 return !!valid; } 

A simple mention of the with statement distorts the entire function contained - optimization is performed at the level of detail at the function level, and not at each statement.

+8
Aug 28 '13 at 12:46 on
source share
β€” -

To actually test a function optimized with a specific version of Node.js, you can refer to the Optimization Killers wiki bluebird.
I tested 3 solutions on Node 7.2:

  • with({}) - Function optimized with TurboFan
  • try {} catch(e) {} - Function optimized with TurboFan
  • eval(''); - The function is not optimized.

To guarantee that V8 optimization is disabled, you must add eval('') to the function body.

+1
Jul 24 '17 at 12:45
source share



All Articles