From 1969b4126a784db93409926583e9fc480b79e385 Mon Sep 17 00:00:00 2001 From: CaptaiNiveau Date: Wed, 15 May 2024 11:26:12 +0200 Subject: [PATCH] Use Lazy for expensive properties in System Dataset In my tests with repeated creation (clones) of Faker instances, the creation of this dataset was by far the most expensive one. Making it lazy should alleviate a lot of GC pressure, until the Dataset is actually used. --- Source/Bogus/DataSets/System.cs | 64 ++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/Source/Bogus/DataSets/System.cs b/Source/Bogus/DataSets/System.cs index 46c8781c..8fdcb594 100644 --- a/Source/Bogus/DataSets/System.cs +++ b/Source/Bogus/DataSets/System.cs @@ -17,41 +17,47 @@ public class System : DataSet /// The locale that will be used to generate values. public System(string locale = "en") : base(locale) { - mimes = this.GetArray("mimeTypes"); - - lookup = mimes.OfType() - .ToDictionary(o => o["mime"].StringValue); - - mimeKeys = mimes - .OfType() - .Select(o => o["mime"].StringValue) - .Distinct() - .ToArray(); - - exts = mimes - .OfType() - .SelectMany(bObject => + mimes = new Lazy( + () => this.GetArray("mimeTypes")); + + lookup = new Lazy>( + () => mimes.Value.OfType() + .ToDictionary(o => o["mime"].StringValue)); + + mimeKeys = new Lazy( + () => mimes.Value + .OfType() + .Select(o => o["mime"].StringValue) + .Distinct() + .ToArray()); + + exts = new Lazy( + () => mimes.Value + .OfType() + .SelectMany(bObject => { - if( bObject.ContainsKey("extensions") ) + if (bObject.ContainsKey("extensions")) { var extensions = bObject["extensions"] as BArray; return extensions.OfType().Select(s => s.StringValue); } + return Enumerable.Empty(); }) - .ToArray(); - - types = mimeKeys.Select(k => k.Substring(0, k.IndexOf('/'))) - .Distinct() - .ToArray(); + .ToArray()); + + types = new Lazy( + () => mimeKeys.Value.Select(k => k.Substring(0, k.IndexOf('/'))) + .Distinct() + .ToArray()); } protected Lorem Lorem = null; - private readonly Dictionary lookup; - private readonly BArray mimes; - private readonly string[] exts; - private readonly string[] types; - private readonly string[] mimeKeys; + private readonly Lazy> lookup; + private readonly Lazy mimes; + private readonly Lazy exts; + private readonly Lazy types; + private readonly Lazy mimeKeys; private static readonly string[] commonFileTypes = { "video", "audio", "image", "text", "application" }; @@ -145,7 +151,7 @@ public string CommonFileName(string ext = null) /// public string MimeType() { - return this.Random.ArrayElement(this.mimeKeys); + return this.Random.ArrayElement(this.mimeKeys.Value); } @@ -181,7 +187,7 @@ public string CommonFileExt() /// public string FileType() { - return this.Random.ArrayElement(this.types); + return this.Random.ArrayElement(this.types.Value); } @@ -194,13 +200,13 @@ public string FileType() public string FileExt(string mimeType = null) { if( mimeType != null && - lookup.TryGetValue(mimeType, out var mime) && + lookup.Value.TryGetValue(mimeType, out var mime) && mime.ContainsKey("extensions") ) { return this.Random.ArrayElement(mime["extensions"] as BArray); } - return this.Random.ArrayElement(exts); + return this.Random.ArrayElement(exts.Value); } ///