Skip to content

Commit

Permalink
Improve notification filter performance (#15610)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Mar 28, 2024
1 parent a3f5637 commit ec3fa06
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 262 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Cysharp.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.Extensions.Options;
using OrchardCore.Abstractions.Pooling;
using OrchardCore.DisplayManagement.Implementation;
using OrchardCore.Environment.Cache;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Cysharp.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.TagHelpers.Cache;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Options;
using OrchardCore.Abstractions.Pooling;
using OrchardCore.Environment.Cache;

namespace OrchardCore.DynamicCache.TagHelpers
Expand Down
207 changes: 0 additions & 207 deletions src/OrchardCore/OrchardCore.Abstractions/Pooling/ZStringWriter.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Cysharp.Text;
using GraphQL;
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Abstractions.Pooling;
using OrchardCore.Apis.GraphQL;
using OrchardCore.ContentManagement.Display;
using OrchardCore.ContentManagement.GraphQL.Options;
Expand Down
17 changes: 11 additions & 6 deletions src/OrchardCore/OrchardCore.DisplayManagement/Notify/Notifier.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.Logging;

namespace OrchardCore.DisplayManagement.Notify
{
public class Notifier : INotifier
{
private readonly IList<NotifyEntry> _entries;
private readonly List<NotifyEntry> _entries;
private readonly ILogger _logger;

public Notifier(ILogger<Notifier> logger)
Expand All @@ -16,17 +17,21 @@ public Notifier(ILogger<Notifier> logger)
_logger = logger;
}

[Obsolete("This method will be removed in a later version. Use AddAsync()")]
// TODO The implementation for this is provided as an interface default implementation
// when the interface method is removed, replace this with AddAsync.
public void Add(NotifyType type, LocalizedHtmlString message)
{
throw new System.NotImplementedException();
}

public ValueTask AddAsync(NotifyType type, LocalizedHtmlString message)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Notification '{NotificationType}' with message '{NotificationMessage}'", type, message.Value);
_logger.LogInformation("Notification '{NotificationType}' with message '{NotificationMessage}'", type, message.ToString());
}

_entries.Add(new NotifyEntry { Type = type, Message = message });

return ValueTask.CompletedTask;
}

public IList<NotifyEntry> List()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Text.Encodings.Web;
using System.Web;
using Cysharp.Text;
using Microsoft.AspNetCore.Html;

namespace OrchardCore.DisplayManagement.Notify
Expand All @@ -12,7 +15,40 @@ public enum NotifyType

public class NotifyEntry
{
private (HtmlEncoder HtmlEncoder, string Message) _cache;

public NotifyType Type { get; set; }
public IHtmlContent Message { get; set; }

public string ToHtmlString(HtmlEncoder htmlEncoder)
{
// When the object is created from a cookie the message
// is an HtmlString so we can use this instead of using
// the TextWriter path.

if (Message is IHtmlString htmlString)
{
return htmlString.ToHtmlString();
}

// Cache the encoded version for the specified encoder.
// This is necessary as long as there will be string-based comparisons
// and the need of NotifyEntryComparer

var cache = _cache;

if (cache.Message != null && cache.HtmlEncoder == htmlEncoder)
{
return cache.Message;
}

using var stringWriter = new ZStringWriter();
Message.WriteTo(stringWriter, htmlEncoder);
stringWriter.Flush();

_cache = cache = new(htmlEncoder, stringWriter.ToString());

return cache.Message;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;

Expand All @@ -14,12 +15,12 @@ public NotifyEntryComparer(HtmlEncoder htmlEncoder)

public bool Equals(NotifyEntry x, NotifyEntry y)
{
return x.Type == y.Type && x.GetMessageAsString(_htmlEncoder) == y.GetMessageAsString(_htmlEncoder);
return x.Type == y.Type && x.ToHtmlString(_htmlEncoder) == y.ToHtmlString(_htmlEncoder);
}

public int GetHashCode(NotifyEntry obj)
{
return obj.GetMessageAsString(_htmlEncoder).GetHashCode() & 23 * obj.Type.GetHashCode();
return HashCode.Combine(obj.ToHtmlString(_htmlEncoder), obj.Type);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
Expand Down Expand Up @@ -48,18 +46,11 @@ public override void Write(Utf8JsonWriter writer, NotifyEntry value, JsonSeriali
return;
}

var o = new JsonObject();

// Serialize the message as it's an IHtmlContent
var stringBuilder = new StringBuilder();
using (var stringWriter = new StringWriter(stringBuilder))
var o = new JsonObject
{
notifyEntry.Message.WriteTo(stringWriter, _htmlEncoder);
}

// Write all well-known properties
o.Add(nameof(NotifyEntry.Type), notifyEntry.Type.ToString());
o.Add(nameof(NotifyEntry.Message), notifyEntry.GetMessageAsString(_htmlEncoder));
{ nameof(NotifyEntry.Type), notifyEntry.Type.ToString() },
{ nameof(NotifyEntry.Message), notifyEntry.ToHtmlString(_htmlEncoder) }
};

o.WriteTo(writer);
}
Expand Down
Loading

0 comments on commit ec3fa06

Please sign in to comment.