-
Notifications
You must be signed in to change notification settings - Fork 546
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
Add caching to DrawShapedText #3033
base: main
Are you sure you want to change the base?
Conversation
/azp run |
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
Very nice one!! Possible to avoid Maybe add a |
a12cfea
to
32fb1e3
Compare
Is it possible to use https://learn.microsoft.com/dotnet/api/system.runtime.caching.memorycache Not sure if this is a good thing or not? Do people use this API? Is this sufficient? |
/azp run |
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
I didn't want to introduce another dependency. But I can try it and see what the benchmarks say. A |
32fb1e3
to
cd12eef
Compare
I tested
Both make it slower than the Here are the benchmarks with the cache variants:
The project with the benchmarks for the various variants is on https://github.com/MichaelRumpler/SkiaTextRendering. As you can see the simple |
In comparsion with the Anyway these are my tests results when comparing different thread-safe dictionary access methods using max. 8 threads:
However, it depends on the Btw. find my tests here, if you want. When it's only about a few items, |
/azp run |
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
shaper = shaperCache.TryGetValue(key, out var value) | ||
? value.shaper | ||
: new SKShaper(typeface); | ||
|
||
shaperCache[key] = (shaper, DateTime.Now); // update timestamp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this not be done in a single step using GetOrAdd
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how GetOrAdd
could help, but I tried AddOrUpdate
here:
var newTuple = shaperCache.AddOrUpdate(key,
_ => (new SKShaper(typeface), DateTime.Now),
(_, old) => (old.shaper, DateTime.Now));
But the simple Dictionary
with a lock
is still faster.
lock (shaperCache)
{
shaper = shaperCache.TryGetValue(key, out var value)
? value.shaper
: new SKShaper(typeface);
shaperCache[key] = (shaper, DateTime.Now); // update timestamp
}
Description of Change
This enables a user to set a cache duration. When set to a value > 0, it caches the
SKShaper
and theSKShaper.Result
thus repeated calls with the same font and/or text are faster.Bugs Fixed
API Changes
Added:
public static void SetShaperCacheDuration(this SKCanvas canvas, uint milliseconds)
Behavioral Changes
If the cache duration was set to a value bigger than zero,
DrawShapedText
will cache theSKShaper
and theSKShaper.Result
for that amount of milliseconds. Therefore repeated calls with the sameSKFont
and/orstring
will be faster.If the cache duration is set to 0, then the cache will be cleared, all contained
SKShaper
objects disposed and caching will be disabled.The default cache duration is 0. So if
SetShaperCacheDuration
is never called,DrawShapedText
works as before.Required skia PR
None.
PR Checklist