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

Implemented object pools und thread safety settings #229

Merged
merged 10 commits into from
Dec 3, 2021

Conversation

axunonb
Copy link
Member

@axunonb axunonb commented Dec 3, 2021

1. Object Pools

After implementing Object Pools for all classes which are frequently instantiated, GC and memory allocation again went down significantly.

In order to return "smart" objects back to the object pool, its important to use one of the following patterns.

Examples:

a) Single thread context (no need to care about object pooling)

var resultString = Smart.Format("format string", args);

b) Recommended: Auto-dispose Format (e.g.: caching, multi treading context)

var smart = Smart.CreateDefaultSmartFormat();
// Note "using" for auto-disposing the parsedFormat
using var parsedFormat = new Parser().ParseFormat("format string", args);
var resultString = smart.Format(parsedFormat);

c) Call Format.Dispose() (e.g.: caching, multi treading context)

var smart = Smart.CreateDefaultSmartFormat();
var parsedFormat = new Parser().ParseFormat("format string", args);
var resultString = smart.Format(parsedFormat);
// Don't use (or reference) "parsedFormat" after disposing
parsedFormat.Dispose();

2. Thread Safety

SmartFormat makes heavy use of caching and object pooling for expensive operations, which both require static containers.

a) Instantiating SmartFormatters from different threads:

`SmartSettings.IsThreadSafeMode=true` **must** be set, so that thread safe containers are used. This brings an inherent performance penalty.

 **Note:** The simplified `Smart.Format(...)` API overloads use a static `SmartFormatter` instance which is **not** thread safe. Call `Smart.CreateDefaultSmartFormat()` to create a default `Formatter`.

a) Instantiating SmartFormatters from a single thread:

`SmartSettings.IsThreadSafeMode=false` **should** be set for avoiding the multithreading overhead and thus for best performance. 

The simplified `Smart.Format(...)` API overloads are allowed here.

axunonb and others added 9 commits November 2, 2021 22:14
commit a8dec64
Merge: ebb820c c4f0115
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 21:55:35 2021 +0100

    Merge branch 'axuno:main' into main

commit c4f0115
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 21:54:46 2021 +0100

    Delete .github/workflows directory

commit 3283ba9
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 17:05:09 2021 +0100

    Remove demo projects

commit 0ebf3f4
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 16:30:42 2021 +0100

    Corrected path to test project

commit a9a79d1
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 16:22:48 2021 +0100

    Add AltCover to test

commit 608533f
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 16:07:01 2021 +0100

    Update build.yml

commit 3005692
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 15:00:06 2021 +0100

    Update build.yml

commit 4683d59
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 14:23:29 2021 +0100

    Update build.yml

commit d3f9b68
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 14:19:05 2021 +0100

    Update build.yml

commit 4ac19ea
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 14:11:42 2021 +0100

    Update build.yml

commit ef76dba
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 14:08:13 2021 +0100

    Update build.yml

commit e2406b4
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 14:01:07 2021 +0100

    Update build.yml

commit adee45a
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 14:00:40 2021 +0100

    Update build.yml

commit 5464bb9
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Nov 2 13:53:50 2021 +0100

    Create build.yml

commit ebb820c
Merge: 365bbd0 ca28b30
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Thu Oct 21 10:06:49 2021 +0200

    Merge branch 'axuno:main' into main

commit ca28b30
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Thu Oct 21 10:05:03 2021 +0200

    Backport fixes from v3 (axuno#213)

    Bumped version to 2.7.1

    Back-ported bug fixes from v3.0.0-alpha:
    axuno#179 - Corrected DualFromZeroToTwo plural rule
    axuno#211 . Illegal placeholder characters that are not 8-bit, will no more throw unexpected ThrowByteOverflowException

commit 365bbd0
Author: axunonb <nb@axuno.net>
Date:   Thu Oct 21 09:16:12 2021 +0200

    Bumped version to 2.7.1

    Backported bug fixes from v3.0.0-alpha:
    axuno#179 - Corrected DualFromZeroToTwo plural rule
    axuno#211 . Illegal placeholder characters that are not 8-bit, will no more throw unexpected ThrowByteOverflowException

commit 3165fc7
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Thu Oct 21 08:03:32 2021 +0200

    Updated NuGet secure key

commit 8169585
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Sun Sep 5 23:09:38 2021 +0200

    Update README.md

commit 1e09b6a
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Mon Aug 30 01:52:50 2021 +0200

    Update README.md

commit 8954f3e
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Mon Aug 30 01:51:32 2021 +0200

    Update README.md

commit a16b01c
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Jun 15 00:06:03 2021 +0200

    Update README.md

commit d9462f5
Author: axunonb <axunonb@users.noreply.github.com>
Date:   Tue Jun 15 00:05:17 2021 +0200

    Update README.md
* Performance tests
* Documentation in CHANGES.md
* Tests for PoolSettings.IsPoolingEnabled = false
* Include netstandard2.0 in unit tests
@codecov
Copy link

codecov bot commented Dec 3, 2021

Codecov Report

Merging #229 (1542526) into version/v3.0 (62c0e7e) will increase coverage by 1%.
The diff coverage is 97%.

Impacted file tree graph

@@             Coverage Diff              @@
##           version/v3.0   #229    +/-   ##
============================================
+ Coverage            95%    96%    +1%     
============================================
  Files                56     84    +28     
  Lines              2350   2884   +534     
============================================
+ Hits               2241   2768   +527     
- Misses              109    116     +7     
Impacted Files Coverage Δ
src/SmartFormat/Core/Settings/SmartSettings.cs 65% <ø> (ø)
src/SmartFormat/FormattingErrorEventArgs.cs 100% <ø> (ø)
src/SmartFormat/Smart.cs 100% <ø> (ø)
src/SmartFormat/Pooling/PoolingException.cs 50% <50%> (ø)
src/SmartFormat/Core/Parsing/SplitList.cs 72% <72%> (ø)
...rmat/Pooling/ObjectPools/ObjectPoolSingleThread.cs 86% <86%> (ø)
src/SmartFormat/Core/Parsing/Format.cs 95% <91%> (+11%) ⬆️
src/SmartFormat/SmartFormatter.cs 97% <94%> (+<1%) ⬆️
src/SmartFormat/Pooling/ObjectPools/LinkedPool.cs 98% <98%> (ø)
src/SmartFormat/Core/Formatting/FormatDetails.cs 100% <100%> (ø)
... and 48 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 62c0e7e...1542526. Read the comment docs.

@axunonb axunonb merged commit 8df6c0d into axuno:version/v3.0 Dec 3, 2021
@axunonb axunonb deleted the pr-object-pool branch December 3, 2021 23:20
axunonb added a commit to axunonb/SmartFormat that referenced this pull request Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant