TooTallNate / ref

Turn Buffer instances into "pointers"
http://tootallnate.github.com/ref
453 stars 141 forks source link

Performance Issue in asserts in coerceType function #38

Closed dan-tull closed 8 years ago

dan-tull commented 8 years ago

I was just profiling some code that uses ref-struct to parse a binary log file and was finding several locations where I accessed fields on a struct instance were showing up as hot spots. Further digging points to this assertion statement:

assert(rtn && 'size' in rtn && 'indirection' in rtn
            , 'could not determine a proper "type" from: ' + JSON.stringify(type))

Though the assertion is never failing in my case, the code was doing a lot of work to generate the error message anyway and that work was really adding up over time.

Changing the asserts to:

        if (!(rtn && 'size' in rtn && 'indirection' in rtn)) {
             assert(false, 'could not determine a proper "type" from: ' + JSON.stringify(type))
        }

...sped up parsing time for a file with 180K events from 16 to 8 seconds.

I may still wind up changing to manual (or even native) parsing for extra speed, but the ref/struct/ffi libraries have been so useful I wanted to take the time to pass along a report. I could send the change as a pull request, too if the change looks satisfactory.

Thanks.

TooTallNate commented 8 years ago

That sounds like a smart change to me :+1:. Pull request welcome!