-
-
Notifications
You must be signed in to change notification settings - Fork 48
Introduce the concept of a shared glyph cache #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JimBobSquarePants
merged 5 commits into
SixLabors:main
from
codymullins:shared-glyph-cache
May 26, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91e887c
Introduce the concept of a shared glyph cache
codymullins b96887f
Merge branch 'main' into shared-glyph-cache
JimBobSquarePants db79820
make shared glyph cache the default
codymullins 6e4148a
Simplify glyph cache initialization
codymullins d4f38dc
Regenerate TextMeasuring reference image
codymullins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/ImageSharp.Drawing/Processing/GlyphCacheDefaultsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| namespace SixLabors.ImageSharp.Drawing.Processing; | ||
|
|
||
| /// <summary> | ||
| /// Adds extensions that control whether glyph-outline caching is shared across all | ||
| /// <c>DrawText</c> calls on a canvas (per-canvas) rather than rebuilt per call. | ||
| /// </summary> | ||
| public static class GlyphCacheDefaultsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Sets whether canvases created from this configuration share a single glyph-outline cache | ||
| /// across every <c>DrawText</c> call (per-canvas), instead of using a private cache per call. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Sharing lets a glyph outline built once be reused by later text runs on the same canvas, | ||
| /// which can help text-heavy output, at the cost of sub-pixel-quantized reuse across calls. | ||
| /// Disabled by default. | ||
| /// </remarks> | ||
| /// <param name="configuration">The configuration to store the setting against.</param> | ||
| /// <param name="enabled"><see langword="true"/> to share the glyph cache per canvas.</param> | ||
| public static void SetSharedGlyphCache(this Configuration configuration, bool enabled) | ||
| { | ||
| Guard.NotNull(configuration, nameof(configuration)); | ||
| configuration.Properties[typeof(SharedGlyphCacheKey)] = enabled; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets whether canvases created from this configuration share a glyph-outline cache per canvas. | ||
| /// </summary> | ||
| /// <param name="configuration">The configuration to read the setting from.</param> | ||
| /// <returns><see langword="true"/> if sharing is enabled; otherwise <see langword="false"/> (the default).</returns> | ||
| internal static bool GetSharedGlyphCache(this Configuration configuration) | ||
| => configuration.Properties.TryGetValue(typeof(SharedGlyphCacheKey), out object? value) && value is true; | ||
|
JimBobSquarePants marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Dedicated marker type used as the Configuration.Properties key, mirroring how the | ||
| // pluggable drawing backend keys its entry on typeof(IDrawingBackend). | ||
| private sealed class SharedGlyphCacheKey | ||
| { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawTextSharedGlyphCache.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using SixLabors.Fonts; | ||
| using SixLabors.ImageSharp.Drawing.Processing; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
| using SixLabors.ImageSharp.Processing; | ||
|
|
||
| namespace SixLabors.ImageSharp.Drawing.Benchmarks.Drawing; | ||
|
|
||
| /// <summary> | ||
| /// Measures the per-canvas shared glyph-outline cache (opt-in via | ||
| /// <see cref="GlyphCacheDefaultsExtensions.SetSharedGlyphCache"/>) against the default | ||
| /// per-call cache. | ||
| /// <para> | ||
| /// Unlike <see cref="DrawTextRepeatedGlyphs"/> (a single <c>DrawText</c> call, where the | ||
| /// per-call cache already captures repeats), this draws <see cref="RunCount"/> separate | ||
| /// <c>DrawText</c> calls that reuse a common alphabet across runs - the cross-call reuse the | ||
|
JimBobSquarePants marked this conversation as resolved.
Outdated
|
||
| /// shared cache is designed to exploit. The baseline rebuilds each run's glyph outlines from | ||
| /// scratch; the shared variant reuses outlines built by earlier runs on the same canvas. | ||
| /// </para> | ||
| /// </summary> | ||
| [MemoryDiagnoser] | ||
| public class DrawTextSharedGlyphCache | ||
| { | ||
| public const int Width = 700; | ||
|
|
||
| // Vertical advance between runs. The canvas height is sized in Setup to fit every run so all | ||
| // text is actually rasterized (not culled off-canvas), keeping the on/off comparison fair. | ||
| private const float LineHeight = 20F; | ||
|
|
||
| private readonly Brush brush = Brushes.Solid(Color.Black); | ||
|
|
||
| private readonly DrawingOptions drawingOptions = new() | ||
| { | ||
| GraphicsOptions = new GraphicsOptions { Antialias = true } | ||
| }; | ||
|
|
||
| private Image<Rgba32> privateCacheImage; | ||
| private Image<Rgba32> sharedCacheImage; | ||
| private Font font; | ||
| private string[] runs; | ||
|
|
||
| [Params(50, 250, 1000)] | ||
| public int RunCount { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| this.font = SystemFonts.CreateFont("Arial", 16); | ||
|
|
||
| // Short runs drawn as separate DrawText calls. They share a common word pool so glyph | ||
| // outlines overlap heavily across runs, which is exactly what a per-canvas cache can reuse. | ||
| string[] words = ["The", "quick", "brown", "fox", "jumps", "over", "lazy", "dog", "Sphinx", "black", "quartz", "judge", "vow"]; | ||
| this.runs = new string[this.RunCount]; | ||
| for (int i = 0; i < this.RunCount; i++) | ||
| { | ||
| this.runs[i] = string.Join( | ||
| ' ', | ||
| words[i % words.Length], | ||
| words[(i + 3) % words.Length], | ||
| words[(i + 7) % words.Length], | ||
| words[(i + 11) % words.Length]); | ||
| } | ||
|
|
||
| // Two isolated configurations differing only in the opt-in flag. Cloning Configuration.Default | ||
| // gives an independent Properties bag (the same pattern used to scope SetDrawingBackend), so | ||
| // setting the flag on one clone does not affect the other or the global default. | ||
| Configuration privateConfig = Configuration.Default.Clone(); | ||
| privateConfig.SetSharedGlyphCache(false); | ||
|
|
||
| Configuration sharedConfig = Configuration.Default.Clone(); | ||
| sharedConfig.SetSharedGlyphCache(true); | ||
|
|
||
| // Tall enough to lay every run on its own line within the canvas bounds. | ||
| int height = 16 + (int)(this.RunCount * LineHeight); | ||
| this.privateCacheImage = new Image<Rgba32>(privateConfig, Width, height); | ||
| this.sharedCacheImage = new Image<Rgba32>(sharedConfig, Width, height); | ||
| } | ||
|
|
||
| [GlobalCleanup] | ||
| public void Cleanup() | ||
| { | ||
| this.privateCacheImage.Dispose(); | ||
| this.sharedCacheImage.Dispose(); | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true, Description = "Per-call glyph cache (default)")] | ||
| public void PrivateGlyphCache() | ||
| => this.privateCacheImage.Mutate(c => c.Paint(this.drawingOptions, this.DrawRuns)); | ||
|
|
||
| [Benchmark(Description = "Shared per-canvas glyph cache")] | ||
| public void SharedGlyphCache() | ||
| => this.sharedCacheImage.Mutate(c => c.Paint(this.drawingOptions, this.DrawRuns)); | ||
|
|
||
| private void DrawRuns(DrawingCanvas canvas) | ||
| { | ||
| float y = 8; | ||
| foreach (string run in this.runs) | ||
| { | ||
| RichTextOptions options = new(this.font) { Origin = new PointF(8, y) }; | ||
| canvas.DrawText(options, run, this.brush, null); | ||
| y += LineHeight; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.