Skip to content

Commit

Permalink
Reduce allocations when creating a new UnleashContext (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmascitelli authored Nov 28, 2023
1 parent 1bf92a5 commit 93df4f8
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions src/Unleash/UnleashContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ namespace Unleash
{
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// A context which the feature request should be validated againt. Usually scoped to a web request through an implementation of IUnleashContextProvider.
/// A context which the feature request should be validated against. Usually scoped to a web request through an implementation of IUnleashContextProvider.
/// </summary>
public class UnleashContext
{
Expand All @@ -15,19 +14,35 @@ public class UnleashContext
public string SessionId { get; set; }
public string RemoteAddress { get; set; }
public DateTimeOffset? CurrentTime { get; set; }
public Dictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> Properties { get; set; }

public UnleashContext()
{
Properties = new Dictionary<string, string>();
}

public UnleashContext(string appName, string environment, string userId, string sessionId, string remoteAddress, DateTimeOffset? currentTime, Dictionary<string, string> properties)
{
AppName = appName;
Environment = environment;
UserId = userId;
SessionId = sessionId;
RemoteAddress = remoteAddress;
CurrentTime = currentTime;
Properties = properties;
}

public string GetByName(string contextName)
{
switch (contextName)
{
case "environment":
case "environment":
return Environment;
case "appName":
case "appName":
return AppName;
case "userId":
case "userId":
return UserId;
case "sessionId":
case "sessionId":
return SessionId;
case "remoteAddress":
return RemoteAddress;
Expand All @@ -40,19 +55,10 @@ public string GetByName(string contextName)

public UnleashContext ApplyStaticFields(UnleashSettings settings)
{
var builder = new Builder(this);
var environment = string.IsNullOrEmpty(Environment) ? settings.Environment : Environment;
var appName = string.IsNullOrEmpty(AppName) ? settings.AppName : AppName;

if (string.IsNullOrEmpty(Environment))
{
builder.Environment(settings.Environment);
}

if (string.IsNullOrEmpty(AppName))
{
builder.AppName(settings.AppName);
}

return builder.Build();
return new UnleashContext(appName, environment, UserId, SessionId, RemoteAddress, CurrentTime, new Dictionary<string, string>(Properties));
}

internal static Builder New()
Expand All @@ -68,11 +74,11 @@ internal class Builder
private string sessionId;
private string remoteAddress;
private DateTimeOffset? currentTime;
private readonly Dictionary<string, string> properties = new Dictionary<string, string>();
private readonly Dictionary<string, string> properties;

public Builder()
{

properties = new Dictionary<string, string>();
}

public Builder(UnleashContext context)
Expand All @@ -83,7 +89,7 @@ public Builder(UnleashContext context)
sessionId = context.SessionId;
remoteAddress = context.RemoteAddress;
currentTime = context.CurrentTime;
properties = context.Properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
properties = new Dictionary<string, string>(context.Properties);
}

public Builder AppName(string appName)
Expand Down Expand Up @@ -135,18 +141,7 @@ public Builder AddProperty(string name, string value)
}

public UnleashContext Build()
{
return new UnleashContext()
{
AppName = appName,
Environment = environment,
UserId = userId,
SessionId = sessionId,
RemoteAddress = remoteAddress,
Properties = properties,
CurrentTime = currentTime
};
}
=> new UnleashContext(appName, environment, userId, sessionId, remoteAddress, currentTime, properties);
}
}
}

0 comments on commit 93df4f8

Please sign in to comment.