start experimental cache package

This commit is contained in:
Maximilian Keßler 2022-02-02 17:24:06 +01:00
parent 4bda829518
commit ddbdc19d2f

109
src/wip/cache/cache.pysty3 vendored Normal file
View file

@ -0,0 +1,109 @@
__PACKAGE__(cache)
\bool_new:N \g__cache_cache_bool
\int_new:N \g__cache_cache_version_document_int
\int_new:N \g__cache_cache_version_aux_int
\int_gset:Nn \g__cache_cache_version_aux_int { -1 }
\tl_new:N \g__cache_dump_auxfile_tl
\tl_new:N \g__cache_lazy_document_tl
\tl_new:N \g__cache_lazy_auxfile_tl
\keys_define:nn { cache }
{
cache .bool_set:N = \g__cache_cache_bool,
cache .default:n = { true },
__cache version__ .int_set:N = \g__cache_cache_version_document_int,
cache version .meta:nn = { cache } { cache = true, __cache version__ = #1 },
cache version .default:n = { 0 },
}
\RequirePackage{l3keys2e}
\ProcessKeysOptions{cache}
% Setting up lazy execution and
% selecting of executing after reading of aux file
% if cache option has been used
\bool_if:NTF \g__cache_cache_bool
{
%% Writing things (at one go) to aux file at end of document:
\cs_new:Npn \__cache_put_aux:n #1
{
\tl_gput_right:Nn \g__cache_dump_auxfile_tl { #1 }
}
\cs_generate_variant:Nn \__cache_put_aux:n { x, V }
\cs_generate_variant:Nn \iow_now:Nn { c V }
%%% Handles dumping data to aux file at end of document
\hook_gput_code:nnn { enddocument } { cache }
{
\iow_now:cV { @auxout } \g__cache_dump_auxfile_tl
}
%%% Writes the current cache version into aux file
\__cache_put_aux:x
{
\ExplSyntaxOn
\int_gset:Nn \exp_not:N \g__cache_cache_version_aux_int
{
\int_use:N \g__cache_cache_version_document_int
}
\ExplSyntaxOff
}
%%% Executing something lazily at beginning of document
%%% Lazy code only gets executed if auxfile version is older than document
\cs_new:Npn \__cache_lazy:n
{
\tl_gput_right:Nn \g__cache_lazy_document_tl
}
%%% Caching things. Handles writing and reading to aux file
%%% and makes code available in the next run of LaTeX
\cs_new:Npn \__cache_cache:n #1
{
\tl_set:Nn \l_tmpa_tl
{
\csname tl_gput_right:cn \endcsname { g__cache_lazy_auxfile_tl } { #1 }
}
\regex_replace_all:nnN { \cP\# } { \cO\# } \l_tmpa_tl
\__cache_put_aux:V \l_tmpa_tl
}
%%% This handles loading either the cached definitions
%%% from last run or executing the lazy definitions from the current run
%%% after loading the aux file
\hook_gput_code:nnn { begindocument } { cache }
{
\int_compare:nNnTF
\g__cache_cache_version_aux_int < \g__cache_cache_version_document_int
{
\hook_use:n { __cache / lazy / document }
}
{
\hook_use:n { __cache / lazy / auxfile }
}
}
}
{
\cs_set_eq:NN \__cache_lazy:n \use:n
\cs_set_eq:NN \__cache_cache:n \use_none:n
}
\cs_new:Npn \__cached_new_document_command:nnn #1 #2 #3
{
\NewDocumentCommand{#1}{#2}{#3}
\__cache_cache:n
{
\csname __cached_new_document_command:nnn \endcsname { #1 } { #2 } { #3 }
}
}
\NewDocumentCommand{\DeclareCachedDocumentCommand}{mmm}
{
\__cache_lazy:n
{
\__cached_new_document_command:nnn{#1}{#2}{#3}
}
}