Skip to content

Commit

Permalink
tweak and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszKrajewski committed May 6, 2023
1 parent 5645a2d commit 6e47490
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 0.0.6 (2023/05/06)
## 0.0.7 (2023/05/06)
* CHANGED: build process
* ADDED: GitHub actions
* FIXED: Minor bug in AliveKeeper (sometimes actions were called with not items at all)
* CHANGED: Minor optimization in AliveKeeper (removed unnecessary allocations)

## 0.0.5 (2021/04/11)
* ADDED: ITimeSource to ManualResetSignal
Expand Down
14 changes: 10 additions & 4 deletions src/K4os.Async.Toys/AliveKeeper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using K4os.Async.Toys.Internal;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

Expand Down Expand Up @@ -126,7 +123,16 @@ private static IAliveKeeperSettings Validate(IAliveKeeperSettings settings) =>
private static T Pass(T x) => x;
private bool IsActive(T item) => _items.ContainsKey(item);
private bool IsDisposing => _cancel.IsCancellationRequested;
private T[] ActiveOnly(IEnumerable<T> items) => items.Where(IsActive).ToArray();

private T[] ActiveOnly(T[] items)
{
// this method is optimized for the fact that most of the time
// all of them will be active, so same array can be returned
return items.Length <= 0 || items.All(IsActive)
? items
: items.Where(IsActive).ToArray();
}

private void Deactivate(T item) => _items.TryRemove(item, out _);
private bool TryActivate(T item) => _items.TryAdd(item, null);

Expand Down

0 comments on commit 6e47490

Please sign in to comment.