René's URL Explorer Experiment


Title: `+=` copies array many times · Issue #23899 · PowerShell/PowerShell · GitHub

Open Graph Title: `+=` copies array many times · Issue #23899 · PowerShell/PowerShell

X Title: `+=` copies array many times · Issue #23899 · PowerShell/PowerShell

Description: Summary of the new feature / enhancement I was looking into += as the canonical example of inefficient powershell code for my presentation at psconfeu, and noticed that doing += on 10k items allocates 1.5 GB of memory and forces many gar...

Open Graph Description: Summary of the new feature / enhancement I was looking into += as the canonical example of inefficient powershell code for my presentation at psconfeu, and noticed that doing += on 10k items alloca...

X Description: Summary of the new feature / enhancement I was looking into += as the canonical example of inefficient powershell code for my presentation at psconfeu, and noticed that doing += on 10k items alloca...

Opengraph URL: https://github.com/PowerShell/PowerShell/issues/23899

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`+=` copies array many times","articleBody":"### Summary of the new feature / enhancement\n\nI was looking into `+=` as the canonical example of inefficient powershell code for my presentation at psconfeu, and noticed that doing `+=` on 10k items allocates 1.5 GB of memory and forces many garbage collections. This is way more allocations than the common wisdom of \"it copies array once for every item\" suggests.\r\n\r\n```powershell\r\n$start = [GC]::GetTotalAllocatedBytes($true)\r\n\r\n$numbers = 1..10000 \r\n$newNumbers = @()\r\nforeach ($number in $numbers) {\r\n    $newNumbers += $number\r\n}\r\n\r\n$end = [GC]::GetTotalAllocatedBytes($true)\r\n\r\n\"$([System.Math]::Round(($end-$start) / 1GB, 2)) GB\"\r\n```\r\n\r\nThe relevant code in powershell:\r\n\r\nhttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs#L3519-L3536 \r\n\r\nThis code handles the array as enumerable of unknown size, instead of special casing it to avoid growing the list over and over as it resizes from the initial capacity of 4 items. So for example for the final items in my 10k array it grows the list 12 times per item.\r\n\r\nThis can be confirmed by profiling the code in VS allocation profiler, where many of the allocations are attributed to arrays that have sizes which are multiplies of 2.\r\n\r\n![image](https://github.com/PowerShell/PowerShell/assets/5735905/6531ed4f-3420-4b3c-a175-43434ee42763) \n\n### Proposed technical implementation details (optional)\n\nas suggested by @SeeminglyScience  \r\n\r\n> a simple optimization could be just adding a sizeHint param to AddObject\r\n\r\nOr \r\n\r\n> the binder, to emit different code gen based on the type","author":{"url":"https://github.com/nohwnd","@type":"Person","name":"nohwnd"},"datePublished":"2024-06-06T21:53:59Z","url":"https://github.com/PowerShell/PowerShell/issues/23899"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:200bf6fb-0736-fed2-6ff0-680973fb4f81
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA804:354CE1:22EBBE:305AD8:6AFDA069
html-safe-nonced3ca8cb38ab8c1eb477a8b77de84233444deda1084fddc921d0a41ab249c3037
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODA0OjM1NENFMToyMkVCQkU6MzA1QUQ4OjZBRkRBMDY5IiwidmlzaXRvcl9pZCI6IjE3Mzk4NDU0MjYxMDM1NTgyNDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac1d5c8ecb7422a8603920dd8f117c6dabdf8c4e40eb91b5a309d40e79f4a40598
hovercard-subject-tagissue:2339211352
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/23899/issue_layout
twitter:imagehttps://opengraph.githubassets.com/fe2f206bd5c6c10bbe55a3b10398d317ad61e6944c480a89a12ba454003ce2a5/PowerShell/PowerShell/issues/23899
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/fe2f206bd5c6c10bbe55a3b10398d317ad61e6944c480a89a12ba454003ce2a5/PowerShell/PowerShell/issues/23899
og:image:altSummary of the new feature / enhancement I was looking into += as the canonical example of inefficient powershell code for my presentation at psconfeu, and noticed that doing += on 10k items alloca...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamenohwnd
hostnamegithub.com
expected-hostnamegithub.com
Nonebc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea
turbo-cache-controlno-preview
go-importgithub.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git
octolytics-dimension-user_id11524380
octolytics-dimension-user_loginPowerShell
octolytics-dimension-repository_id49609581
octolytics-dimension-repository_nwoPowerShell/PowerShell
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id49609581
octolytics-dimension-repository_network_root_nwoPowerShell/PowerShell
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release927a6c5ab1c32b38830f1b9d3050c0b9e9aa92e1
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/PowerShell/PowerShell/issues/23899#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F23899
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F23899
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=PowerShell%2FPowerShell
Reloadhttps://github.com/PowerShell/PowerShell/issues/23899
Reloadhttps://github.com/PowerShell/PowerShell/issues/23899
Reloadhttps://github.com/PowerShell/PowerShell/issues/23899
PowerShellhttps://github.com/PowerShell
PowerShellhttps://github.com/PowerShell/PowerShell
Codehttps://github.com/PowerShell/PowerShell
Issues1.3k (1.3k)https://github.com/PowerShell/PowerShell/issues
Pull requests366 (366)https://github.com/PowerShell/PowerShell/pulls
Discussionshttps://github.com/PowerShell/PowerShell/discussions
Actionshttps://github.com/PowerShell/PowerShell/actions
Projectshttps://github.com/PowerShell/PowerShell/projects
Security and quality3 (3)https://github.com/PowerShell/PowerShell/security
Insightshttps://github.com/PowerShell/PowerShell/insights
https://github.com/PowerShell/PowerShell
New issuehttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/23899
New issuehttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/23899
#23901https://github.com/PowerShell/PowerShell/pull/23901
+= copies array many timeshttps://github.com/PowerShell/PowerShell/issues/23899#top
#23901https://github.com/PowerShell/PowerShell/pull/23901
Issue-Enhancementthe issue is more of a feature request than a bughttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Enhancement%22
https://github.com/nohwnd
Jakub Jareš (nohwnd)https://github.com/nohwnd
on Jun 6, 2024https://github.com/PowerShell/PowerShell/issues/23899#issue-2339211352
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs#L3519-L3536https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs#L3519-L3536
https://private-user-images.githubusercontent.com/5735905/337449190-6531ed4f-3420-4b3c-a175-43434ee42763.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3OTUwMDY4NzAsIm5iZiI6MTc5NTAwNjU3MCwicGF0aCI6Ii81NzM1OTA1LzMzNzQ0OTE5MC02NTMxZWQ0Zi0zNDIwLTRiM2MtYTE3NS00MzQzNGVlNDI3NjMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI2MTExOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNjExMThUMTI1NjEwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YTU0YmJlMjE2MzM4ZTQ4MWFlMjMyZjZiMTUxMDhkZjA5YWRhZmEyMGVlYzc4ZDJiZjAxMWY2N2NjZWMxNTU2MyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmcmVzcG9uc2UtY29udGVudC10eXBlPWltYWdlJTJGcG5nIn0.xwQJakCd6SOUUfRE9u7KiG814KeT11OP7bnJnXST8FA
Patrick Meinecke (@SeeminglyScience)https://github.com/SeeminglyScience
Jakub Jareš (nohwnd)https://github.com/nohwnd
Issue-Enhancementthe issue is more of a feature request than a bughttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Enhancement%22
Needs-TriageThe issue is new and needs to be triaged by a work group.https://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Needs-Triage%22
on Jun 6, 2024https://github.com/PowerShell/PowerShell/issues/23899#event-13072635159
https://github.com/StartAutomating
James Brundage (StartAutomating)https://github.com/StartAutomating
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2153631058
Jakub Jareš (@nohwnd)https://github.com/nohwnd
Patrick Meinecke (@SeeminglyScience)https://github.com/SeeminglyScience
https://github.com/rhubarb-geek-nz
rhubarb-geek-nzhttps://github.com/rhubarb-geek-nz
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2153687480
https://github.com/jborean93
Jordan Borean (jborean93)https://github.com/jborean93
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2153784242
#23901https://github.com/PowerShell/PowerShell/pull/23901
https://github.com/SeeminglyScience
Patrick Meinecke (SeeminglyScience)https://github.com/SeeminglyScience
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2153923246
https://github.com/StartAutomating
James Brundage (StartAutomating)https://github.com/StartAutomating
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2153935155
Patrick Meinecke (@SeeminglyScience)https://github.com/SeeminglyScience
https://github.com/SeeminglyScience
Patrick Meinecke (SeeminglyScience)https://github.com/SeeminglyScience
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2153944269
https://github.com/rhubarb-geek-nz
rhubarb-geek-nzhttps://github.com/rhubarb-geek-nz
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2154138746
https://github.com/iRon7
iRon7https://github.com/iRon7
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2154509771
increase assignment operator (+=)https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_assignment_operators#the-assignment-by-addition-operator
PowerShell Pipelinehttps://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pipelines
Rule request: Avoid using the Assignment by Addition Operator (+=) to build a collection #1934https://github.com/PowerShell/PSScriptAnalyzer/issues/1934
Why should I avoid using the increase assignment operator (+=) to create a collectionhttps://stackoverflow.com/questions/60708578/why-should-i-avoid-using-the-increase-assignment-operator-to-create-a-colle
Feature Request: Support +/-/+=/-= operators for all IList types #13152https://github.com/PowerShell/PowerShell/issues/13152
https://github.com/237dmitry
237dmitryhttps://github.com/237dmitry
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2154769921
https://github.com/nohwnd
Jakub Jareš (nohwnd)https://github.com/nohwnd
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2154907671
https://github.com/nohwnd/Profilerhttps://github.com/nohwnd/Profiler
https://camo.githubusercontent.com/9a3286eb18045fbcfee652472dd50bc4c592fd21764bce2c797ebc82be8c469c/68747470733a2f2f6f70656e67726170682e6769746875626173736574732e636f6d2f643763343161313562663161303235616463613234336165353837643734393466356661353130383739323062653934636530303566386131616433386533632f6e6f68776e642f50726f66696c6572
https://camo.githubusercontent.com/7e41d4698b37a89b2a8c06057583a5f48ce614b0fb7e2b93d27a2ec01c8f024b/68747470733a2f2f6769746875622e6769746875626173736574732e636f6d2f66617669636f6e732f66617669636f6e2e737667
GitHub - nohwnd/Profiler: Script, ScriptBlock and module performance profiler for PowerShell 5, and PowerShell 7.https://github.com/nohwnd/Profiler
https://github.com/237dmitry
237dmitryhttps://github.com/237dmitry
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2155103579
https://github.com/SeeminglyScience
Patrick Meinecke (SeeminglyScience)https://github.com/SeeminglyScience
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2155269203
https://github.com/mklement0
Michael Klement (mklement0)https://github.com/mklement0
on Jun 7, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2155650257
#5643https://github.com/PowerShell/PowerShell/issues/5643
https://github.com/SeeminglyScience
Patrick Meinecke (SeeminglyScience)https://github.com/SeeminglyScience
on Jun 11, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2161231102
https://github.com/nohwnd
Jakub Jareš (nohwnd)https://github.com/nohwnd
on Jun 12, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2162535049
https://github.com/SeeminglyScience
Patrick Meinecke (SeeminglyScience)https://github.com/SeeminglyScience
on Jun 12, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2163409234
Dongbo Wang (daxian-dbw)https://github.com/daxian-dbw
completedhttps://github.com/PowerShell/PowerShell/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#23901https://github.com/PowerShell/PowerShell/pull/23901
on Jun 17, 2024https://github.com/PowerShell/PowerShell/issues/23899#event-13192765103
https://github.com/apps/microsoft-github-policy-service
microsoft-github-policy-servicehttps://github.com/apps/microsoft-github-policy-service
on Jun 17, 2024https://github.com/PowerShell/PowerShell/issues/23899#issuecomment-2174610365
Microsoft GitHub Policy Servicehttps://aka.ms/1es-gitops
Jakub Jareš (@nohwnd)https://github.com/nohwnd
https://aka.ms/PSRepoFeedbackhttps://aka.ms/PSRepoFeedback
Microsoft Formshttps://aka.ms/PSRepoFeedback
microsoft-github-policy-servicehttps://github.com/apps/microsoft-github-policy-service
Needs-TriageThe issue is new and needs to be triaged by a work group.https://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Needs-Triage%22
on Jun 17, 2024https://github.com/PowerShell/PowerShell/issues/23899#event-13192766002
Sign up for freehttps://github.com/signup?return_to=https://github.com/PowerShell/PowerShell/issues/23899
Sign in to commenthttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/23899
Issue-Enhancementthe issue is more of a feature request than a bughttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Enhancement%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.