Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve notification filter performance #15610

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}

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,5 @@
using System.Text.Encodings.Web;
using Cysharp.Text;
using Microsoft.AspNetCore.Html;

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

public class NotifyEntry
{
private HtmlEncoder _htmlEncoder;
private string _encodedMessage;

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

public string ToString(HtmlEncoder htmlEncoder)
{
// 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

if (_encodedMessage != null && _htmlEncoder == htmlEncoder)
{
return _encodedMessage;
}

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

_htmlEncoder = htmlEncoder;
return _encodedMessage = stringWriter.ToString();
}
}
}
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.ToString(_htmlEncoder) == y.ToString(_htmlEncoder);
}

public int GetHashCode(NotifyEntry obj)
{
return obj.GetMessageAsString(_htmlEncoder).GetHashCode() & 23 * obj.Type.GetHashCode();
return HashCode.Combine(obj.ToString(_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.ToString(_htmlEncoder) }
};

o.WriteTo(writer);
}
Expand Down

This file was deleted.

Loading
Loading