OK I see. My bad on that assumption. The truth is I actually wanted to write a native node module to do a lot of manipulation of dynamic JS objects (from a few to many thousand properties of varying types). I wrote this example as a test because I started noticing the overhead of marshaling objects in C++ was higher than I expected. Are there any v8 data structures that would make ^^ task reasonably faster in C++? Or would pulling the objects into v8 using the API just be expensive? If so, what is the ideal situation that would improve performance by re-writing using v8? Fairly static types? That was the impression I got from the few tutorials I found out there...
On Tuesday, October 25, 2016 at 1:13:11 AM UTC-4, Jochen Eisinger wrote: > > No, I'm talking about the two for loops. > > I'd expect that if you want to transfer large amounts of data back and > forth, you'll get better results by using typed arrays. > > On Tue, Oct 25, 2016 at 7:10 AM Samantha Krieger <[email protected] > <javascript:>> wrote: > >> Thanks for responding! >> >> OK I assume you're talking about the overhead associated largely with >> these lines: >> >> Isolate* isolate = Isolate::GetCurrent(); >> HandleScope scope(isolate); >> >> if (args.Length() < 1 || !args[0]->IsArray()) { >> isolate->ThrowException( >> Exception::TypeError( >> String::NewFromUtf8(isolate, >> "First argument should be an array"))); >> return; >> } >> >> Handle<Array> arr = Handle<Array>::Cast(args[0]); >> >> .... >> >> >> If that is the case: >> >> 1) Wouldn't the overhead get amortized with larger array size? >> (Presumably the answer is no so why not?) >> 2) Is there anything I can do to minimize that overhead? It was my >> understanding that using Isolates and Handle Scopes are a part of the >> necessary boiler plate. Can they be avoided? >> (3) What about them causes so much overhead if they are the ones >> responsible?) >> >> Really appreciate all the help!!!! Thanks so much again! >> >> On Monday, October 24, 2016 at 3:42:00 AM UTC-4, Jochen Eisinger wrote: >> >>> compared to the JS function, the C implementation has a lot of overhead >>> to copy the data back and forth, so it's not surprising that this overhead >>> shows up especially on larger arrays >>> >>> On Mon, Oct 24, 2016 at 2:47 AM Samantha Krieger <[email protected]> >>> wrote: >>> >> Just wondering if I'm crazy/doing something incredibly stupid. >>>> >>>> I wrote this sort function using v8 (wrapper around qsort): >>>> >>>> void Sort(const FunctionCallbackInfo<Value>& args) { >>>> Isolate* isolate = Isolate::GetCurrent(); >>>> HandleScope scope(isolate); >>>> >>>> if (args.Length() < 1 || !args[0]->IsArray()) { >>>> isolate->ThrowException( >>>> Exception::TypeError( >>>> String::NewFromUtf8(isolate, >>>> "First argument should be an array"))); >>>> return; >>>> } >>>> >>>> Handle<Array> arr = Handle<Array>::Cast(args[0]); >>>> int size = arr->Length(); >>>> double other_arr[size]; >>>> for (int i = 0; i < size; i++){ >>>> other_arr[i] = arr->Get(i)->NumberValue(); >>>> } >>>> >>>> >>>> qsort(other_arr, size, sizeof(other_arr[0]), compare); >>>> Handle<Array> res = Array::New(isolate, size); >>>> for (int i = 0; i < size; ++i) { >>>> // printf ("I val: %f\n",other_arr[i]); >>>> res->Set(i, Number::New(isolate, other_arr[i])); >>>> } >>>> >>>> args.GetReturnValue().Set(res); >>>> >>>> } >>>> >>>> >>>> >>>> when i run it in js from node, its markedly slower than the equivalent js >>>> sort (which I understand v8 [].sort() uses qsort under the hood) for >>>> objects between 1000 - 10000+ >>>> >>>> >>>> var sort = require('bindings')('sort.node'); >>>> >>>> >>>> function benchmarkSort(size){ >>>> >>>> var arr = helpers.generateArray(size); >>>> var cres = benchmark(sort.sort, [arr], 10); >>>> >>>> >>>> var arr = helpers.generateArray(size); >>>> var jsres = benchmark(helpers.sort, [arr], 10); >>>> return { >>>> c: cres, >>>> js: jsres >>>> }; >>>> >>>> } >>>> >>>> >>>> function benchmark(fn, args, times){ >>>> var noTimes = (typeof times != 'undefined'? times: NO_TIMES); >>>> var start = Date.now(); >>>> var result = run(fn, noTimes, args); >>>> var duration = Date.now() - start; >>>> return duration; >>>> } >>>> >>>> >>>> ----- Cost for sorting 100 ------- >>>> JS time: 1 msec >>>> C time: 1 msec >>>> ----- Cost for sorting 1000 ------- >>>> JS time: 4 msec >>>> C time: 3 msec >>>> ----- Cost for sorting 10000 ------- >>>> JS time: 22 msec >>>> C time: 37 msec >>>> ----- Cost for sorting 100000 ------- >>>> JS time: 278 msec >>>> C time: 441 msec >>>> >>>> >>>> These results seem incredibly strange. Am I doing something incredibly >>>> stupid/missing something? Can someone explain whats going on? Much >>>> appreciated!!!! >>>> >>>> -- >>>> -- >>>> v8-dev mailing list >>>> >>> [email protected] >>> >>> >>>> http://groups.google.com/group/v8-dev >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "v8-dev" group. >>>> >>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>> >>> >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- >> -- >> v8-dev mailing list >> [email protected] <javascript:> >> http://groups.google.com/group/v8-dev >> --- >> You received this message because you are subscribed to the Google Groups >> "v8-dev" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > -- -- v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev --- You received this message because you are subscribed to the Google Groups "v8-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
