[
https://issues.apache.org/jira/browse/FOP-2651?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18147787#comment-18147787
]
Niklas commented on FOP-2651:
-----------------------------
A few months back we ran into issues with PNG rendering when increasing the
number of configured fonts to approx. 50. The setup is multiple long-lived
FopFactory instances in containers that render several thousand PNGs per hour
each. The +~JF temp copies piled up as in your analysis and the containers
reached their memory limit. Our observations:
* sharing fonts between renders is good, but sharing a temp copy in
java.io.tmpdir has issues (see 1.);
* the real cost, in time and in memory, is in FOP's own font work: not in the
temp copies and not in the AWT font (see 2.).
We have been working on a patch in this area as well (see 3.). Details:
*1. Sharing a font created from a stream*
{{AwtFontCache}} keeps one temp copy per font for the life of the JVM. If that
copy is deleted while the JVM keeps rendering, Java replaces the font with its
default font for good ({{{}SunFontManager.deRegisterBadFont{}}}), and every
later render draws text in the wrong font without any error. We had two
situations where this would happen:
* a graceful shutdown (for example Spring Boot) is still letting the running
jobs finish, but the FileCloser shutdown hook has already deleted all temp font
files on SIGTERM
* a tmp cleaner deletes files older than a few days.
We went down the same path first: our first version shared the loaded fonts,
still created from streams, so in effect it cached the temp copies like
{{{}AwtFontCache{}}}. But after a SIGTERM, every later render in that JVM drew
the default font.
Reproduced on main (fef45a4e4): one long-lived FopFactory, PNG output with
large text, rendering continued for 3 s after SIGTERM, every render compared
byte for byte with a reference rendered at startup, 3 runs per variant:
||Variant||renders after SIGTERM||rendered with the default font||
|main|568|0|
|main + FOP-2651.patch|699|694|
|File instead of InputStream|740|0|
|fonts shared between renders (see 3.) |1361|0|
Main survives because every render creates new temp copies after the shutdown
hook has run; only a render in progress at that moment can break.
So for {{file:}} fonts we would suggest {{createFont(int, File)}} rather than
sharing the copy, and a stream only for other sources. Your
{{PCLRepeatedRenderFontSoak}} passes unchanged with only that change, without a
cache (main: 9 new temp files in renders 2..10, 27 after 200 renders). The
cache also reads and hashes the whole font file on every render to build its
key.
*2. The real cost*
There are two separate problems:
* the temp copies pile up over time until disk or memory runs out. Passing a
File removes them completely;
* FOP loads all configured fonts on every render. That is where the time and
memory per render go, and neither the AWT cache nor a {{File}} changes it.
Measured on main (fef45a4e4), one long-lived FopFactory, single thread, PNG
output, 30 configured fonts, average of two passes:
||Variant||docs/s||MB allocated per document||temp copies left||
|main|11.4|122|hundreds|
|main + FOP-2651.patch|14.4|155|30|
|File instead of InputStream|17.0|122|0|
|fonts shared between renders (see 3.)|102.8|5.3|0|
The File change removes all temp copies, yet throughput only rises from 11.4 to
17.0 docs/s and the allocation stays at 122 MB. Only sharing the loaded fonts
removes the cost. With 4 and 8 fonts, and with a new FopFactory per render, the
picture is the same.
On the Font2D per render: agreed that {{createFont(int, File)}} still creates
one per font and render, but it holds no file, and the JDK's channel pool
({{{}fontFileCache{}}}, 20 slots) is bounded, so nothing piles up.
*3. Sharing the loaded fonts*
We are preparing a patch (to follow in next days) that adds {{cache-fonts}} on
{{<fonts>}} for the PNG and TIFF renderers. It loads the configured fonts once
per FopFactory and output format and shares them between renders, including the
AWT fonts, and gives {{file:}} fonts to Java as files. It also contains the
same {{getFont(int)}} fix as your patch (points vs. millipoints).
PCL is not covered by the sharing, but the File change does apply to PCL and
AWT, since they load fonts through {{{}ConfiguredFontCollection{}}}.
> 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)