@@ -10,63 +10,94 @@ internal sealed class EventIdPropertyCache
10
10
{
11
11
private readonly object _createLock = new ( ) ;
12
12
private readonly int _maxCapacity ;
13
- private readonly Dictionary < int , LogEventProperty > _propertyCache ;
13
+ private readonly Dictionary < EventKey , LogEventProperty > _propertyCache ;
14
14
15
15
private int count ;
16
16
17
17
public EventIdPropertyCache ( int maxCapacity )
18
18
{
19
- this . _maxCapacity = maxCapacity ;
20
- this . _propertyCache = new Dictionary < int , LogEventProperty > ( capacity : maxCapacity ) ;
19
+ _maxCapacity = maxCapacity ;
20
+ _propertyCache = new Dictionary < EventKey , LogEventProperty > ( capacity : maxCapacity ) ;
21
21
}
22
22
23
23
public LogEventProperty GetOrCreateProperty ( in EventId eventId )
24
24
{
25
- if ( _propertyCache . TryGetValue ( eventId . Id , out var cachedProperty ) )
25
+ var eventKey = new EventKey ( eventId ) ;
26
+
27
+ if ( _propertyCache . TryGetValue ( eventKey , out var cachedProperty ) )
26
28
{
27
29
return cachedProperty ;
28
30
}
29
31
30
32
lock ( _createLock )
31
33
{
32
- return GetOrCreateSynchronized ( in eventId ) ;
34
+ // Double check under lock
35
+ if ( _propertyCache . TryGetValue ( eventKey , out cachedProperty ) )
36
+ {
37
+ return cachedProperty ;
38
+ }
39
+
40
+ cachedProperty = CreateCore ( in eventKey ) ;
41
+
42
+ if ( count < _maxCapacity )
43
+ {
44
+ _propertyCache [ eventKey ] = cachedProperty ;
45
+ count ++ ;
46
+ }
47
+
48
+ return cachedProperty ;
33
49
}
34
50
}
35
51
36
- private static LogEventProperty CreateCore ( in EventId eventId )
52
+ private static LogEventProperty CreateCore ( in EventKey eventKey )
37
53
{
38
54
var properties = new List < LogEventProperty > ( 2 ) ;
39
55
40
- if ( eventId . Id != 0 )
56
+ if ( eventKey . Id != 0 )
41
57
{
42
- properties . Add ( new LogEventProperty ( "Id" , new ScalarValue ( eventId . Id ) ) ) ;
58
+ properties . Add ( new LogEventProperty ( "Id" , new ScalarValue ( eventKey . Id ) ) ) ;
43
59
}
44
60
45
- if ( eventId . Name != null )
61
+ if ( eventKey . Name != null )
46
62
{
47
- properties . Add ( new LogEventProperty ( "Name" , new ScalarValue ( eventId . Name ) ) ) ;
63
+ properties . Add ( new LogEventProperty ( "Name" , new ScalarValue ( eventKey . Name ) ) ) ;
48
64
}
49
65
50
66
return new LogEventProperty ( "EventId" , new StructureValue ( properties ) ) ;
51
67
}
52
68
53
- private LogEventProperty GetOrCreateSynchronized ( in EventId eventId )
69
+ private readonly struct EventKey : IEquatable < EventKey >
54
70
{
55
- // Double check under lock
56
- if ( _propertyCache . TryGetValue ( eventId . Id , out var cachedProperty ) )
71
+
72
+ public EventKey ( EventId eventId )
57
73
{
58
- return cachedProperty ;
74
+ Id = eventId . Id ;
75
+ Name = eventId . Name ;
59
76
}
60
77
61
- cachedProperty = CreateCore ( in eventId ) ;
78
+ public int Id { get ; }
62
79
63
- if ( count < _maxCapacity )
80
+ public string ? Name { get ; }
81
+
82
+ /// <inheritdoc />
83
+ public override int GetHashCode ( )
64
84
{
65
- _propertyCache [ eventId . Id ] = cachedProperty ;
66
- count ++ ;
85
+ unchecked
86
+ {
87
+ var hashCode = 17 ;
88
+
89
+ hashCode = ( hashCode * 397 ) ^ this . Id ;
90
+ hashCode = ( hashCode * 397 ) ^ ( this . Name ? . GetHashCode ( ) ?? 0 ) ;
91
+
92
+ return hashCode ;
93
+ }
67
94
}
68
-
69
- return cachedProperty ;
95
+
96
+ /// <inheritdoc />
97
+ public bool Equals ( EventKey other ) => this . Id == other . Id && this . Name == other . Name ;
98
+
99
+ /// <inheritdoc />
100
+ public override bool Equals ( object ? obj ) => obj is EventKey other && Equals ( other ) ;
70
101
}
71
102
}
72
103
}
0 commit comments