Title: `~lib/array/Array#push` is unnecessarily garbage-y · Issue #1798 · AssemblyScript/assemblyscript · GitHub
Open Graph Title: `~lib/array/Array#push` is unnecessarily garbage-y · Issue #1798 · AssemblyScript/assemblyscript
X Title: `~lib/array/Array#push` is unnecessarily garbage-y · Issue #1798 · AssemblyScript/assemblyscript
Description: In a benchmark I discovered that AssemblyScript is significantly slower than JavaScript when using --runtime incremental when filling Arrays. Language Runtime Average vs JS JavaScript 233.68ms 1.0x AssemblyScript stub 345.35ms 1.5x Assem...
Open Graph Description: In a benchmark I discovered that AssemblyScript is significantly slower than JavaScript when using --runtime incremental when filling Arrays. Language Runtime Average vs JS JavaScript 233.68ms 1.0x...
X Description: In a benchmark I discovered that AssemblyScript is significantly slower than JavaScript when using --runtime incremental when filling Arrays. Language Runtime Average vs JS JavaScript 233.68ms 1.0x...
Opengraph URL: https://github.com/AssemblyScript/assemblyscript/issues/1798
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`~lib/array/Array#push` is unnecessarily garbage-y","articleBody":"In a benchmark I discovered that AssemblyScript is _significantly_ slower than JavaScript when using `--runtime incremental` when filling Arrays.\r\n\r\nLanguage | Runtime | Average | vs JS\r\n-- | -- | -- | --\r\nJavaScript | | 233.68ms | 1.0x\r\nAssemblyScript | stub | 345.35ms | 1.5x\r\nAssemblyScript | minimal | 354.60ms | 1.5x\r\nAssemblyScript | incremental | 18758.50ms | 80.3x\r\n\r\n\r\nA `d8` trace revealed the following profile:\r\n\r\n```\r\n[Bottom up (heavy) profile]:\r\n ticks parent name\r\n 18670 96.1% /usr/lib/system/libsystem_platform.dylib\r\n 13530 72.5% Function: *~lib/rt/itcms/__renew\r\n 13530 100.0% Function: *~lib/array/ensureSize\r\n 13530 100.0% Function: *~lib/array/Array#push\r\n 13530 100.0% Function: *binaryheap_optimized/BinaryHeap#push\r\n 13530 100.0% Function: *binaryheap_optimized/push\r\n 5119 27.4% Function: *~lib/rt/itcms/__new\r\n 5119 100.0% Function: *~lib/rt/itcms/__renew\r\n 5119 100.0% Function: *~lib/array/ensureSize\r\n 5119 100.0% Function: *~lib/array/Array#push\r\n 5119 100.0% Function: *binaryheap_optimized/BinaryHeap#push\r\n```\r\n\r\nwhich led me to investigate the `Array#push()` [implementation](https://github.com/AssemblyScript/assemblyscript/blob/42c2dbc987c2e9f4096a226b62bbf0e72b4a0e51/std/assembly/array.ts#L204-L216), which calls`ensureSize()`. `ensureSize()` will create a new buffer with exactly one slot more if the previous buffer was full. This means that once you ran out of capacity once, every subsequent call to `push()` will create a new buffer copy all the data to the new buffer and create work for the garbage collector as the old buffer is now collectible.\r\n\r\nBoth [Rust](https://github.com/rust-lang/rust/blob/58f32da346642ff3f50186f6f4a0de46e61008be/library/alloc/src/raw_vec.rs#L431) and [Go](https://github.com/golang/go/blob/3f4977bd5800beca059defb5de4dc64cd758cbb9/src/runtime/slice.go#L144-L163) amortize that work by doubling the capacity every time the buffer is exhausted. I’m sure C++ does the same but I couldn’t get through the stdlib code 😅 \r\n\r\nIn an experiment, replacing the `~lib/array/Array` with a `CustomArray\u003cT>` that has exponential buffer growth, the performance problems go away:\r\n\r\n\r\nLanguage | Variant | Runtime | Average | vs JS\r\n-- | -- | -- | -- | --\r\nJavaScript | | | 233.68ms | 1.0x\r\nAssemblyScript | customarray | stub | 329.43ms | 1.4x\r\nAssemblyScript | customarray | minimal | 329.23ms | 1.4x\r\nAssemblyScript | customarray | incremental | 335.35ms | 1.4x\r\nAssemblyScript | ~lib | stub | 345.35ms | 1.5x\r\nAssemblyScript | ~lib | minimal | 354.60ms | 1.5x\r\nAssemblyScript | ~lib | incremental | 18758.50ms | 80.3x\r\n\r\nI would like to PR in a fix, but wanted to check first if there are any concerns about adopting a similar approach for ASC’s `Array\u003cT>`?","author":{"url":"https://github.com/surma","@type":"Person","name":"surma"},"datePublished":"2021-04-11T13:13:32Z","url":"https://github.com/AssemblyScript/assemblyscript/issues/1798"}
| react-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8767f1e7-4832-4fd4-35a4-53b859642406 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D322:1E56FC:631F15:840A91:6B00AA2F |
| html-safe-nonce | b27ce8dad3f333780076c4d1b448331c24b7d600de68f80145e2ae9c2bfa1261 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzIyOjFFNTZGQzo2MzFGMTU6ODQwQTkxOjZCMDBBQTJGIiwidmlzaXRvcl9pZCI6IjE4ODk4NDQyNTk4MTM2MzI1NTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8ab7081ce60b497315f06bf769ab4d2a0e819332a90eadba44efbf8c7758c7a3 |
| hovercard-subject-tag | issue:855304090 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/AssemblyScript/assemblyscript/1798/issue_layout |
| twitter:image | https://opengraph.githubassets.com/75d927b5168c92466ecff80e800dadd7cac91cb9a1f2e050d0a4adf67742f6be/AssemblyScript/assemblyscript/issues/1798 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/75d927b5168c92466ecff80e800dadd7cac91cb9a1f2e050d0a4adf67742f6be/AssemblyScript/assemblyscript/issues/1798 |
| og:image:alt | In a benchmark I discovered that AssemblyScript is significantly slower than JavaScript when using --runtime incremental when filling Arrays. Language Runtime Average vs JS JavaScript 233.68ms 1.0x... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | surma |
| hostname | github.com |
| expected-hostname | github.com |
| None | 455b493e3402a3a3a06beac5d20bb7db3da060808b220aa48d6a7eca7c59863a |
| turbo-cache-control | no-preview |
| go-import | github.com/AssemblyScript/assemblyscript git https://github.com/AssemblyScript/assemblyscript.git |
| octolytics-dimension-user_id | 28916798 |
| octolytics-dimension-user_login | AssemblyScript |
| octolytics-dimension-repository_id | 105138356 |
| octolytics-dimension-repository_nwo | AssemblyScript/assemblyscript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 105138356 |
| octolytics-dimension-repository_network_root_nwo | AssemblyScript/assemblyscript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2f575159976167242d488c0b062eb355c4e198b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width