Skip to content

Commit 75bbac2

Browse files
A bit of cleanup around the new WalkSet method.
1 parent 18c0ea5 commit 75bbac2

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

CacheImplBase.cs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,6 @@ to the Add method. */
124124
/// </remarks>
125125
protected abstract int ReplacementOffset { get; }
126126

127-
/// <summary>
128-
/// Return an enumerable container of indices into the value array from a set in the key array.
129-
/// </summary>
130-
/// <param name="set">The set to operate on.</param>
131-
/// <remarks>
132-
/// Generalizing enumeration of sets this way is actually slower -- notably, but not excessively --
133-
/// than copying the loop to all the places in the class that need to enumerate sets. I'll stay
134-
/// with this trade-off for now since it gives me some flexibility to modify the implementation
135-
/// later.
136-
/// </remarks>
137-
protected IEnumerable<int> GetSetPointerIndices(int set) {
138-
/* Get the first array index for the set; in other words, where in the array does the set start? */
139-
var setBegin = set * ways_;
140-
141-
int setOffset; // Offset into the set in the index array for where the key is stored
142-
int pointerIndex; // Actual array location in the key array for setOffset
143-
144-
/* Loop over the set, incrementing both the set offset (setOffset) and the pointer-array index (pointerIndex) */
145-
for (setOffset = 0, pointerIndex = setBegin; setOffset < ways_; ++setOffset, ++pointerIndex) {
146-
yield return pointerIndex;
147-
}
148-
}
149-
150127
/// <summary>
151128
/// Adds an element with the provided <paramref name="key"/> and <paramref name="value"/>
152129
/// to the ParksComputing.ISetAssociativeCache if the <paramref name="key"/> is not found.
@@ -167,7 +144,7 @@ protected void AddOrUpdate(TKey key, TValue value, Action<TKey, TValue, int, int
167144

168145
/// <remarks>
169146
/// We don't use the WalkSet method here because that introduces just enough extra
170-
/// logic to make this code path about 33% slower, and that's too much of a slow-down
147+
/// logic to make this code path about 30% slower, and that's too much of a slow-down
171148
/// even if it is still really fast in absolute terms.
172149
/// </remarks>
173150

@@ -395,11 +372,6 @@ public virtual bool Remove(TKey key) {
395372
public virtual bool Remove(KeyValuePair<TKey, TValue> item) {
396373
var set = FindSet(item.Key);
397374

398-
/* Get the first array index for the set; in other words, where in the array does the
399-
set start? */
400-
var setBegin = set * ways_;
401-
var setEnd = setBegin + ways_;
402-
403375
return WalkSet(set, (set, pointerIndex) => {
404376
int valueIndex = pointerArray_[pointerIndex].Key;
405377

@@ -421,6 +393,16 @@ set start? */
421393
});
422394
}
423395

396+
/// <summary>
397+
/// Walks over each element of a set in the key array and calls a delegate for each
398+
/// element, passing the set and the key-array index corresponding to the current
399+
/// element to the delegate. If the delegate returns <c>true</c>, the iteration stops.
400+
/// </summary>
401+
/// <param name="set">The set to iterate over.</param>
402+
/// <param name="func">The delegate to call for each element.</param>
403+
/// <returns>
404+
/// <c>true</c> if the delegate returns <c>true</c> at any time; <c>false</c> otherwise.
405+
/// </returns>
424406
protected bool WalkSet(int set, Func<int, int, bool> func) {
425407
/* Get the first array index for the set; in other words, where in the array does the
426408
set start? */
@@ -437,6 +419,27 @@ set start? */
437419
return false;
438420
}
439421

422+
/// <summary>
423+
/// Return an enumerable container of indices into the value array from a set in the key array.
424+
/// </summary>
425+
/// <param name="set">The set to operate on.</param>
426+
/// <remarks>
427+
/// Compare this to the <c>WalkSet</c> method. Generalizing enumeration of sets this way is
428+
/// noticeably slower than <c>WalkSet</c>, probably because of all the code that is
429+
/// generated behind the scenes. I left it here in case it still may serve some purpose,
430+
/// </remarks>
431+
protected IEnumerable<int> GetSetPointerIndices(int set) {
432+
/* Get the first array index for the set; in other words, where in the array does the set start? */
433+
var setBegin = set * ways_;
434+
435+
int setOffset; // Offset into the set in the index array for where the key is stored
436+
int pointerIndex; // Actual array location in the key array for setOffset
437+
438+
/* Loop over the set, incrementing both the set offset (setOffset) and the pointer-array index (pointerIndex) */
439+
for (setOffset = 0, pointerIndex = setBegin; setOffset < ways_; ++setOffset, ++pointerIndex) {
440+
yield return pointerIndex;
441+
}
442+
}
440443

441444
/// <summary>
442445
/// Gets an System.Collections.Generic.ICollection containing the keys of the ParksComputing.ISetAssociativeCache.

0 commit comments

Comments
 (0)