Esempio n. 1
0
        public void TestDefault()
        {
            var listPool = SharedPools.BigDefault <List <int> >();

            Assert.IsAssignableFrom <ObjectPool <List <int> > >(listPool);

            var stackPool = SharedPools.BigDefault <Stack <int> >();

            Assert.IsAssignableFrom <ObjectPool <Stack <int> > >(stackPool);

            Assert.NotSame(listPool, stackPool);
        }
Esempio n. 2
0
                protected override void Dispose_NoLock()
                {
                    foreach (var map in _documentWorkQueue.Values)
                    {
                        foreach (var workItem in map.Values)
                        {
                            workItem.AsyncToken.Dispose();
                        }

                        SharedPools.BigDefault <Dictionary <DocumentId, WorkItem> >().ClearAndFree(map);
                    }

                    _documentWorkQueue.Clear();
                }
                protected override bool AddOrReplace_NoLock(WorkItem item)
                {
                    Contract.ThrowIfNull(item.DocumentId);

                    Cancel_NoLock(item.DocumentId);

                    // see whether we need to update
                    var key = item.DocumentId;

                    // now document work
                    if (
                        _documentWorkQueue.TryGetValue(key.ProjectId, out var documentMap) &&
                        documentMap.TryGetValue(key, out var existingWorkItem)
                        )
                    {
                        // TODO: should I care about language when replace it?
                        Debug.Assert(existingWorkItem.Language == item.Language);

                        // replace it
                        documentMap[key] = existingWorkItem.With(
                            item.InvocationReasons,
                            item.ActiveMember,
                            item.SpecificAnalyzers,
                            item.IsRetry,
                            item.AsyncToken
                            );
                        return(false);
                    }

                    // add document map if it is not already there
                    if (documentMap == null)
                    {
                        documentMap = SharedPools
                                      .BigDefault <Dictionary <DocumentId, WorkItem> >()
                                      .AllocateAndClear();
                        _documentWorkQueue.Add(key.ProjectId, documentMap);

                        if (_documentWorkQueue.Count == 1)
                        {
                            Logger.Log(FunctionId.WorkCoordinator_AsyncWorkItemQueue_FirstItem);
                        }
                    }

                    // okay, it is new one
                    // always hold onto the most recent one for the same document
                    documentMap.Add(key, item);

                    return(true);
                }
Esempio n. 4
0
                protected override bool TryTake_NoLock(DocumentId key, out WorkItem workInfo)
                {
                    workInfo = default;
                    if (_documentWorkQueue.TryGetValue(key.ProjectId, out var documentMap) &&
                        documentMap.TryGetValue(key, out workInfo))
                    {
                        documentMap.Remove(key);

                        if (documentMap.Count == 0)
                        {
                            _documentWorkQueue.Remove(key.ProjectId);
                            SharedPools.BigDefault <Dictionary <DocumentId, WorkItem> >().ClearAndFree(documentMap);
                        }

                        return(true);
                    }

                    return(false);
                }
Esempio n. 5
0
        private static IList <SyntaxToken> GetTokensFromText(
            ISyntaxFactsService syntaxFacts, Document document, VersionStamp version, SyntaxNode root,
            SourceText content, string text, Func <SyntaxToken, bool> candidate, CancellationToken cancellationToken)
        {
            if (text.Length > 0)
            {
                using (var positions = SharedPools.BigDefault <List <int> >().GetPooledObject())
                {
                    if (SyntaxTreeIdentifierInfo.TryGetIdentifierLocations(document, version, text, positions.Object, cancellationToken))
                    {
                        return(GetTokensFromText(root, positions.Object, text, candidate, cancellationToken).ToList());
                    }
                }

                return(GetTokensFromText(syntaxFacts, root, content, text, candidate, cancellationToken).ToList());
            }

            return(SpecializedCollections.EmptyList <SyntaxToken>());
        }
        private void Free(Dictionary <string, List <int> > map)
        {
            if (map == null)
            {
                return;
            }

            foreach (var value in map.Values)
            {
                if (value == null)
                {
                    continue;
                }

                SharedPools.BigDefault <List <int> >().ClearAndFree(value);
            }

            SharedPools.StringIgnoreCaseDictionary <List <int> >().ClearAndFree(map);
        }
        private static Dictionary <string, List <int> > CreateIdentifierLocations(Document document, SyntaxNode root, CancellationToken cancellationToken)
        {
            var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

            var identifierMap = SharedPools.StringIgnoreCaseDictionary <List <int> >().AllocateAndClear();

            foreach (var token in root.DescendantTokens(descendIntoTrivia: true))
            {
                if (token.IsMissing || token.Span.Length == 0)
                {
                    continue;
                }

                if (syntaxFacts.IsIdentifier(token) || syntaxFacts.IsGlobalNamespaceKeyword(token))
                {
                    var valueText = token.ValueText;
                    identifierMap.GetOrAdd(valueText, _ => SharedPools.BigDefault <List <int> >().AllocateAndClear()).Add(token.Span.Start);
                }
            }

            return(identifierMap);
        }