Skip to content

Commit 9a71448

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore reduced a memory consumption in cases, where not used the embedding of objects and types
1 parent 66ba17a commit 9a71448

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2222
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</Description>
2323
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconUrl>
2424
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;ChakraCore</PackageTags>
25-
<PackageReleaseNotes>1. ChakraCore was updated to version 1.11.5;
26-
2. Fixed a error #65 “Memory leak using EmbedHostObject”.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Reduced a memory consumption in cases, where not used the embedding of objects and types.</PackageReleaseNotes>
2726
</PropertyGroup>
2827

2928
<Import Project="../../build/common.props" />

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/JsRt/TypeMapper.cs

+51-8
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,43 @@ internal sealed class TypeMapper : IDisposable
2424
/// <summary>
2525
/// Storage for lazy-initialized embedded objects
2626
/// </summary>
27-
private ConcurrentDictionary<EmbeddedObjectKey, Lazy<EmbeddedObject>> _lazyEmbeddedObjects =
28-
new ConcurrentDictionary<EmbeddedObjectKey, Lazy<EmbeddedObject>>();
27+
private ConcurrentDictionary<EmbeddedObjectKey, Lazy<EmbeddedObject>> _lazyEmbeddedObjects;
2928

3029
/// <summary>
3130
/// Callback for finalization of embedded object
3231
/// </summary>
3332
private JsFinalizeCallback _embeddedObjectFinalizeCallback;
3433

34+
/// <summary>
35+
/// Synchronizer of embedded object storage's initialization
36+
/// </summary>
37+
private readonly object _embeddedObjectStorageInitializationSynchronizer = new object();
38+
39+
/// <summary>
40+
/// Flag indicating whether the embedded object storage is initialized
41+
/// </summary>
42+
private bool _embeddedObjectStorageInitialized;
43+
3544
/// <summary>
3645
/// Storage for lazy-initialized embedded types
3746
/// </summary>
38-
private ConcurrentDictionary<string, Lazy<EmbeddedType>> _lazyEmbeddedTypes =
39-
new ConcurrentDictionary<string, Lazy<EmbeddedType>>();
47+
private ConcurrentDictionary<string, Lazy<EmbeddedType>> _lazyEmbeddedTypes;
4048

4149
/// <summary>
4250
/// Callback for finalization of embedded type
4351
/// </summary>
4452
private JsFinalizeCallback _embeddedTypeFinalizeCallback;
4553

54+
/// <summary>
55+
/// Synchronizer of embedded type storage's initialization
56+
/// </summary>
57+
private readonly object _embeddedTypeStorageInitializationSynchronizer = new object();
58+
59+
/// <summary>
60+
/// Flag indicating whether the embedded type storage is initialized
61+
/// </summary>
62+
private bool _embeddedTypeStorageInitialized;
63+
4664
/// <summary>
4765
/// Flag indicating whether this object is disposed
4866
/// </summary>
@@ -53,10 +71,7 @@ internal sealed class TypeMapper : IDisposable
5371
/// Constructs an instance of type mapper
5472
/// </summary>
5573
public TypeMapper()
56-
{
57-
_embeddedObjectFinalizeCallback = EmbeddedObjectFinalizeCallback;
58-
_embeddedTypeFinalizeCallback = EmbeddedTypeFinalizeCallback;
59-
}
74+
{ }
6075

6176

6277
/// <summary>
@@ -66,6 +81,20 @@ public TypeMapper()
6681
/// <returns>JavaScript value created from an host object</returns>
6782
public JsValue GetOrCreateScriptObject(object obj)
6883
{
84+
if (!_embeddedObjectStorageInitialized)
85+
{
86+
lock (_embeddedObjectStorageInitializationSynchronizer)
87+
{
88+
if (!_embeddedObjectStorageInitialized)
89+
{
90+
_lazyEmbeddedObjects = new ConcurrentDictionary<EmbeddedObjectKey, Lazy<EmbeddedObject>>();
91+
_embeddedObjectFinalizeCallback = EmbeddedObjectFinalizeCallback;
92+
93+
_embeddedObjectStorageInitialized = true;
94+
}
95+
}
96+
}
97+
6998
var embeddedObjectKey = new EmbeddedObjectKey(obj);
7099
EmbeddedObject embeddedObject = _lazyEmbeddedObjects.GetOrAdd(
71100
embeddedObjectKey,
@@ -82,6 +111,20 @@ public JsValue GetOrCreateScriptObject(object obj)
82111
/// <returns>JavaScript value created from an host type</returns>
83112
public JsValue GetOrCreateScriptType(Type type)
84113
{
114+
if (!_embeddedTypeStorageInitialized)
115+
{
116+
lock (_embeddedTypeStorageInitializationSynchronizer)
117+
{
118+
if (!_embeddedTypeStorageInitialized)
119+
{
120+
_lazyEmbeddedTypes = new ConcurrentDictionary<string, Lazy<EmbeddedType>>();
121+
_embeddedTypeFinalizeCallback = EmbeddedTypeFinalizeCallback;
122+
123+
_embeddedTypeStorageInitialized = true;
124+
}
125+
}
126+
}
127+
85128
string embeddedTypeKey = type.AssemblyQualifiedName;
86129
EmbeddedType embeddedType = _lazyEmbeddedTypes.GetOrAdd(
87130
embeddedTypeKey,

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/readme.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
1. ChakraCore was updated to version 1.11.5;
34-
2. Fixed a error #65 “Memory leak using EmbedHostObject”.
33+
Reduced a memory consumption in cases, where not used the embedding of objects
34+
and types.
3535

3636
=============
3737
DOCUMENTATION

0 commit comments

Comments
 (0)