/// <summary>
 /// Deserializes a pip graph fragment from stream.
 /// </summary>
 public async Task <bool> DeserializeAsync(
     Stream stream,
     Func <PipGraphFragmentContext, PipGraphFragmentProvenance, PipId, Pip, Task <bool> > handleDeserializedPip = null,
     string fragmentDescriptionOverride = null,
     AbsolutePath filePathOrigin        = default)
 {
     using (var reader = new PipRemapReader(m_pipExecutionContext, m_pipGraphFragmentContext, stream))
     {
         try
         {
             string serializedDescription = reader.ReadNullableString();
             FragmentDescription = fragmentDescriptionOverride ?? serializedDescription;
             var  provenance             = new PipGraphFragmentProvenance(filePathOrigin, FragmentDescription);
             bool serializedUsingTopSort = reader.ReadBoolean();
             Func <PipId, Pip, Task <bool> > handleDeserializedPipInFragment = (pipId, pip) => handleDeserializedPip(m_pipGraphFragmentContext, provenance, pipId, pip);
             if (serializedUsingTopSort)
             {
                 return(await DeserializeTopSortAsync(handleDeserializedPipInFragment, reader));
             }
             else
             {
                 return(await DeserializeSeriallyAsync(handleDeserializedPipInFragment, reader));
             }
         }
         finally
         {
             Interlocked.Add(ref Stats.OptimizedSymbols, reader.OptimizedSymbols);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Deserializes a pip graph fragment and call the given handleDeserializedPip function on each pip deserialized.
        /// </summary>
        public bool Deserialize(
            AbsolutePath filePath,
            Func <PipGraphFragmentContext, PipGraphFragmentProvenance, PipId, Pip, bool> handleDeserializedPip = null,
            string fragmentDescriptionOverride = null)
        {
            Contract.Requires(filePath.IsValid);

            string fileName = filePath.ToString(m_pipExecutionContext.PathTable);

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var reader = new PipRemapReader(m_pipExecutionContext, m_pipGraphFragmentContext, stream))
                {
                    string serializedDescription = reader.ReadNullableString();
                    FragmentDescription = (fragmentDescriptionOverride ?? serializedDescription) ?? filePath.ToString(m_pipExecutionContext.PathTable);
                    var provenance = new PipGraphFragmentProvenance(filePath, FragmentDescription);

                    m_totalPipsToDeserialize = reader.ReadInt32();

                    for (int i = 0; i < m_totalPipsToDeserialize; i++)
                    {
                        var pip = Pip.Deserialize(reader);

                        // Pip id is not deserialized when pip is deserialized.
                        // Pip id must be read separately. To be able to add a pip to the graph, the pip id of the pip
                        // is assumed to be unset, and is set when the pip gets inserted into the pip table.
                        // Thus, one should not assign the pip id of the deserialized pip with the deserialized pip id.
                        // Do not use reader.ReadPipId() for reading the deserialized pip id. The method reader.ReadPipId()
                        // remaps the pip id to a new pip id.
                        var pipId = new PipId(reader.ReadUInt32());

                        var success = handleDeserializedPip?.Invoke(m_pipGraphFragmentContext, provenance, pipId, pip);

                        if (success.HasValue & !success.Value)
                        {
                            return(false);
                        }

                        Stats.Increment(pip, serialize: false);
                    }
                }

            return(true);
        }
Beispiel #3
0
 /// <summary>
 /// Deserializes a pip graph fragment from stream.
 /// </summary>
 public bool Deserialize(
     Stream stream,
     Func <PipGraphFragmentContext, PipGraphFragmentProvenance, PipId, Pip, bool> handleDeserializedPip = null,
     string fragmentDescriptionOverride = null,
     AbsolutePath filePathOrigin        = default)
 {
     using (var reader = new PipRemapReader(m_pipExecutionContext, m_pipGraphFragmentContext, stream))
     {
         string serializedDescription = reader.ReadNullableString();
         FragmentDescription = fragmentDescriptionOverride ?? serializedDescription;
         var  provenance             = new PipGraphFragmentProvenance(filePathOrigin, FragmentDescription);
         bool serializedUsingTopSort = reader.ReadBoolean();
         Func <PipId, Pip, bool> handleDeserializedPipInFragment = (pipId, pip) => handleDeserializedPip(m_pipGraphFragmentContext, provenance, pipId, pip);
         if (serializedUsingTopSort)
         {
             return(DeserializeTopSort(handleDeserializedPipInFragment, reader));
         }
         else
         {
             return(DeserializeSerially(handleDeserializedPipInFragment, reader));
         }
     }
 }
Beispiel #4
0
 public PipDataEntriesPointerInlineReader(PipRemapReader baseInliningReader, Stream stream, PathTable pathTable, bool debug = false, bool leaveOpen = true)
     : base(stream, pathTable, debug, leaveOpen)
 {
     m_baseInliningReader = baseInliningReader;
 }
Beispiel #5
0
        private async Task <bool> DeserializeTopSortAsync(Func <PipId, Pip, Task <bool> > handleDeserializedPip, PipRemapReader reader)
        {
            bool successful = true;

            m_totalPipsToDeserialize = reader.ReadInt32();
            int totalPipsRead = 0;

            while (totalPipsRead < m_totalPipsToDeserialize)
            {
                var deserializedPips = reader.ReadReadOnlyList <(Pip pip, PipId pipId)>((deserializer) =>
                {
                    var pip = Pip.Deserialize(reader);

                    // Pip id is not deserialized when pip is deserialized.
                    // Pip id must be read separately. To be able to add a pip to the graph, the pip id of the pip
                    // is assumed to be unset, and is set when the pip gets inserted into the pip table.
                    // Thus, one should not assign the pip id of the deserialized pip with the deserialized pip id.
                    // Do not use reader.ReadPipId() for reading the deserialized pip id. The method reader.ReadPipId()
                    // remaps the pip id to a new pip id.
                    var pipId = new PipId(reader.ReadUInt32());
                    return(pip, pipId);
                });

                totalPipsRead += deserializedPips.Count;
                Task <bool>[] tasks = new Task <bool> [deserializedPips.Count];

                for (int i = 0; i < deserializedPips.Count; i++)
                {
                    var deserializedPip = deserializedPips[i];
                    tasks[i] = HandleAndReportDeserializedPipAsync(handleDeserializedPip, deserializedPip.pipId, deserializedPip.pip);
                }

                successful &= (await Task.WhenAll(tasks)).All(x => x);
            }

            return(successful);
        }
Beispiel #6
0
        private async Task <bool> DeserializeSeriallyAsync(Func <PipId, Pip, Task <bool> > handleDeserializedPip, PipRemapReader reader)
        {
            bool successful = true;

            m_totalPipsToDeserialize = reader.ReadInt32();
            for (int totalPipsRead = 0; totalPipsRead < m_totalPipsToDeserialize; totalPipsRead++)
            {
                var pip   = Pip.Deserialize(reader);
                var pipId = new PipId(reader.ReadUInt32());
                if (!await(handleDeserializedPip?.Invoke(pipId, pip)))
                {
                    successful = false;
                }

                Stats.Increment(pip, serialize: false);
            }

            return(successful);
        }
Beispiel #7
0
        private bool DeserializeTopSort(Func <PipId, Pip, bool> handleDeserializedPip, PipRemapReader reader)
        {
            bool successful = true;

            m_totalPipsToDeserialize = reader.ReadInt32();
            int totalPipsRead = 0;

            while (totalPipsRead < m_totalPipsToDeserialize)
            {
                var deserializedPips = reader.ReadReadOnlyList <(Pip, PipId)>((deserializer) =>
                {
                    var pip = Pip.Deserialize(reader);

                    // Pip id is not deserialized when pip is deserialized.
                    // Pip id must be read separately. To be able to add a pip to the graph, the pip id of the pip
                    // is assumed to be unset, and is set when the pip gets inserted into the pip table.
                    // Thus, one should not assign the pip id of the deserialized pip with the deserialized pip id.
                    // Do not use reader.ReadPipId() for reading the deserialized pip id. The method reader.ReadPipId()
                    // remaps the pip id to a new pip id.
                    var pipId = new PipId(reader.ReadUInt32());
                    return(pip, pipId);
                });
                totalPipsRead += deserializedPips.Count;

                Parallel.ForEach(deserializedPips, new ParallelOptions(), deserializedPip =>
                {
                    if (!(handleDeserializedPip?.Invoke(deserializedPip.Item2, deserializedPip.Item1)).Value)
                    {
                        successful = false;
                    }

                    Stats.Increment(deserializedPip.Item1, serialize: false);
                });
            }

            return(successful);
        }
Beispiel #8
0
        private async Task <bool> DeserializeTopSortAsync(Func <PipId, Pip, Task <bool> > handleDeserializedPip, PipRemapReader reader)
        {
            bool successful = true;

            m_totalPipsToDeserialize = reader.ReadInt32();
            var numberOfLayers = reader.ReadInt32();
            int totalPips      = 0;

            for (int layer = 0; layer < numberOfLayers; ++layer)
            {
                var deserializedPips = reader.ReadReadOnlyList((deserializer) => DeserializePipAndPipId(reader));

                totalPips += deserializedPips.Count;

                Task <bool>[] tasks = new Task <bool> [deserializedPips.Count];

                for (int i = 0; i < deserializedPips.Count; i++)
                {
                    var(pip, pipId) = deserializedPips[i];
                    tasks[i]        = HandleAndReportDeserializedPipAsync(handleDeserializedPip, pipId, pip);
                }

                successful &= (await Task.WhenAll(tasks)).All(x => x);
            }

            Contract.Assert(totalPips == m_totalPipsToDeserialize, "Unexpected number of deserialized pips");

            return(successful);
        }
Beispiel #9
0
        private async Task <bool> DeserializeSeriallyAsync(Func <PipId, Pip, Task <bool> > handleDeserializedPip, PipRemapReader reader)
        {
            bool successful = true;

            m_totalPipsToDeserialize = reader.ReadInt32();
            for (int totalPipsRead = 0; totalPipsRead < m_totalPipsToDeserialize; totalPipsRead++)
            {
                (Pip pip, PipId pipId) = DeserializePipAndPipId(reader);

                if (!await handleDeserializedPip(pipId, pip))
                {
                    successful = false;
                }

                Stats.Increment(pip, serialize: false);
            }

            return(successful);
        }