From 4395c0b03397514e5104d4cd5d4abc293f6cfa7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Zaragoza=20Cort=C3=A9s?= Date: Tue, 8 Dec 2020 18:46:16 -0800 Subject: [PATCH 1/4] Only initialize Bond protocols when needed. --- src/QsCompiler/Compiler/CompilationLoader.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/QsCompiler/Compiler/CompilationLoader.cs b/src/QsCompiler/Compiler/CompilationLoader.cs index 86d51ec911..fb41324ff0 100644 --- a/src/QsCompiler/Compiler/CompilationLoader.cs +++ b/src/QsCompiler/Compiler/CompilationLoader.cs @@ -477,11 +477,17 @@ public CompilationLoader(SourceLoader loadSources, ReferenceLoader loadReference this.RaiseCompilationTaskStart(null, "OverallCompilation"); // loading the content to compiler - BondSchemas.Protocols.Initialize(); this.logger = logger; this.LoadDiagnostics = ImmutableArray.Empty; this.config = options ?? default; + // When loading references is done through the generated C# and the syntax tree is not serialized, + // Bond protocols are not needed. + if (!this.config.LoadReferencesBasedOnGeneratedCsharp && !this.config.SerializeSyntaxTree) + { + BondSchemas.Protocols.Initialize(); + } + Status rewriteStepLoading = Status.Succeeded; this.externalRewriteSteps = ExternalRewriteStepsManager.Load(this.config, d => this.LogAndUpdateLoadDiagnostics(ref rewriteStepLoading, d), ex => this.LogAndUpdate(ref rewriteStepLoading, ex)); this.PrintLoadedRewriteSteps(this.externalRewriteSteps); From 8b390e171ee3161d44cf92c0ce523c91c1c0ddc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Zaragoza=20Cort=C3=A9s?= Date: Wed, 9 Dec 2020 11:15:45 -0800 Subject: [PATCH 2/4] Have separate APIs for initializing serializers and deserializers. --- src/QsCompiler/BondSchemas/Protocols.cs | 15 +++++++++++++-- src/QsCompiler/Compiler/CompilationLoader.cs | 13 +++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/QsCompiler/BondSchemas/Protocols.cs b/src/QsCompiler/BondSchemas/Protocols.cs index 862161560e..fb44d86ac7 100644 --- a/src/QsCompiler/BondSchemas/Protocols.cs +++ b/src/QsCompiler/BondSchemas/Protocols.cs @@ -46,13 +46,24 @@ public static class Protocols } /// - /// Starts the creation of Bond serializers and deserializers. + /// Starts the creation of a Bond deserializer. /// - public static void Initialize() + public static void InitializeDeserializer() { lock (BondSharedDataStructuresLock) { simpleBinaryDeserializerInitialization = QueueSimpleBinaryDeserializerInitialization(); + } + } + + /// + /// Starts the creation of a Bond serializer. + /// + public static void InitializeSerializer() + { + lock (BondSharedDataStructuresLock) + { + simpleBinarySerializerInitialization = QueueSimpleBinarySerializerInitialization(); } } diff --git a/src/QsCompiler/Compiler/CompilationLoader.cs b/src/QsCompiler/Compiler/CompilationLoader.cs index fb41324ff0..749f5b313d 100644 --- a/src/QsCompiler/Compiler/CompilationLoader.cs +++ b/src/QsCompiler/Compiler/CompilationLoader.cs @@ -481,11 +481,16 @@ public CompilationLoader(SourceLoader loadSources, ReferenceLoader loadReference this.LoadDiagnostics = ImmutableArray.Empty; this.config = options ?? default; - // When loading references is done through the generated C# and the syntax tree is not serialized, - // Bond protocols are not needed. - if (!this.config.LoadReferencesBasedOnGeneratedCsharp && !this.config.SerializeSyntaxTree) + // When loading references is done through the generated C# a Bond deserializer is not needed. + if (this.config.LoadReferencesBasedOnGeneratedCsharp) { - BondSchemas.Protocols.Initialize(); + BondSchemas.Protocols.InitializeDeserializer(); + } + + // When the syntax tree is not serialized a Bond serializer is not needed. + if (this.config.SerializeSyntaxTree) + { + BondSchemas.Protocols.InitializeSerializer(); } Status rewriteStepLoading = Status.Succeeded; From cce6da4647bc88a9268018affe18d9383190e8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Zaragoza=20Cort=C3=A9s?= Date: Wed, 9 Dec 2020 11:22:38 -0800 Subject: [PATCH 3/4] Make Bond initialization APIs safe to call anytime. --- src/QsCompiler/BondSchemas/Protocols.cs | 30 ++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/QsCompiler/BondSchemas/Protocols.cs b/src/QsCompiler/BondSchemas/Protocols.cs index fb44d86ac7..9d10a0ed42 100644 --- a/src/QsCompiler/BondSchemas/Protocols.cs +++ b/src/QsCompiler/BondSchemas/Protocols.cs @@ -45,6 +45,25 @@ public static class Protocols return CompilerObjectTranslator.CreateQsCompilation(bondCompilation); } + /// + /// Starts the creation of Bond serializers and deserializers. + /// + public static void Initialize() + { + lock (BondSharedDataStructuresLock) + { + if (simpleBinaryDeserializerInitialization == null) + { + simpleBinaryDeserializerInitialization = QueueSimpleBinaryDeserializerInitialization(); + } + + if (simpleBinarySerializerInitialization == null) + { + simpleBinarySerializerInitialization = QueueSimpleBinarySerializerInitialization(); + } + } + } + /// /// Starts the creation of a Bond deserializer. /// @@ -52,7 +71,10 @@ public static void InitializeDeserializer() { lock (BondSharedDataStructuresLock) { - simpleBinaryDeserializerInitialization = QueueSimpleBinaryDeserializerInitialization(); + if (simpleBinaryDeserializerInitialization == null) + { + simpleBinaryDeserializerInitialization = QueueSimpleBinaryDeserializerInitialization(); + } } } @@ -63,8 +85,10 @@ public static void InitializeSerializer() { lock (BondSharedDataStructuresLock) { - - simpleBinarySerializerInitialization = QueueSimpleBinarySerializerInitialization(); + if (simpleBinarySerializerInitialization == null) + { + simpleBinarySerializerInitialization = QueueSimpleBinarySerializerInitialization(); + } } } From 816f909000b5661e8df45ed8b6dcd0fba480cd9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Zaragoza=20Cort=C3=A9s?= Date: Wed, 9 Dec 2020 11:26:37 -0800 Subject: [PATCH 4/4] Fix logical error. --- src/QsCompiler/Compiler/CompilationLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/QsCompiler/Compiler/CompilationLoader.cs b/src/QsCompiler/Compiler/CompilationLoader.cs index 749f5b313d..1764c75bcf 100644 --- a/src/QsCompiler/Compiler/CompilationLoader.cs +++ b/src/QsCompiler/Compiler/CompilationLoader.cs @@ -482,7 +482,7 @@ public CompilationLoader(SourceLoader loadSources, ReferenceLoader loadReference this.config = options ?? default; // When loading references is done through the generated C# a Bond deserializer is not needed. - if (this.config.LoadReferencesBasedOnGeneratedCsharp) + if (!this.config.LoadReferencesBasedOnGeneratedCsharp) { BondSchemas.Protocols.InitializeDeserializer(); }