Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up projects.json file #273

Closed
lizmat opened this issue Sep 20, 2018 · 8 comments
Closed

Speed up projects.json file #273

lizmat opened this issue Sep 20, 2018 · 8 comments

Comments

@lizmat
Copy link
Contributor

lizmat commented Sep 20, 2018

Please find below the code I wrote to hyper the parsing of the "projects.json" file. This drops the parsing from 2.2 seconds to 1 second on my machine (without the new quick parser on rakudo) and to 0.3 with the new quick parser.

It basically depends on the way the "projects.json" file is created, with the first line being [ and the last line being ], and separated by lines consisting of just , in between.

I'm not sure where to hook that into zef, so I'm adding it here as an issue.

use Test;
use nqp;

my $file = "projects.json";
my $meta = $file.IO.slurp;
my $then = now;
my @old := Rakudo::Internals::JSON.from-json($meta);
say "Old: {now - $then}";

my class Chunkify does Iterator {
    has $.iterator;
    has str @!lines;
    has int $!done;

    method TWEAK() {
        die "did not see beginning" unless $!iterator.pull-one eq '['
    }

    method !done() {
        my \chunk = @!lines.join(" ");
        @!lines = ();
        chunk
    }

    method pull-one() {
        if $!done {
            IterationEnd
        }
        else {
            until (my \pulled = $!iterator.pull-one) =:= IterationEnd {
                if pulled eq ',' {
                    return self!done;
                }
                elsif pulled eq ']' {
                    $!done = 1;
                    return self!done;
                }
                else {
                    @!lines.push(pulled);
                }
            }
            die 'did not see end';
        }
}

sub parse-projects($file) {
    Seq.new(Chunkify.new( iterator => $file.IO.lines.iterator ))
      .hyper.map: { Rakudo::Internals::JSON.from-json($_) }
}

$then = now;
my @new = parse-projects($file);
say "New: {now - $then}";
#dd @new;

#is-deeply @new[$_], @old[$_] for ^@old;
lizmat referenced this issue in rakudo/rakudo Sep 20, 2018
This parser only handles hashes, arrays and strings and will return
Nil if *anything* goes wrong.  It is now plugged into "R:I:JSON.from-json"
to first attempt the very fast parser: of that fails, fall back to the
original, slow, grammar based parser.

How much faster: at least 10x faster!
@ugexe
Copy link
Owner

ugexe commented Sep 20, 2018

Technically this works, but only for the ecosystem data specifically generated and served by me. It wouldn’t work on the ecosystem data generated by ecosystem-api.p6c.org (a default) or any darkpan-like.

@niner
Copy link
Collaborator

niner commented Sep 20, 2018 via email

@ugexe
Copy link
Owner

ugexe commented Sep 20, 2018

I'm content with the slower parsing for less technical debt trade off.

@lizmat
Copy link
Contributor Author

lizmat commented Sep 20, 2018

Please find below another version of the parallel parsing: this version does not depend on any particular format: it parses both CPAN and ecosystem JSON files. On my machine this reduces the parse time from 2.3 to about 0.9. I think this presents a reasonable speed increase / technical debt tradeoff, as it basically only chunks the JSON files and then uses whatever parser is around to parse the chunks in parallel. The only thing it expects is the first non-whitespace char to be a [ and the last non-whitespace char to be a ].

use Test;
use nqp;

my $file = "projects2.json";
my $meta = $file.IO.slurp;
my $then = now;
my @old := Rakudo::Internals::JSON.from-json($meta);
say "Old: {now - $then}";

my class Chunkify does Iterator {
    has str $.json;
    has int $!pos;

    my int $backslash   = ord('\\');
    my int $open-array  = ord('[');
    my int $close-array = ord(']');
    my int $open-hash   = ord('{');
    my int $close-hash  = ord('}');

    method TWEAK() {
        die "did not see beginning"
          unless nqp::ordat($!json,$!pos) == $open-array;
    }

    method pull-one() {
        my int $ord;
        my int $start;
        my int $open-hashes;
        my int $pos   = $!pos;
        my int $chars = nqp::chars($!json);

        while ++$pos < $chars {
            $ord = nqp::ordat($!json,$pos);
            if $ord == $backslash {
                # no action, we ignore backslashes
            }
            elsif $ord == $close-hash {
                unless --$open-hashes {
                    die "no start found" unless $start;
                    $!pos = $pos;
                    return nqp::substr($!json,$start,$pos - $start + 1)
                }
            }
            elsif $ord == $open-hash {
                $start = $pos unless $open-hashes++;
            }
        }
        nqp::ordat($!json,$pos - 1) == $close-array
          ?? IterationEnd
          !! die 'did not see end';
    }
}

sub parse-projects($file) {
    Seq.new(Chunkify.new( json => $file.IO.slurp.trim ))
      .hyper(:32batch).map: { Rakudo::Internals::JSON.from-json($_) }
}

$then = now;
my @new = parse-projects($file);
say "New: {now - $then}";
#dd @new;

#is-deeply @new[$_], @old[$_] for ^@old;

@lizmat
Copy link
Contributor Author

lizmat commented Sep 20, 2018

Never mind: I just realized we could hook this into R:I:from-json, which would relieve the technical debt from zef

@lizmat lizmat closed this as completed Sep 20, 2018
@lizmat
Copy link
Contributor Author

lizmat commented Sep 20, 2018

rakudo/rakudo@c9432c2072

@ugexe
Copy link
Owner

ugexe commented Sep 20, 2018

Hmm, but I do not see a speed difference between 2018.08 and blead for time zef search HTTP

fwiw I get ~7s with or without the JSON improvement, and 4.8s with JSON::Fast

@ugexe
Copy link
Owner

ugexe commented Sep 20, 2018

I see a speedup for time perl6 -e 'from-json("projects1.json".IO.slurp)' (2.5s to 1.6s), but for whatever reason that doesn't seem to improve speed for zef search ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants