<commit_before># Backend class for the MoarVM.
class HLL::Backend::MoarVM {
    our %moar_config := nqp::backendconfig();

    method apply_transcodings($s, $transcode) {
        $s
    }

    method config() {
        %moar_config
    }

    method force_gc() {
        nqp::force_gc();
    }

    method name() {
        %moar_config<name>
    }

    method nqpevent($spec?) {
        # Doesn't do anything just yet
    }

    my $prof_start_sub;
    my $prof_end_sub;
    method ensure_prof_routines() {
        unless $prof_start_sub {
            $prof_start_sub := self.compunit_mainline(self.mbc(self.mast(QAST::CompUnit.new(
                QAST::Block.new(
                    QAST::Op.new( :op('mvmstartprofile'),
                        QAST::Var.new( :name('config'), :scope('local'), :decl('param') ) )
                )))));
            $prof_end_sub := self.compunit_mainline(self.mbc(self.mast(QAST::CompUnit.new(
                QAST::Block.new(
                    QAST::Op.new( :op('mvmendprofile') )
                )))));
        }
    }
    method run_profiled($what, $filename) {
        my @END := nqp::gethllsym('perl6', '@END_PHASERS');
        @END.push: -> { self.dump_profile_data($prof_end_sub(), $filename) } if nqp::defined(@END);
        self.ensure_prof_routines();
        $prof_start_sub(nqp::hash());
        my $res  := $what();
        unless nqp::defined(@END) {
            my $data := $prof_end_sub();
            self.dump_profile_data($data, $filename);
        }
        $res;
    }
    method dump_profile_data($data, $filename) {
        my @pieces := nqp::list_s();

        sub post_process_call_graph_node($node) {
            for $node<allocations> -> %alloc_info {
                my $type := %alloc_info<type>;
                %alloc_info<type> := $type.HOW.name($type);
            }
            if $node<callees> {
                for $node<callees> {
                    post_process_call_graph_node($_);
                }
            }
        }

        sub to_json($obj) {
            if nqp::islist($obj) {
                nqp::push_s(@pieces, '[');
                my $first := 1;
                for $obj {
                    if $first {
                        $first := 0;
                    }
                    else {
                        nqp::push_s(@pieces, ',');
                    }
                    to_json($_);
                }
                nqp::push_s(@pieces, ']');
            }
            elsif nqp::ishash($obj) {
                nqp::push_s(@pieces, '{');
                my $first := 1;
                for $obj {
                    if $first {
                        $first := 0;
                    }
                    else {
                        nqp::push_s(@pieces, ',');
                    }
                    nqp::push_s(@pieces, '"');
                    nqp::push_s(@pieces, $_.key);
                    nqp::push_s(@pieces, '":');
                    to_json($_.value);
                }
                nqp::push_s(@pieces, '}');
            }
            elsif nqp::isstr($obj) {
                nqp::push_s(@pieces, '"');
                if nqp::index($obj, '\\') {
                    $obj := subst($obj, /'\\'/, '\\\\\\\\', :global);
                }
                if nqp::index($obj, '"') {
                    $obj := subst($obj, /'"'/, '\\\\"', :global);
                }
                if nqp::index($obj, "'") {
                    $obj := subst($obj, /"'"/, '\\\'', :global);
                }
                nqp::push_s(@pieces, $obj);
                nqp::push_s(@pieces, '"');
            }
            elsif nqp::isint($obj) || nqp::isnum($obj) {
                nqp::push_s(@pieces, ~$obj);
            }
            else {
                nqp::die("Don't know how to dump a " ~ $obj.HOW.name($obj));
            }
            if nqp::elems(@pieces) > 4096 {<commit_msg>Fix "cannot dump Str" profile report bug.<commit_after># Backend class for the MoarVM.
class HLL::Backend::MoarVM {
    our %moar_config := nqp::backendconfig();

    method apply_transcodings($s, $transcode) {
        $s
    }

    method config() {
        %moar_config
    }

    method force_gc() {
        nqp::force_gc();
    }

    method name() {
        %moar_config<name>
    }

    method nqpevent($spec?) {
        # Doesn't do anything just yet
    }

    my $prof_start_sub;
    my $prof_end_sub;
    method ensure_prof_routines() {
        unless $prof_start_sub {
            $prof_start_sub := self.compunit_mainline(self.mbc(self.mast(QAST::CompUnit.new(
                QAST::Block.new(
                    QAST::Op.new( :op('mvmstartprofile'),
                        QAST::Var.new( :name('config'), :scope('local'), :decl('param') ) )
                )))));
            $prof_end_sub := self.compunit_mainline(self.mbc(self.mast(QAST::CompUnit.new(
                QAST::Block.new(
                    QAST::Op.new( :op('mvmendprofile') )
                )))));
        }
    }
    method run_profiled($what, $filename) {
        my @END := nqp::gethllsym('perl6', '@END_PHASERS');
        @END.push: -> { self.dump_profile_data($prof_end_sub(), $filename) } if nqp::defined(@END);
        self.ensure_prof_routines();
        $prof_start_sub(nqp::hash());
        my $res  := $what();
        unless nqp::defined(@END) {
            my $data := $prof_end_sub();
            self.dump_profile_data($data, $filename);
        }
        $res;
    }
    method dump_profile_data($data, $filename) {
        my @pieces := nqp::list_s();

        sub post_process_call_graph_node($node) {
            for $node<allocations> -> %alloc_info {
                my $type := %alloc_info<type>;
                %alloc_info<type> := $type.HOW.name($type);
            }
            if $node<callees> {
                for $node<callees> {
                    post_process_call_graph_node($_);
                }
            }
        }

        sub to_json($obj) {
            if nqp::islist($obj) {
                nqp::push_s(@pieces, '[');
                my $first := 1;
                for $obj {
                    if $first {
                        $first := 0;
                    }
                    else {
                        nqp::push_s(@pieces, ',');
                    }
                    to_json($_);
                }
                nqp::push_s(@pieces, ']');
            }
            elsif nqp::ishash($obj) {
                nqp::push_s(@pieces, '{');
                my $first := 1;
                for $obj {
                    if $first {
                        $first := 0;
                    }
                    else {
                        nqp::push_s(@pieces, ',');
                    }
                    nqp::push_s(@pieces, '"');
                    nqp::push_s(@pieces, $_.key);
                    nqp::push_s(@pieces, '":');
                    to_json($_.value);
                }
                nqp::push_s(@pieces, '}');
            }
            elsif nqp::isstr($obj) {
                nqp::push_s(@pieces, '"');
                if nqp::index($obj, '\\') {
                    $obj := subst($obj, /'\\'/, '\\\\\\\\', :global);
                }
                if nqp::index($obj, '"') {
                    $obj := subst($obj, /'"'/, '\\\\"', :global);
                }
                if nqp::index($obj, "'") {
                    $obj := subst($obj, /"'"/, '\\\'', :global);
                }
                nqp::push_s(@pieces, $obj);
                nqp::push_s(@pieces, '"');
            }
            elsif nqp::isint($obj) || nqp::isnum($obj) {
                nqp::push_s(@pieces, ~$obj);
            }
            elsif nqp::can($obj, 'Str') {
                to_json(nqp::unbox_s($obj.Str));
            }
            else {
                nqp::die("Don't know how to dump a " ~ $obj.HOW.name($obj));
            }
            if nqp::elems(@pieces) > 4096 {
{'fraction_non_alphanumeric': 0.16291073901548533, 'fraction_numerical': 0.0022661462923328716, 'mean_word_length': 1.2270815811606393, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '896532', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2372, 'n_words': 359}