Antlr4.StringTemplate.TemplateGroup.CacheCompiledGroup C# (CSharp) Method

CacheCompiledGroup() private method

private CacheCompiledGroup ( string cachePath, string prefix, string fileName, System.DateTime lastWriteTime ) : void
cachePath string
prefix string
fileName string
lastWriteTime System.DateTime
return void
        private void CacheCompiledGroup(string cachePath, string prefix, string fileName, DateTime lastWriteTime)
        {
            var timer = System.Diagnostics.Stopwatch.StartNew();

            var serializedStrings = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            var comparer = ObjectReferenceEqualityComparer<object>.Default;
            var serializedObjects = new HashSet<object>(ObjectReferenceEqualityComparer<object>.Default);

            // start with the root set
            serializedObjects.UnionWith(_importsToClearOnUnload.OfType<object>());
            serializedObjects.UnionWith(templates.Values.OfType<object>());
            serializedObjects.UnionWith(dictionaries.Values.SelectMany(i => i.Values).OfType<object>());
            // update to the reachable set
            serializedObjects = CalculateReachableSerializedObjects(serializedObjects.ToArray());

            MemoryStream stream = new MemoryStream();
            var writer = new BinaryWriter(stream, Encoding.UTF8);
            writer.Write(lastWriteTime.Ticks);

            // objects
            List<object> orderedObjectsForExport = GetOrderedExports(serializedObjects);
            writer.Write(orderedObjectsForExport.Count);
            foreach (var obj in orderedObjectsForExport)
            {
                WriteGroupObject(writer, obj);
            }

            // imported groups
            writer.Write(_importsToClearOnUnload.Count);
            foreach (var group in _importsToClearOnUnload)
                writer.Write(comparer.GetHashCode(group));

            // delimiters
            writer.Write(delimiterStartChar);
            writer.Write(delimiterStopChar);

            // templates & aliases
            writer.Write(templates.Count);
            foreach (var template in templates)
            {
                writer.Write(template.Key);
                writer.Write(comparer.GetHashCode(template.Value));
            }

            // dictionaries
            writer.Write(dictionaries.Count);
            foreach (var dictionary in dictionaries)
            {
                writer.Write(dictionary.Key);
                writer.Write(dictionary.Value.Count);
                foreach (var dictionaryValue in dictionary.Value)
                {
                    writer.Write(dictionaryValue.Key);
                    writer.Write(comparer.GetHashCode(dictionaryValue.Value));
                }
            }

            System.Diagnostics.Debug.WriteLine(string.Format("Successfully cached the group {0} in {1}ms ({2} bytes).", Name, timer.ElapsedMilliseconds, stream.Length));

            Directory.CreateDirectory(cachePath);

            string cacheFileName = Path.GetFileNameWithoutExtension(fileName) + (uint)fileName.GetHashCode() + prefix.Replace(Path.DirectorySeparatorChar, '_').Replace(Path.AltDirectorySeparatorChar, '_');
            cacheFileName = Path.Combine(cachePath, cacheFileName);
            File.WriteAllBytes(cacheFileName, stream.ToArray());
        }