{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/comments/60565021","id":60565021,"diff_hunk":"@@ -0,0 +1,102 @@\n+﻿// Licensed to the .NET Foundation under one or more agreements.\n+// The .NET Foundation licenses this file to you under the MIT license.\n+// See the LICENSE file in the project root for more information.\n+\n+using System.Diagnostics;\n+using System.Threading;\n+\n+namespace System.Collections.Generic\n+{\n+    internal class DefaultObjectPool<T> : ObjectPool<T> where T : class, new()\n+    {\n+        /// <summary>The default maximum number of oebjects that are available for rent.</summary>\n+        private const int DefaultMaxPooled = 128;\n+\n+        private readonly T[] _objects;\n+\n+        private SpinLock _lock; // do not make this readonly; it's a mutable struct\n+        private int _index;\n+\n+        /// <summary>\n+        /// Creates the pool with DefaultMaxPooled objects.\n+        /// </summary>\n+        public DefaultObjectPool()\n+            : this(DefaultMaxPooled)\n+        {\n+        }\n+\n+        /// <summary>\n+        /// Creates the pool with maxPooled objects.\n+        /// </summary>\n+        public DefaultObjectPool(int maxPooled)\n+        {\n+            if (maxPooled <= 0)\n+            {\n+                throw new ArgumentOutOfRangeException(nameof(maxPooled));\n+            }\n+\n+            _lock = new SpinLock(Debugger.IsAttached); // only enable thread tracking if debugger is attached; it adds non-trivial overheads to Enter/Exit\n+            _objects = new T[maxPooled];\n+        }\n+\n+        /// <summary>Takes an object from the pool. If the pool is empty, returns a new object.</summary>","path":"src/mscorlib/src/System/Collections/Generic/DefaultObjectPool.cs","position":42,"original_position":42,"commit_id":"e089bc69605759c362e418c1b18e76b6429b985e","original_commit_id":"e089bc69605759c362e418c1b18e76b6429b985e","user":{"login":"mikedn","id":9973420,"avatar_url":"https://avatars.githubusercontent.com/u/9973420?v=3","gravatar_id":"","url":"https://api.github.com/users/mikedn","html_url":"https://github.com/mikedn","followers_url":"https://api.github.com/users/mikedn/followers","following_url":"https://api.github.com/users/mikedn/following{/other_user}","gists_url":"https://api.github.com/users/mikedn/gists{/gist_id}","starred_url":"https://api.github.com/users/mikedn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikedn/subscriptions","organizations_url":"https://api.github.com/users/mikedn/orgs","repos_url":"https://api.github.com/users/mikedn/repos","events_url":"https://api.github.com/users/mikedn/events{/privacy}","received_events_url":"https://api.github.com/users/mikedn/received_events","type":"User","site_admin":false},"body":"How is the new object created?","created_at":"2016-04-21T11:29:13Z","updated_at":"2016-04-21T11:29:13Z","html_url":"https://github.com/dotnet/coreclr/pull/4468#discussion_r60565021","pull_request_url":"https://api.github.com/repos/dotnet/coreclr/pulls/4468","_links":{"self":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/comments/60565021"},"html":{"href":"https://github.com/dotnet/coreclr/pull/4468#discussion_r60565021"},"pull_request":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/4468"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/4468","id":67322828,"html_url":"https://github.com/dotnet/coreclr/pull/4468","diff_url":"https://github.com/dotnet/coreclr/pull/4468.diff","patch_url":"https://github.com/dotnet/coreclr/pull/4468.patch","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/4468","number":4468,"state":"open","locked":false,"title":"[Discuss] Avoid IEnumeration allocation (List+Dictionary)","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"body":"(For discussion) 1M enumerations from 1M objects at 40 MBytes to 5 objects at 1,224 bytes\r\n\r\nThe common `IEnumerator` interface `foreach` code\r\n```csharp\r\nIList<int> list = new List<int>();\r\nforeach (var value in list)\r\n{\r\n    // ...\r\n}\r\n```\r\nTurns into something like\r\n```csharp\r\nIEnumerable<int> enum = list.GetEnumerator();\r\ntry\r\n{\r\n    while (enum.MoveNext())\r\n    {\r\n        var value = enum.Current;\r\n        // ...\r\n    }\r\n}\r\nfinally\r\n{\r\n    enum.Dispose();\r\n}\r\n```\r\nWhich boxes the struct enumerator to the interface and causes allocation.\r\n\r\nThis change introduces `ObjectPool<T>`; closely based on `ArrayPool<T>` - as an `internal` type (so as not to expose new api).\r\n\r\n`List<T>` and `Dictionary<TKey, TValue>` use this shared pool for class enumerators which contain the struct enumerator. This allows no boxing/allocation (beyond the initial pool allocations); when the interface is used for List and Dictionary enumeration. \r\n\r\nThe enumerator is returned to the pool on the call to Dispose and the struct enumerator set to `default(Enumerator)`.\r\n\r\nInterface dispatch will still be slower than using the stuct enumerator however.\r\n\r\nResolves #4446 (Measurements below)\r\n\r\n/cc @rynowak, @stephentoub, @davidfowl, @jaredpar","created_at":"2016-04-21T09:29:40Z","updated_at":"2016-04-21T11:29:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c2c91ee7a315c646d7f09a7354cb3f48a8905124","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/coreclr/pulls/4468/commits","review_comments_url":"https://api.github.com/repos/dotnet/coreclr/pulls/4468/comments","review_comment_url":"https://api.github.com/repos/dotnet/coreclr/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/4468/comments","statuses_url":"https://api.github.com/repos/dotnet/coreclr/statuses/e089bc69605759c362e418c1b18e76b6429b985e","head":{"label":"benaadams:ienumerator-allocs","ref":"ienumerator-allocs","sha":"e089bc69605759c362e418c1b18e76b6429b985e","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"repo":{"id":30264189,"name":"coreclr","full_name":"benaadams/coreclr","owner":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/benaadams/coreclr","description":"This repo contains the .NET Core runtime, called CoreCLR. It is an execution engine for .NET apps, performing functions such as IL byte code loading, compilation to machine code and garbage collection. The repo contains the complete up-to-date CoreCLR codebase.","fork":true,"url":"https://api.github.com/repos/benaadams/coreclr","forks_url":"https://api.github.com/repos/benaadams/coreclr/forks","keys_url":"https://api.github.com/repos/benaadams/coreclr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/benaadams/coreclr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/benaadams/coreclr/teams","hooks_url":"https://api.github.com/repos/benaadams/coreclr/hooks","issue_events_url":"https://api.github.com/repos/benaadams/coreclr/issues/events{/number}","events_url":"https://api.github.com/repos/benaadams/coreclr/events","assignees_url":"https://api.github.com/repos/benaadams/coreclr/assignees{/user}","branches_url":"https://api.github.com/repos/benaadams/coreclr/branches{/branch}","tags_url":"https://api.github.com/repos/benaadams/coreclr/tags","blobs_url":"https://api.github.com/repos/benaadams/coreclr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/benaadams/coreclr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/benaadams/coreclr/git/refs{/sha}","trees_url":"https://api.github.com/repos/benaadams/coreclr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/benaadams/coreclr/statuses/{sha}","languages_url":"https://api.github.com/repos/benaadams/coreclr/languages","stargazers_url":"https://api.github.com/repos/benaadams/coreclr/stargazers","contributors_url":"https://api.github.com/repos/benaadams/coreclr/contributors","subscribers_url":"https://api.github.com/repos/benaadams/coreclr/subscribers","subscription_url":"https://api.github.com/repos/benaadams/coreclr/subscription","commits_url":"https://api.github.com/repos/benaadams/coreclr/commits{/sha}","git_commits_url":"https://api.github.com/repos/benaadams/coreclr/git/commits{/sha}","comments_url":"https://api.github.com/repos/benaadams/coreclr/comments{/number}","issue_comment_url":"https://api.github.com/repos/benaadams/coreclr/issues/comments{/number}","contents_url":"https://api.github.com/repos/benaadams/coreclr/contents/{+path}","compare_url":"https://api.github.com/repos/benaadams/coreclr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/benaadams/coreclr/merges","archive_url":"https://api.github.com/repos/benaadams/coreclr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/benaadams/coreclr/downloads","issues_url":"https://api.github.com/repos/benaadams/coreclr/issues{/number}","pulls_url":"https://api.github.com/repos/benaadams/coreclr/pulls{/number}","milestones_url":"https://api.github.com/repos/benaadams/coreclr/milestones{/number}","notifications_url":"https://api.github.com/repos/benaadams/coreclr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/benaadams/coreclr/labels{/name}","releases_url":"https://api.github.com/repos/benaadams/coreclr/releases{/id}","deployments_url":"https://api.github.com/repos/benaadams/coreclr/deployments","created_at":"2015-02-03T20:56:42Z","updated_at":"2015-06-11T05:46:49Z","pushed_at":"2016-04-21T09:26:02Z","git_url":"git://github.com/benaadams/coreclr.git","ssh_url":"git@github.com:benaadams/coreclr.git","clone_url":"https://github.com/benaadams/coreclr.git","svn_url":"https://github.com/benaadams/coreclr","homepage":"","size":76724,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"patch-1"}},"base":{"label":"dotnet:master","ref":"master","sha":"660f57009f5a0d2455b1b4e8f6bd2905dc12fe1a","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":30092893,"name":"coreclr","full_name":"dotnet/coreclr","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/coreclr","description":"This repo contains the .NET Core runtime, called CoreCLR, and the base library, called mscorlib. It includes the garbage collector, JIT compiler, base .NET data types and many low-level classes.","fork":false,"url":"https://api.github.com/repos/dotnet/coreclr","forks_url":"https://api.github.com/repos/dotnet/coreclr/forks","keys_url":"https://api.github.com/repos/dotnet/coreclr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/coreclr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/coreclr/teams","hooks_url":"https://api.github.com/repos/dotnet/coreclr/hooks","issue_events_url":"https://api.github.com/repos/dotnet/coreclr/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/coreclr/events","assignees_url":"https://api.github.com/repos/dotnet/coreclr/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/coreclr/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/coreclr/tags","blobs_url":"https://api.github.com/repos/dotnet/coreclr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/coreclr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/coreclr/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/coreclr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/coreclr/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/coreclr/languages","stargazers_url":"https://api.github.com/repos/dotnet/coreclr/stargazers","contributors_url":"https://api.github.com/repos/dotnet/coreclr/contributors","subscribers_url":"https://api.github.com/repos/dotnet/coreclr/subscribers","subscription_url":"https://api.github.com/repos/dotnet/coreclr/subscription","commits_url":"https://api.github.com/repos/dotnet/coreclr/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/coreclr/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/coreclr/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/coreclr/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/coreclr/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/coreclr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/coreclr/merges","archive_url":"https://api.github.com/repos/dotnet/coreclr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/coreclr/downloads","issues_url":"https://api.github.com/repos/dotnet/coreclr/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/coreclr/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/coreclr/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/coreclr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/coreclr/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/coreclr/releases{/id}","deployments_url":"https://api.github.com/repos/dotnet/coreclr/deployments","created_at":"2015-01-30T22:10:02Z","updated_at":"2016-04-21T10:55:30Z","pushed_at":"2016-04-21T09:29:42Z","git_url":"git://github.com/dotnet/coreclr.git","ssh_url":"git@github.com:dotnet/coreclr.git","clone_url":"https://github.com/dotnet/coreclr.git","svn_url":"https://github.com/dotnet/coreclr","homepage":"http://dotnet.github.io/","size":82841,"stargazers_count":6198,"watchers_count":6198,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1404,"mirror_url":null,"open_issues_count":787,"forks":1404,"open_issues":787,"watchers":6198,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/4468"},"html":{"href":"https://github.com/dotnet/coreclr/pull/4468"},"issue":{"href":"https://api.github.com/repos/dotnet/coreclr/issues/4468"},"comments":{"href":"https://api.github.com/repos/dotnet/coreclr/issues/4468/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/4468/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/4468/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/coreclr/statuses/e089bc69605759c362e418c1b18e76b6429b985e"}}}}