Browse Source

More minor performance improvements.

pull/492/head
Sebastian 6 years ago
parent
commit
b84ae7290b
  1. 42
      backend/src/Squidex.Domain.Apps.Entities/Contents/Text/Extensions.cs
  2. 31
      backend/src/Squidex.Infrastructure.Azure/EventSourcing/StreamPosition.cs
  3. 30
      backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs
  4. 8
      backend/src/Squidex.Infrastructure/Email/SmtpEmailSender.cs
  5. 8
      backend/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs
  6. 2
      backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs

42
backend/src/Squidex.Domain.Apps.Entities/Contents/Text/Extensions.cs

@ -15,7 +15,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
public static class Extensions public static class Extensions
{ {
private static readonly ObjectPool<StringBuilder> Pool = new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy()); private static readonly ObjectPool<StringBuilder> StringBuilderPool =
new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy());
public static Dictionary<string, string> ToTexts(this NamedContentData data) public static Dictionary<string, string> ToTexts(this NamedContentData data)
{ {
@ -24,23 +25,30 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
if (data != null) if (data != null)
{ {
var languages = new Dictionary<string, StringBuilder>(); var languages = new Dictionary<string, StringBuilder>();
try
foreach (var value in data.Values)
{ {
if (value != null) foreach (var value in data.Values)
{ {
foreach (var (key, jsonValue) in value) if (value != null)
{ {
AppendJsonText(languages, key, jsonValue); foreach (var (key, jsonValue) in value)
{
AppendJsonText(languages, key, jsonValue);
}
} }
} }
}
foreach (var (key, stringBuilder) in languages) foreach (var (key, sb) in languages)
{
result[key] = sb.ToString();
}
}
finally
{ {
result[key] = stringBuilder.ToString(); foreach (var (_, sb) in languages)
{
Pool.Return(stringBuilder); StringBuilderPool.Return(sb);
}
} }
} }
@ -73,19 +81,19 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
if (!string.IsNullOrWhiteSpace(text)) if (!string.IsNullOrWhiteSpace(text))
{ {
if (!languages.TryGetValue(language, out var stringBuilder)) if (!languages.TryGetValue(language, out var sb))
{ {
stringBuilder = Pool.Get(); sb = StringBuilderPool.Get();
languages[language] = stringBuilder; languages[language] = sb;
} }
if (stringBuilder.Length > 0) if (sb.Length > 0)
{ {
stringBuilder.Append(" "); sb.Append(" ");
} }
stringBuilder.Append(text); sb.Append(text);
} }
} }
} }

31
backend/src/Squidex.Infrastructure.Azure/EventSourcing/StreamPosition.cs

