latin: prefer cached json of static data include
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 14 May 2022 12:50:45 +0000 (14:50 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 6 Feb 2023 12:35:32 +0000 (13:35 +0100)
Executing perl takes about 23ms (6ms if dumped with perlinc-static)
while parsing the same data from JSON takes just 1.2ms.

Makefile
common.inc.plp
tools/perlinc-static [deleted file]

index c6c18d952db1c29cb5e8d448bcf953eadc943692..75f269b81c478281499b4abfc9d75470a402262d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-all: sitemap.xml light.css plan.plp UPDATE data/digraphs.inc.pl data/unicode-cover.inc.pl data/countries.inc.pl data/browser data/termcol-xcolor.inc.pl data/digraphs-xorg.inc.pl word
+all: sitemap.xml light.css plan.plp UPDATE data/digraphs.inc.pl data/unicode-cover.inc.pl data/countries.inc.pl data/browser data/termcol-xcolor.inc.pl data/digraphs-xorg.inc.pl word cache
 more: all
 
 .PHONY: force # applied to download after 2 hours
@@ -23,6 +23,7 @@ plan.plp: TODO
 UPDATE: $(download)
        $(call cmdsave,git log -1 --date=short --pretty="%ad    %s")
 
+cache: data/writing-latn.json
 word: word/put.min.js data/wordlist.inc.pl data/wordlist.en.json data/wordlist.nl.json data/wordlist.ru.json data/wordpairs.json
 
 word/put.js: $(download)
@@ -87,6 +88,8 @@ data/wordlist.%.inc.pl: tools/mkwordlist data/wordlist.version.txt
        $(call cmdsave,$< $*)
 data/word%.json: data/word%.inc.pl
        $(call cmdsave,perl -MJSON=encode_json -E "print encode_json(do \$$ARGV[0])" ./$<)
+data/%.json: %.inc.pl
+       $(call cmdsave,perl -MJSON=encode_json -E "print encode_json([ do \$$ARGV[0] ])" ./$<)
 
 data/wordpairs.inc.pl: data/wordlist.version.txt
        @perl -I. -MShiar_Sheet::DB -MData::Dump=pp -E 'say pp(Shiar_Sheet::DB->connect->select("word w JOIN word a ON w.id=a.ref" => "w.id, a.id", {"a.lang"=>undef})->map or exit 1)' >$@
@@ -158,8 +161,3 @@ clean:
        -rm data/browser/support.inc.pl
        -rm data/browser/usage-wm.inc.pl
 
-.SECONDEXPANSION:
-
-data/writing-latn.inc.pl: tools/perlinc-static $$(@F)
-       $(call cmdsave,$^)
-
index 39865bdb55f2ca961da6d7f0ebcaecbc760aab9a..afec9d11f2172bb2130301eaf7468422a86020a2 100644 (file)
@@ -102,7 +102,11 @@ sub checkmodified {
 sub Data {
        my ($filename) = @_;
        my @data = eval {
-               do "$filename.inc.pl";
+               open my $cache, '<:raw', "data/$filename.json"
+                       or return do "$filename.inc.pl"; # silent fallback to original code
+               require JSON;
+               local $/; # slurp
+               return JSON::decode_json(readline $cache);
        };
        if ($! or $@ or !@data or !$data[0]) {
                die ['Table data not found', $@ || $!];
diff --git a/tools/perlinc-static b/tools/perlinc-static
deleted file mode 100755 (executable)
index 4edf5ac..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env perl
-use 5.014;
-use warnings;
-
-use Data::Dumper;
-
-my @data = do $ARGV[0] or die $@;
-print Data::Dumper->new([\@data])->Terse(1)->Quotekeys(0)->Indent(1)->Dump;