Skip to content

Commit bb69488

Browse files
committed
Minor perf optimizations
1 parent fec0829 commit bb69488

File tree

9 files changed

+108
-56
lines changed

9 files changed

+108
-56
lines changed

src/Libraries/SmartStore.Core/IO/MimeTypes.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ public static string MapMimeTypeToExtension(string mimeType)
3131

3232
try
3333
{
34-
var key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
35-
object value = key != null ? key.GetValue("Extension", null) : null;
36-
result = value != null ? value.ToString().Trim('.') : null;
34+
using (var key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false))
35+
{
36+
object value = key != null ? key.GetValue("Extension", null) : null;
37+
result = value != null ? value.ToString().Trim('.') : null;
38+
}
3739
}
3840
catch (SecurityException)
3941
{

src/Libraries/SmartStore.Core/Infrastructure/ComparableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ protected virtual IEnumerable<PropertyInfo> GetSignaturePropertiesCore()
160160
var properties = t.GetProperties()
161161
.Where(p => Attribute.IsDefined(p, typeof(ObjectSignatureAttribute), true));
162162

163-
return properties.Union(_extraSignatureProperties);
163+
return properties.Union(_extraSignatureProperties).ToList();
164164
}
165165

166166
/// <summary>

src/Libraries/SmartStore.Services/Common/KeepAliveTask.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public KeepAliveTask(IStoreContext storeContext)
1515
{
1616
this._storeContext = storeContext;
1717
}
18-
/// <summary>
19-
/// Executes a task
20-
/// </summary>
18+
2119
public void Execute()
2220
{
2321
var storeUrl = _storeContext.CurrentStore.Url.TrimEnd('\\').EnsureEndsWith("/");

src/Presentation/SmartStore.Web.Framework/Themes/DefaultThemeRegistry.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919

2020
namespace SmartStore.Web.Framework.Themes
2121
{
22-
public partial class DefaultThemeRegistry : IThemeRegistry
22+
public partial class DefaultThemeRegistry : DisposableObject, IThemeRegistry
2323
{
2424
#region Fields
2525

2626
internal const string THEME_MANIFESTS_ALL_KEY = "sm.theme-manifests.all";
2727

2828
private readonly SmartStoreConfig _cfg;
2929
private readonly IEventPublisher _eventPublisher;
30-
private readonly ConcurrentDictionary<string, ThemeManifest> _themes = new ConcurrentDictionary<string,ThemeManifest>(StringComparer.InvariantCultureIgnoreCase);
30+
private readonly ConcurrentDictionary<string, ThemeManifest> _themes = new ConcurrentDictionary<string, ThemeManifest>(StringComparer.InvariantCultureIgnoreCase);
31+
32+
private FileSystemWatcher _watcherCfg;
33+
private FileSystemWatcher _watcherFolders;
3134

3235
#endregion
3336

@@ -54,32 +57,32 @@ public DefaultThemeRegistry(
5457

5558
private void WatchConfigFiles()
5659
{
57-
var watcher = new FileSystemWatcher();
58-
59-
watcher.Path = CommonHelper.MapPath(_cfg.ThemeBasePath);
60-
watcher.Filter = "theme.config";
61-
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
62-
watcher.IncludeSubdirectories = true;
63-
watcher.EnableRaisingEvents = true;
64-
65-
watcher.Changed += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
66-
watcher.Deleted += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
67-
watcher.Created += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
68-
watcher.Renamed += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
60+
_watcherCfg = new FileSystemWatcher();
61+
62+
_watcherCfg.Path = CommonHelper.MapPath(_cfg.ThemeBasePath);
63+
_watcherCfg.Filter = "theme.config";
64+
_watcherCfg.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
65+
_watcherCfg.IncludeSubdirectories = true;
66+
_watcherCfg.EnableRaisingEvents = true;
67+
68+
_watcherCfg.Changed += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
69+
_watcherCfg.Deleted += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
70+
_watcherCfg.Created += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
71+
_watcherCfg.Renamed += (s, e) => ThemeConfigChanged(e.Name, e.FullPath);
6972
}
7073

7174
private void WatchFolders()
7275
{
73-
var watcher = new FileSystemWatcher();
76+
_watcherFolders = new FileSystemWatcher();
7477

75-
watcher.Path = CommonHelper.MapPath(_cfg.ThemeBasePath);
76-
watcher.Filter = "*";
77-
watcher.NotifyFilter = NotifyFilters.DirectoryName;
78-
watcher.IncludeSubdirectories = false;
79-
watcher.EnableRaisingEvents = true;
78+
_watcherFolders.Path = CommonHelper.MapPath(_cfg.ThemeBasePath);
79+
_watcherFolders.Filter = "*";
80+
_watcherFolders.NotifyFilter = NotifyFilters.DirectoryName;
81+
_watcherFolders.IncludeSubdirectories = false;
82+
_watcherFolders.EnableRaisingEvents = true;
8083

81-
watcher.Renamed += (s, e) => ThemeFolderRenamed(e.Name, e.FullPath, e.OldName, e.OldFullPath);
82-
watcher.Deleted += (s, e) => TryRemoveManifest(e.Name);
84+
_watcherFolders.Renamed += (s, e) => ThemeFolderRenamed(e.Name, e.FullPath, e.OldName, e.OldFullPath);
85+
_watcherFolders.Deleted += (s, e) => TryRemoveManifest(e.Name);
8386
}
8487

8588
private void ThemeConfigChanged(string name, string fullPath)
@@ -195,6 +198,18 @@ private void LoadThemes()
195198

196199
#endregion
197200

201+
202+
protected override void OnDispose(bool disposing)
203+
{
204+
if (disposing)
205+
{
206+
_watcherCfg.Dispose();
207+
_watcherFolders.Dispose();
208+
209+
_watcherCfg = null;
210+
_watcherFolders = null;
211+
}
212+
}
198213
}
199214

200215
}

src/Presentation/SmartStore.Web.Framework/Themes/ThemeVarsVirtualFile.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ public override Stream Open()
4040
private Stream GenerateStreamFromString(string value)
4141
{
4242
var stream = new MemoryStream();
43-
var writer = new StreamWriter(stream, Encoding.Unicode);
44-
writer.Write(value);
45-
writer.Flush();
46-
stream.Seek(0, SeekOrigin.Begin);
47-
return stream;
43+
44+
using (var writer = new StreamWriter(stream, Encoding.Unicode, 1024, true))
45+
{
46+
writer.Write(value);
47+
writer.Flush();
48+
stream.Seek(0, SeekOrigin.Begin);
49+
return stream;
50+
}
4851
}
4952

5053
}

src/Presentation/SmartStore.Web.Framework/UI/Components/ComponentRenderer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ public void Render()
7373
public string ToHtmlString()
7474
{
7575
string str;
76-
using (StringWriter stringWriter = new StringWriter())
76+
using (var stringWriter = new StringWriter())
7777
{
78-
this.WriteHtml(new HtmlTextWriter(stringWriter));
79-
str = stringWriter.ToString();
78+
using (var htmlWriter = new HtmlTextWriter(stringWriter))
79+
{
80+
this.WriteHtml(htmlWriter);
81+
str = stringWriter.ToString();
82+
}
8083
}
8184
return str;
8285
}

src/Presentation/SmartStore.Web/Infrastructure/Cache/ModelCacheEventConsumer.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,22 @@ public partial class ModelCacheEventConsumer:
247247
/// Key for TopicWidget caching
248248
/// </summary>
249249
/// <remarks>
250-
/// {0} : widget zone
251-
/// {1} : store id
252-
/// {2} : language id
250+
/// {0} : store id
251+
/// {1} : language id
253252
/// </remarks>
254-
public const string TOPIC_WIDGET_PATTERN_KEY = "sm.pres.topic.widget";
255-
public const string TOPIC_WIDGET_ALL_MODEL_KEY = "sm.pres.topic.widget-all-{0}";
256-
public const string TOPIC_WIDGET_BYZONE_PATTERN_KEY = "sm.pres.topic.widget-byzone";
257-
public const string TOPIC_WIDGET_BYZONE_MODEL_KEY = "sm.pres.topic.widget-byzone-{0}-{1}-{2}";
253+
public const string TOPIC_WIDGET_PATTERN_KEY = "sm.pres.topic.widget";
254+
public const string TOPIC_WIDGET_ALL_MODEL_KEY = "sm.pres.topic.widget-all-{0}-{1}";
255+
256+
/// <summary>
257+
/// Key for TopicWidget by zone caching
258+
/// </summary>
259+
/// <remarks>
260+
/// {0} : widget zone
261+
/// {1} : store id
262+
/// {2} : language id
263+
/// </remarks>
264+
public const string TOPIC_WIDGET_BYZONE_PATTERN_KEY = "sm.pres.topic.widget-byzone";
265+
public const string TOPIC_WIDGET_BYZONE_MODEL_KEY = "sm.pres.topic.widget-byzone-{0}-{1}-{2}";
258266

259267
/// <summary>
260268
/// Key for CategoryTemplate caching

src/Presentation/SmartStore.Web/Infrastructure/DefaultWidgetSelector.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,31 @@ public virtual IEnumerable<WidgetRouteInfo> GetWidgets(string widgetZone)
8181
#region Topic Widgets
8282

8383
// add special "topic widgets" to the list
84-
var allTopicsCacheKey = string.Format(ModelCacheEventConsumer.TOPIC_WIDGET_ALL_MODEL_KEY, _storeContext.CurrentStore.Id);
84+
var allTopicsCacheKey = string.Format(ModelCacheEventConsumer.TOPIC_WIDGET_ALL_MODEL_KEY, _storeContext.CurrentStore.Id, _workContext.WorkingLanguage.Id);
8585
var topicWidgets = _cacheManager.Get(allTopicsCacheKey, () =>
8686
{
87-
var result = _topicService.GetAllTopics(_storeContext.CurrentStore.Id);
88-
var list = result.Where(x => x.RenderAsWidget).ToList();
89-
list.Each(x => _dbContext.DetachEntity(x));
90-
return list;
87+
var allTopicWidgets = _topicService.GetAllTopics(_storeContext.CurrentStore.Id).Where(x => x.RenderAsWidget).ToList();
88+
var stubs = allTopicWidgets
89+
.Select(t => new TopicWidgetStub
90+
{
91+
Id = t.Id,
92+
Bordered = t.WidgetBordered,
93+
ShowTitle = t.WidgetShowTitle,
94+
SystemName = t.SystemName.SanitizeHtmlId(),
95+
Title = t.GetLocalized(x => t.Title),
96+
Body = t.GetLocalized(x => t.Body),
97+
WidgetZones = t.GetWidgetZones().ToArray(),
98+
Priority = t.Priority
99+
})
100+
.ToList();
101+
return stubs;
91102
});
92103

93104
var byZoneTopicsCacheKey = string.Format(ModelCacheEventConsumer.TOPIC_WIDGET_BYZONE_MODEL_KEY, widgetZone, _storeContext.CurrentStore.Id, _workContext.WorkingLanguage.Id);
94105
var topicsByZone = _cacheManager.Get(byZoneTopicsCacheKey, () =>
95106
{
96107
var result = from t in topicWidgets
97-
where t.GetWidgetZones().Contains(widgetZone, StringComparer.InvariantCultureIgnoreCase)
108+
where t.WidgetZones.Contains(widgetZone, StringComparer.InvariantCultureIgnoreCase)
98109
orderby t.Priority
99110
select new WidgetRouteInfo
100111
{
@@ -108,11 +119,11 @@ orderby t.Priority
108119
{"model", new TopicWidgetModel
109120
{
110121
Id = t.Id,
111-
SystemName = t.SystemName.SanitizeHtmlId(),
112-
ShowTitle = t.WidgetShowTitle,
113-
IsBordered = t.WidgetBordered,
114-
Title = t.GetLocalized(x => t.Title),
115-
Html = t.GetLocalized(x => t.Body)
122+
SystemName = t.SystemName,
123+
ShowTitle = t.ShowTitle,
124+
IsBordered = t.Bordered,
125+
Title = t.Title,
126+
Html = t.Body
116127
} }
117128
}
118129
};
@@ -219,6 +230,18 @@ class SimpleWidgetStub
219230
public int Ordinal { get; set; }
220231
}
221232

233+
class TopicWidgetStub
234+
{
235+
public int Id { get; set; }
236+
public string[] WidgetZones { get; set; }
237+
public string SystemName { get; set; }
238+
public bool ShowTitle { get; set; }
239+
public bool Bordered { get; set; }
240+
public string Title { get; set; }
241+
public string Body { get; set; }
242+
public int Priority { get; set; }
243+
}
244+
222245
#endregion
223246

224247
}

src/Presentation/SmartStore.Web/Infrastructure/DependencyRegistrar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
2626
builder.RegisterType<ShoppingCartController>().WithStaticCache();
2727
builder.RegisterType<TopicController>().WithStaticCache();
2828

29-
builder.RegisterType<DefaultWidgetSelector>().As<IWidgetSelector>().WithRequestCache().InstancePerHttpRequest();
29+
builder.RegisterType<DefaultWidgetSelector>().As<IWidgetSelector>().WithStaticCache().InstancePerHttpRequest();
3030

3131
// installation localization service
3232
builder.RegisterType<InstallationLocalizationService>().As<IInstallationLocalizationService>().InstancePerHttpRequest();

0 commit comments

Comments
 (0)