From e79f34d859d48be485589f19fc6905afa5872a53 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 29 Apr 2013 14:52:53 +0200 Subject: only cache ``name:`` lookups --- luaotfload-database.lua | 72 +++++++++++++++--------------------------- luaotfload-features.lua | 3 +- luaotfload.dtx | 18 ++--------- tests/pln-request-5-cached.tex | 3 +- 4 files changed, 32 insertions(+), 64 deletions(-) diff --git a/luaotfload-database.lua b/luaotfload-database.lua index 4aa966a..abf06a2 100644 --- a/luaotfload-database.lua +++ b/luaotfload-database.lua @@ -240,6 +240,7 @@ local load_names local read_fonts_conf local reload_db local resolve +local resolve_cached local save_names local scan_external_dir local update_names @@ -380,22 +381,6 @@ TODO: 9) ??? n) PROFIT!!! ---doc]]-- - ---- the resolver is called after the font request is parsed ---- this is where we insert the cache -local normal_resolver = fonts.definers.resolve -local dummy_resolver = function (specification) - --- this ensures that the db is always loaded - --- before a lookup occurs - if not names.data then names.data = load_names() end - --inspect(specification) - local resolved = normal_resolver(specification) - --inspect(resolved) - return resolved -end - ---[[doc-- The name lookup requires both the “name” and some other keys, so we’ll concatenate them. The spec is modified in place (ugh), so we’ll have to catalogue what @@ -428,33 +413,29 @@ We’ll just cache a deep copy of the entire spec as it leaves the resolver, lest we want to worry if we caught all the details. --doc]]-- ---- spec -> spec -local cached_resolver = function (specification) +--- 'a -> 'a -> table -> (string * int|boolean * boolean) +resolve_cached = function (_, _, specification) if not names.data then names.data = load_names() end local request_cache = names.data.request_cache local request = specification.specification - report("log", 4, "cache", - "looking for “%s” in cache ...", + report("log", 4, "cache", "looking for “%s” in cache ...", request) + local found = names.data.request_cache[request] + + --- case 1) cache positive ---------------------------------------- if found then --- replay fields from cache hit report("info", 4, "cache", "found!") - for i=1, #cache_fields do - local f = cache_fields[i] - if found[f] then specification[f] = found[f] end - end - return specification + return found[1], found[2], true end report("log", 4, "cache", "not cached; resolving") + --- case 2) cache negative ---------------------------------------- --- first we resolve normally ... - local resolved_spec = normal_resolver(specification) - --- ... then we add the fields to the cache - local entry = { } - for i=1, #cache_fields do - local f = cache_fields[i] - entry[f] = resolved_spec[f] - end + local filename, subfont, success = resolve(nil, nil, specification) + if not success then return filename, subfont, false end + --- ... then we add the fields to the cache ... ... + local entry = { filename, subfont } report("log", 4, "cache", "new entry: %s", request) names.data.request_cache[request] = entry @@ -463,21 +444,13 @@ local cached_resolver = function (specification) --- whenever the cache is updated. --- TODO this should trigger a save only once the --- document is compiled (finish_pdffile callback?) + --- TODO we should speed up writing by separating + --- the cache from the db report("log", 5, "cache", "saving updated cache") save_names() - return resolved_spec + return filename, subfont, true end -local resolvers = { - dummy = dummy_resolver, - normal = normal_resolver, - cached = cached_resolver, -} - -fonts.definers.resolve = resolvers[config.luaotfload.resolver] ---fonts.definers.resolve = resolvers.cached ---fonts.definers.resolve = resolvers.dummy - --[[doc-- Luatex-fonts, the font-loader package luaotfload imports, comes with @@ -1390,9 +1363,16 @@ names.update = update_names names.crude_file_lookup = crude_file_lookup names.crude_file_lookup_verbose = crude_file_lookup_verbose -names.resolve = resolve --- replace the resolver from luatex-fonts -names.resolvespec = resolve -names.find_closest = find_closest +--- replace the resolver from luatex-fonts +if config.luaotfload.resolver == "cached" then + report("info", 0, "cache", "caching of name: lookups active") + names.resolve = resolve_cached + names.resolvespec = resolve_cached +else + names.resolve = resolve + names.resolvespec = resolve +end +names.find_closest = find_closest --- dummy required by luatex-fonts (cf. luatex-fonts-syn.lua) diff --git a/luaotfload-features.lua b/luaotfload-features.lua index b11bf22..6cbfdf4 100644 --- a/luaotfload-features.lua +++ b/luaotfload-features.lua @@ -416,7 +416,8 @@ local font_request = Ct(path_lookup * (colon^-1 * features)^-1 local import_values = { --- That’s what the 1.x parser did, not quite as graciously, --- with an array of branch expressions. - "style", "optsize", "lookup", "sub" --[[‽]], "mode", + -- "style", "optsize",--> from slashed notation; handled otherwise + "lookup", "sub" --[[‽]], "mode", } local lookup_types = { "anon", "file", "name", "path" } diff --git a/luaotfload.dtx b/luaotfload.dtx index abc737c..6392c64 100644 --- a/luaotfload.dtx +++ b/luaotfload.dtx @@ -540,8 +540,8 @@ and the derived files % \end{quote} % % \noindent -% If style modifiers are present (\XETEX style), they must precede \meta{font -% features}. +% If style modifiers are present (\XETEX style), they must precede +% \meta{font features}. % % The element \meta{font features} is a semicolon-separated list of feature % tags\footnote{% @@ -1532,19 +1532,6 @@ loadmodule"database.lua" --- “font-nms” loadmodule"colors.lua" --- “font-clr” % \end{macrocode} -% This hack makes fonts called with file method found by fonts.names.resolve -% instead of just trying to find them with \identifier{kpse}. -% It is necessary in cases when font files are not reachable by -% \identifier{kpse} but present in the database, a quite common case -% under Linux. -% -% As of 2013-04-24 we have a workaround in the resolver that handles -% \verb|file:| lookups diverted this way. -% It requires some overhead due to additional extra data saved in the -% names database, and might vanish entirely once the font request syntax -% is understood. -% Until then it is considered a kludge, like the hack below. -% % Relying on the \verb|name:| resolver for everything has been the source % of permanent trouble with the database. % With the introduction of the new syntax parser we now have enough @@ -1715,7 +1702,6 @@ local patch_defined_font = function (specification, size, id) if type(tfmdata) == "table" then call_callback("luaotfload.patch_font", tfmdata) end - -- inspect(table.keys(tfmdata)) return tfmdata end diff --git a/tests/pln-request-5-cached.tex b/tests/pln-request-5-cached.tex index 437b20d..8ba4a5e 100644 --- a/tests/pln-request-5-cached.tex +++ b/tests/pln-request-5-cached.tex @@ -1,6 +1,7 @@ \ifdefined\directlua \directlua{config = config or { luaotfload = { } } - config.luaotfload.resolver = "cached" } + config.luaotfload.resolver = "cached" + config.luaotfload.loglevel = 5 } \input luaotfload.sty \fi -- cgit v1.2.3