[ 
https://issues.apache.org/jira/browse/FOP-2651?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151781#comment-18151781
 ] 

Niklas commented on FOP-2651:
-----------------------------

As announced in my previous comment, I have prepared a patch/PR ([Pull Request 
#138 ยท 
apache/xmlgraphics-fop|https://github.com/apache/xmlgraphics-fop/pull/138]):

{*}Share loaded fonts between renderings in the PNG and TIFF renderers 
(cache-fonts){*}:
 * Local {{file:}} fonts are given to Java as files ({{{}Font.createFont(int, 
File){}}}), so Java makes no temporary copies. This applies with or without 
{{{}cache-fonts{}}}, to all renderers that use 
{{{}ConfiguredFontCollection{}}}. Fonts from other sources, or all fonts when 
the application uses its own resource resolver, are still read as a stream, and 
a warning is logged when they are cached.
 * A new attribute {{cache-fonts}} on {{<fonts>}} (PNG and TIFF, default 
{{{}false{}}}) loads the configured fonts once per {{FopFactory}} and output 
format and shares them between renderings:
{code:xml}
<renderer mime="image/png">
  <fonts cache-fonts="true">
    <font embed-url="fonts/DejaVuSerif.ttf"><font-triplet name="DejaVu Serif" 
style="normal" weight="normal"/></font>
  </fonts>
</renderer>
{code}

h3. How the cache works
 * *Per-rendering state:* each rendering gets its own copy of the per-request 
state (font size, event listener).
 * *Full embedding mode:* the shared fonts are always loaded in full embedding 
mode. Subsetting records the first use of each glyph in maps that are not 
thread-safe, and bitmap output does not subset anyway.
 * *Thread safety:* the code paths that become shared are made thread-safe: 
{{{}Typeface{}}}, {{{}SingleByteFont{}}}, {{{}MultiByteFont{}}}, the 
{{CustomFont}} cmap (copy-on-write for shared fonts only), {{{}GlyphTable{}}}, 
the script processors.
 * *Fonts replaced by Java:* Java silently replaces a font by its default font 
for good when it cannot read the font file at the moment it needs it. Every 
rendering checks the cached fonts for that (the glyph count changes), logs an 
error and loads them again, at most every 30 seconds.
 * *Memory:* all configured fonts are loaded when the cache is filled 
({{{}lazy-load{}}} is ignored) and kept for the lifetime of the 
{{{}FopFactory{}}}. A typical Latin font takes about 0.5 MB, large fonts more.
 * *Other renderers:* PDF, PostScript and the other renderers are not affected.

h3. Measurements

One one-page document, PNG output, one {{{}FopFactory{}}}, Java 17.0.15 in a 
container, {{newFop()}} synchronized. All variants render pixel-identical 
images.

Single thread, documents per second / MB allocated per document:
||Configured fonts||main||main, lazy-load||patch, without cache-fonts||patch, 
cache-fonts||
|4|55 / 13.2|56 / 13.2|61 / 13.1|90 / 5.3|
|8|38 / 23.3|51 / 13.2|52 / 23.2|98 / 5.3|
|30|11.6 / 122|52 / 13.2|16.1 / 122|97 / 5.3|
|30, via <directory>|11.4 / 123|not applicable|16.5 / 122|101 / 5.4|

8 threads, documents per second:
||Configured fonts||main||main, lazy-load||patch, without cache-fonts||patch, 
cache-fonts||
|4|148|205|206|385|
|8|68|205|121|351|
|30|13.8|212|20.1|393|
|30, via <directory>|12.7|not applicable|19.4|394|

Long run, 8 threads, 30 fonts via {{{}<directory>{}}}, container limited to 3 
GB:
 * *main, 30 minutes:* 13-15 documents per second, down to about 9 for 10 
minutes while a backlog of temporary copies was deleted. The copies in {{/tmp}} 
peaked at 123,352 files (53 GB), and the container stayed at its memory limit 
for minutes.
 * *patch with cache-fonts, 15 minutes:* about 400 documents per second 
(355,229 documents), no temporary copies, process memory 809 to 826 MB.

h3. Tests

New tests cover sharing, full embedding mode, the reload after Java replaced a 
font (including the 30-second limit and its log messages), a changed start 
number, file vs. stream loading, and missing-glyph events.

> Creation of CustomFontMetricsMapper using File instead InputStream
> ------------------------------------------------------------------
>
>                 Key: FOP-2651
>                 URL: https://issues.apache.org/jira/browse/FOP-2651
>             Project: FOP
>          Issue Type: Improvement
>            Reporter: Simone Rondelli
>            Priority: Minor
>         Attachments: FOP-2651.patch
>
>
> The constructor of {{CustomFontMetricMapper}} creates an instance of 
> {{java.awt.Font}} using an InputStream. This led to the creation of a temp 
> file with the content of the stream. This is repeated for every renderer for 
> every rendering.
> Passing a file would avoid the creation of such a temp file reducing the 
> overall rendering time.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to