Skip to content

Commit 84b5708

Browse files
Fix doc links
1 parent 581046a commit 84b5708

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ pack: build
5454
push:
5555
dotnet nuget push $(ARTIFACT_PATH)/*.nupkg -s $(NUGET_SOURCE) -k $(NUGET_API_KEY)
5656

57-
generate-docs: build
57+
generate-docs: restore
58+
dotnet build -c Release
5859
dotnet docfx $(DOCS_PATH)/docfx.json --warningsAsErrors true
5960

6061
serve-docs: generate-docs

docs/shape-providers.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ For example, serializers may need to jump from a type shape to its converter.
172172

173173
[!code-csharp[](CSharpSamples/AssociatedTypes.cs#TypeShapeOneType)]
174174

175-
Use the @PolyType.ITypeShape.GetAssociatedTypeShape*?displayProperty=nameWithType method to obtain the shape of an associated type.
176-
The @PolyType.SourceGenModel.SourceGenTypeShapeProvider implementation of this method requires that the associated types be pre-determined at compile time via attributes.
177-
The @PolyType.ReflectionProvider.ReflectionTypeShapeProvider is guaranteed to work with all types and therefore does _not_ require these attributes.
175+
Use the <xref:PolyType.ITypeShape.GetAssociatedTypeShape*> method to obtain the shape of an associated type.
176+
The <xref:PolyType.SourceGenModel.SourceGenTypeShapeProvider> implementation of this method requires that the associated types be pre-determined at compile time via attributes.
177+
The <xref:PolyType.ReflectionProvider.ReflectionTypeShapeProvider> is guaranteed to work with all types and therefore does _not_ require these attributes.
178178
Thus, it can be valuable to test your associated types code with the source generation provider to ensure your code is AOT-compatible.
179179

180180
An associated type must have at least `internal` visibility for its shape to be generated for use within its same assembly.
@@ -190,20 +190,20 @@ This can be appropriate for a serializer that needs to jump from a generic data
190190

191191
[!code-csharp[](CSharpSamples/AssociatedTypes.cs#SerializerConverter)]
192192

193-
Only @PolyType.SourceGenModel.SourceGenTypeShapeProvider produces partial shapes.
194-
The @PolyType.ReflectionProvider.ReflectionTypeShapeProvider always produces complete shapes.
193+
Only <xref:PolyType.SourceGenModel.SourceGenTypeShapeProvider> produces partial shapes.
194+
The <xref:PolyType.ReflectionProvider.ReflectionTypeShapeProvider> always produces complete shapes.
195195

196-
At present, only @PolyType.Abstractions.IObjectTypeShape shapes are generated partially.
196+
At present, only <xref:PolyType.Abstractions.IObjectTypeShape> shapes are generated partially.
197197
Shapes for collections, enums, unions, etc. are generated as full shapes.
198198

199-
Type associations can be defined directly on the originating type via @PolyType.AssociatedTypeShapeAttribute.AssociatedTypes?displayProperty=nameWithType when that type and its associated type are in the same assembly.
200-
When the associated type is in another assembly, use @PolyType.TypeShapeExtensionAttribute.AssociatedTypes?displayProperty=nameWithType in the assembly that declares the associated type.
199+
Type associations can be defined directly on the originating type via <xref:PolyType.AssociatedTypeShapeAttribute.AssociatedTypes> when that type and its associated type are in the same assembly.
200+
When the associated type is in another assembly, use <xref:PolyType.TypeShapeExtensionAttribute.AssociatedTypes> in the assembly that declares the associated type.
201201
You can also define a custom attribute that can define associated types by attributing your own custom attribute with @PolyType.Abstractions.AssociatedTypeAttributeAttribute.
202202

203203
### TypeShapeExtensionAttribute
204204

205-
The @PolyType.TypeShapeExtensionAttribute is an assembly-level attribute.
206-
It is very similar to @PolyType.TypeShapeAttribute, but it is used to customize the generated shape for a type that your assembly does not declare.
205+
The <xref:PolyType.TypeShapeExtensionAttribute> is an assembly-level attribute.
206+
It is very similar to <xref:PolyType.TypeShapeAttribute>, but it is used to customize the generated shape for a type that your assembly does not declare.
207207

208208
### Polymorphic types
209209

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Collections.Concurrent;
3+
using System.Threading;
4+
5+
namespace PolyType.SourceGenModel;
6+
7+
/// <summary>
8+
/// Defines a thread-safe pool for reusing <see cref="System.Runtime.CompilerServices.StrongBox{T}"/> instances.
9+
/// </summary>
10+
public sealed class StrongBoxPool<T> where T : struct
11+
{
12+
// Bounded pool size to avoid unbounded retention of boxed struct values.
13+
private const int MaxPoolSize = 256;
14+
private readonly ConcurrentQueue<StrongBox<T>> _queue = new();
15+
private int _count; // number of items currently stored in the queue
16+
17+
/// <summary>Rents a <see cref="StrongBox{T}"/> whose <see cref="StrongBox{T}.Value"/> is initialized to <paramref name="value"/>.</summary>
18+
public StrongBox<T> Rent(T value)
19+
{
20+
if (_queue.TryDequeue(out StrongBox<T>? box))
21+
{
22+
Interlocked.Decrement(ref _count);
23+
box.Value = value; // overwrite previous value
24+
return box;
25+
}
26+
27+
return new(value);
28+
}
29+
30+
/// <summary>Returns a previously rented <see cref="StrongBox{T}"/> to the pool.</summary>
31+
public void Return(StrongBox<T> box)
32+
{
33+
if (box is null)
34+
{
35+
return;
36+
}
37+
38+
// Clear value to avoid holding onto large structs inadvertently.
39+
box.Value = default;
40+
41+
// Fast path: if we've already reached capacity just drop the box.
42+
if (Volatile.Read(ref _count) >= MaxPoolSize)
43+
{
44+
return; // let it be GC'ed
45+
}
46+
47+
_queue.Enqueue(box);
48+
// If we went over capacity due to a race we simply allow a slight overflow.
49+
Interlocked.Increment(ref _count);
50+
}
51+
}

tests/PolyType.Benchmarks/JsonBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static class JsonData
2525
public static readonly JsonConverter<MyPoco> PolyTypeSourceGen = JsonSerializerTS.CreateConverter<MyPoco>();
2626
}
2727

28-
[MemoryDiagnoser(false)]
28+
[MemoryDiagnoser]
2929
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
3030
public class JsonSerializeBenchmark
3131
{
@@ -80,7 +80,7 @@ public void Reset()
8080
}
8181
}
8282

83-
[MemoryDiagnoser(false)]
83+
[MemoryDiagnoser()]
8484
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
8585
public class JsonDeserializeBenchmark
8686
{

0 commit comments

Comments
 (0)