Skip to content

Large amount of time in loop spent in instanceof #2577

Description

@theqabalist

Hi, this is less of an issue, and more of a discussion for my understanding. I did advent of code 2016 in node, and I was going back to do some of the problems in PS. One of the problems I reimplemented, and I noticed that the performance was degrading with fringe size (in A*). I profiled the outputted code and noticed that 1/3rd of the time was spent in C++ instanceof checks, and I was looking at the referenced javascript code. I notice that instanceof is used extensively for the pattern matching mechanism, and was curious if this is the most performant way to do pattern matching or if something like performance on an operator like that cannot be predictably evaluated ahead of time, and you have to take what you can get. A second part of my question is how you would go about reducing the number of these checks, because it would seem like avoiding pattern matching loses quite a bit of robustness in expression.

Here is a profile preview for reference

89758   90.0%   33.6%  v8::internal::Runtime_InstanceOf(int, v8::internal::Object**, v8::internal::Isolate*)
9350    9.4%    3.5%  v8::internal::Runtime_Interrupt(int, v8::internal::Object**, v8::internal::Isolate*)
88    0.1%    0.0%  v8::internal::Runtime_ToInteger(int, v8::internal::Object**, v8::internal::Isolate*)
88    0.1%    0.0%  v8::internal::Object::GetMethod(v8::internal::Handle<v8::internal::JSReceiver>, v8::internal::Handle<v8::internal::Name>)

Referencing code like

var lookup = function (dictOrd) {
    return Partial_Unsafe.unsafePartial(function (dictPartial) {
        return function (k) {
            return function (tree) {
                if (tree instanceof Leaf) {
                    return Data_Maybe.Nothing.value;
                };
                var comp = Data_Ord.compare(dictOrd);
                var __unused = function (dictPartial1) {
                    return function ($dollar42) {
                        return $dollar42;
                    };
                };
                return __unused(dictPartial)((function () {
                    if (tree instanceof Two) {
                        var $177 = comp(k)(tree.value1);
                        if ($177 instanceof Data_Ordering.EQ) {
                            return new Data_Maybe.Just(tree.value2);
                        };
                        if ($177 instanceof Data_Ordering.LT) {
                            return lookup(dictOrd)(k)(tree.value0);
                        };
                        return lookup(dictOrd)(k)(tree.value3);
                    };

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions