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

Performance improvements #1011

Merged
merged 4 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -62,6 +62,6 @@ public static TranslationStatus Create(ContentData data, Schema schema, Language

private static bool IsValidValue(JsonValue? value)
{
return value != null && value.Value.Type != JsonValueType.Null;
return value != null && value.Value.Value != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ private DefaultValueFactory()

public static JsonValue CreateDefaultValue(IField field, Instant now, string partition)
{
Guard.NotNull(field);
Guard.NotNull(partition);
var args = new Args(now, partition);

return field.RawProperties.Accept(Instance, new Args(now, partition));
return field.RawProperties.Accept(Instance, args);
}

public JsonValue Visit(ArrayFieldProperties properties, Args args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ public ExcludeChangedTypes(IJsonSerializer serializer)

private bool IsChangedType(IField field, JsonValue source)
{
try
{
return !JsonValueValidator.IsValid(field, source, serializer);
}
catch
{
return true;
}
return !JsonValueValidator.IsValid(field, source, serializer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ namespace Squidex.Domain.Apps.Core.ConvertContent;

public sealed class ResolveLanguages : IContentFieldConverter
{
private readonly LanguagesConfig languages;
private readonly HashSet<string> languageCodes;
private readonly Dictionary<string, string[]> languagesWithFallbacks;

public bool ResolveFallback { get; init; }

public ResolveLanguages(LanguagesConfig languages, params Language[] filteredLanguages)
{
this.languages = languages;
HashSet<string> languageCodes;

if (filteredLanguages?.Length > 0)
{
Expand All @@ -36,6 +35,11 @@ public ResolveLanguages(LanguagesConfig languages, params Language[] filteredLan
{
languageCodes.Add(languages.Master);
}

languagesWithFallbacks =
languageCodes.ToDictionary(
language => language,
language => languages.GetPriorities(language).Where(l => l != language).ToArray());
}

public ContentFieldData? ConvertFieldAfter(IRootField field, ContentFieldData source)
Expand All @@ -47,20 +51,15 @@ public ResolveLanguages(LanguagesConfig languages, params Language[] filteredLan

if (ResolveFallback)
{
foreach (var languageCode in languageCodes)
foreach (var (languageCode, fallbacks) in languagesWithFallbacks)
{
if (source.TryGetNonNull(languageCode, out _))
{
continue;
}

foreach (var fallback in languages.GetPriorities(languageCode))
foreach (var fallback in fallbacks)
{
if (fallback == languageCode)
{
continue;
}

if (source.TryGetNonNull(fallback, out var fallbackValue))
{
source[languageCode] = fallbackValue;
Expand All @@ -76,7 +75,7 @@ public ResolveLanguages(LanguagesConfig languages, params Language[] filteredLan

foreach (var (key, _) in source)
{
if (!languageCodes.Contains(key))
if (!languagesWithFallbacks.ContainsKey(key))
{
source.Remove(key);
isRemoved = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ private JsonValueConverter()
public static (object? Result, JsonError? Error) ConvertValue(IField field, JsonValue value, IJsonSerializer serializer,
ResolvedComponents components)
{
Guard.NotNull(field);
Guard.NotNull(value);

var args = new Args(value, serializer, components);

return field.RawProperties.Accept(Instance, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ private static bool IsValidStringList(JsonValue value)
return false;
}

if (a.Count == 0)
{
return true;
}

foreach (var item in a)
{
if (item.Value is not string)
Expand All @@ -135,6 +140,11 @@ private static bool IsValidObjectList(JsonValue value)
return false;
}

if (a.Count == 0)
{
return true;
}

foreach (var item in a)
{
if (item.Value is not JsonObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Squidex.Infrastructure;
using Squidex.Infrastructure.Reflection;
using System.Diagnostics;

namespace Squidex.Domain.Apps.Entities.Assets.Queries;

Expand Down Expand Up @@ -36,7 +37,7 @@ public async Task<IReadOnlyList<IEnrichedAssetEntity>> EnrichAsync(IEnumerable<I
Guard.NotNull(assets);
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("AssetEnricher/EnrichAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetEnricher/EnrichAsync"))
{
var results = new List<AssetEntity>();

Expand Down Expand Up @@ -73,6 +74,8 @@ public async Task<IReadOnlyList<IEnrichedAssetEntity>> EnrichAsync(IEnumerable<I
}
}

activity?.SetTag("numItems", results.Count);

return results;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public Task EnrichAsync(Context context, IEnumerable<AssetEntity> assets,
u = url
};

var json = serializer.Serialize(token);
var json = serializer.SerializeToBytes(token);

asset.EditToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
asset.EditToken = Convert.ToBase64String(json);
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ private static IFieldResolver Resolve<T>(Func<IEnrichedContentEntity, T> resolve

private static string? GetSchemaName(JsonObject component)
{
if (component.TryGetValue("schemaName", out var name) && name.Type == JsonValueType.String)
if (component.TryGetValue("schemaName", out var value) && value.Value is string name)
{
return name.ToString();
return name;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Task<IReadOnlyList<IEnrichedContentEntity>> EnrichAsync(IEnumerable<ICont
private async Task<IReadOnlyList<IEnrichedContentEntity>> EnrichInternalAsync(IEnumerable<IContentEntity> contents, bool cloneData, Context context,
CancellationToken ct)
{
using (Telemetry.Activities.StartActivity("ContentEnricher/EnrichInternalAsync"))
using (var activity = Telemetry.Activities.StartActivity("ContentEnricher/EnrichInternalAsync"))
{
var results = new List<ContentEntity>();

Expand Down Expand Up @@ -110,6 +110,8 @@ private async Task<IReadOnlyList<IEnrichedContentEntity>> EnrichInternalAsync(IE
}
}

activity?.SetTag("numItems", results.Count);

return results;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public Task EnrichAsync(Context context, IEnumerable<ContentEntity> contents, Pr
u = url
};

var json = serializer.Serialize(token);
var json = serializer.SerializeToBytes(token);

content.EditToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
content.EditToken = Convert.ToBase64String(json);
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public static EventStoreData Write(EventData eventData, IJsonSerializer serializ
{
var payload = Encoding.UTF8.GetBytes(eventData.Payload);

var headersJson = serializer.Serialize(eventData.Headers);
var headersBytes = Encoding.UTF8.GetBytes(headersJson);
var headersJson = serializer.SerializeToBytes(eventData.Headers);
var headersBytes = headersJson;

return new EventStoreData(Uuid.FromGuid(Guid.NewGuid()), eventData.Type, payload, headersBytes);
}
Expand Down
Loading