@ -6,11 +6,15 @@
// ========================================================================== // ==========================================================================
using System.Text; using System.Text;
using Microsoft.Extensions.ObjectPool;
namespace Squidex.Infrastructure.EventSourcing namespace Squidex.Infrastructure.EventSourcing
{ {
internal sealed class StreamPosition internal sealed class StreamPosition
{ {
private static readonly ObjectPool<StringBuilder> StringBuilderPool =
new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy());
public long Timestamp { get; } public long Timestamp { get; }
public long CommitOffset { get; } public long CommitOffset { get; }
@ -32,15 +36,21 @@ namespace Squidex.Infrastructure.EventSourcing
public static implicit operator string(StreamPosition position) public static implicit operator string(StreamPosition position)
{ {
var sb = new StringBuilder(20); var sb = StringBuilderPool.Get();
try
sb.Append(position.Timestamp); {
sb.Append("-"); sb.Append(position.Timestamp);
sb.Append(position.CommitOffset); sb.Append("-");
sb.Append("-"); sb.Append(position.CommitOffset);
sb.Append(position.CommitSize); sb.Append("-");
sb.Append(position.CommitSize);
return sb.ToString(); return sb.ToString();
}
finally
{
StringBuilderPool.Return(sb);
}
} }
public static implicit operator StreamPosition(string? position) public static implicit operator StreamPosition(string? position)
@ -49,7 +59,10 @@ namespace Squidex.Infrastructure.EventSourcing
{ {
var parts = position.Split('-'); var parts = position.Split('-');
return new StreamPosition(long.Parse(parts[0]), long.Parse(parts[1]), long.Parse(parts[2])); return new StreamPosition(
long.Parse(parts[0]),
long.Parse(parts[1]),
long.Parse(parts[2]));
} }
return new StreamPosition(0, -1, -1); return new StreamPosition(0, -1, -1);

30
backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs

@ -6,12 +6,16 @@
// ========================================================================== // ==========================================================================
using System.Text; using System.Text;
using Microsoft.Extensions.ObjectPool;
using MongoDB.Bson; using MongoDB.Bson;
namespace Squidex.Infrastructure.EventSourcing namespace Squidex.Infrastructure.EventSourcing
{ {
internal sealed class StreamPosition internal sealed class StreamPosition
{ {
private static readonly ObjectPool<StringBuilder> StringBuilderPool =
new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy());
private static readonly BsonTimestamp EmptyTimestamp = new BsonTimestamp(946681200, 0); private static readonly BsonTimestamp EmptyTimestamp = new BsonTimestamp(946681200, 0);
public BsonTimestamp Timestamp { get; } public BsonTimestamp Timestamp { get; }
@ -35,17 +39,23 @@ namespace Squidex.Infrastructure.EventSourcing
public static implicit operator string(StreamPosition position) public static implicit operator string(StreamPosition position)
{ {
var sb = new StringBuilder(20); var sb = StringBuilderPool.Get();
try
sb.Append(position.Timestamp.Timestamp); {
sb.Append("-"); sb.Append(position.Timestamp.Timestamp);
sb.Append(position.Timestamp.Increment); sb.Append("-");
sb.Append("-"); sb.Append(position.Timestamp.Increment);
sb.Append(position.CommitOffset); sb.Append("-");
sb.Append("-"); sb.Append(position.CommitOffset);
sb.Append(position.CommitSize); sb.Append("-");
sb.Append(position.CommitSize);
return sb.ToString(); return sb.ToString();
}
finally
{
StringBuilderPool.Return(sb);
}
} }
public static implicit operator StreamPosition(string? position) public static implicit operator StreamPosition(string? position)

8
backend/src/Squidex.Infrastructure/Email/SmtpEmailSender.cs

@ -18,7 +18,7 @@ namespace Squidex.Infrastructure.Email
public sealed class SmtpEmailSender : IEmailSender public sealed class SmtpEmailSender : IEmailSender
{ {
private readonly SmptOptions options; private readonly SmptOptions options;
private readonly ObjectPool<SmtpClient> pool; private readonly ObjectPool<SmtpClient> clientPool;
internal sealed class SmtpClientPolicy : PooledObjectPolicy<SmtpClient> internal sealed class SmtpClientPolicy : PooledObjectPolicy<SmtpClient>
{ {
@ -53,19 +53,19 @@ namespace Squidex.Infrastructure.Email
this.options = options.Value; this.options = options.Value;
pool = new DefaultObjectPoolProvider().Create(new SmtpClientPolicy(options.Value)); clientPool = new DefaultObjectPoolProvider().Create(new SmtpClientPolicy(options.Value));
} }
public async Task SendAsync(string recipient, string subject, string body) public async Task SendAsync(string recipient, string subject, string body)
{ {
var smtpClient = pool.Get(); var smtpClient = clientPool.Get();
try try
{ {
await smtpClient.SendMailAsync(options.Sender, recipient, subject, body); await smtpClient.SendMailAsync(options.Sender, recipient, subject, body);
} }
finally finally
{ {
pool.Return(smtpClient); clientPool.Return(smtpClient);
} }
} }
} }

8
backend/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs

@ -12,7 +12,7 @@ namespace Squidex.Infrastructure.Log
{ {
public sealed class JsonLogWriterFactory : IObjectWriterFactory public sealed class JsonLogWriterFactory : IObjectWriterFactory
{ {
private readonly ObjectPool<JsonLogWriter> pool; private readonly ObjectPool<JsonLogWriter> writerPool;
internal sealed class JsonLogWriterPolicy : PooledObjectPolicy<JsonLogWriter> internal sealed class JsonLogWriterPolicy : PooledObjectPolicy<JsonLogWriter>
{ {
@ -47,7 +47,7 @@ namespace Squidex.Infrastructure.Log
public JsonLogWriterFactory(bool indended = false, bool formatLine = false) public JsonLogWriterFactory(bool indended = false, bool formatLine = false)
{ {
pool = new DefaultObjectPoolProvider().Create(new JsonLogWriterPolicy(indended, formatLine)); writerPool = new DefaultObjectPoolProvider().Create(new JsonLogWriterPolicy(indended, formatLine));
} }
public static JsonLogWriterFactory Default() public static JsonLogWriterFactory Default()
@ -62,12 +62,12 @@ namespace Squidex.Infrastructure.Log
public IObjectWriter Create() public IObjectWriter Create()
{ {
return pool.Get(); return writerPool.Get();
} }
public void Release(IObjectWriter writer) public void Release(IObjectWriter writer)
{ {
pool.Return((JsonLogWriter)writer); writerPool.Return((JsonLogWriter)writer);
} }
} }
} }

2
backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs

@ -93,7 +93,7 @@ namespace TestSuite.ApiTests
data: flatData { data: flatData {
name name
cities { cities {
data:flatData { data: flatData {
name name
} }
} }

Loading…
Cancel
Save