Skip to content

Commit b240679

Browse files
committed
Pin the refs for faster access
1 parent 0faf614 commit b240679

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/ImageSharp/Formats/Webp/Lossless/Vp8LBackwardRefs.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,33 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless;
88

99
internal class Vp8LBackwardRefs : IDisposable
1010
{
11-
private readonly IMemoryOwner<PixOrCopy> refs;
11+
private readonly IMemoryOwner<PixOrCopy> owner;
12+
private readonly MemoryHandle handle;
1213
private int count;
1314

1415
public Vp8LBackwardRefs(MemoryAllocator memoryAllocator, int pixels)
1516
{
16-
this.refs = memoryAllocator.Allocate<PixOrCopy>(pixels);
17+
this.owner = memoryAllocator.Allocate<PixOrCopy>(pixels);
18+
this.handle = this.owner.Memory.Pin();
1719
this.count = 0;
1820
}
1921

20-
public void Add(PixOrCopy pixOrCopy) => this.refs.Memory.Span[this.count++] = pixOrCopy;
22+
public void Add(PixOrCopy pixOrCopy)
23+
{
24+
unsafe
25+
{
26+
((PixOrCopy*)this.handle.Pointer)[this.count++] = pixOrCopy;
27+
}
28+
}
2129

2230
public void Clear() => this.count = 0;
2331

24-
public Span<PixOrCopy>.Enumerator GetEnumerator() => this.refs.Slice(0, this.count).GetEnumerator();
32+
public Span<PixOrCopy>.Enumerator GetEnumerator() => this.owner.Slice(0, this.count).GetEnumerator();
2533

2634
/// <inheritdoc/>
27-
public void Dispose() => this.refs.Dispose();
35+
public void Dispose()
36+
{
37+
this.handle.Dispose();
38+
this.owner.Dispose();
39+
}
2840
}

0 commit comments

Comments
 (0)