Title: ffi: getFunction() and the functions accessor rebuild wrappers on every call · Issue #64970 · nodejs/node · GitHub
Open Graph Title: ffi: getFunction() and the functions accessor rebuild wrappers on every call · Issue #64970 · nodejs/node
X Title: ffi: getFunction() and the functions accessor rebuild wrappers on every call · Issue #64970 · nodejs/node
Description: Version main Platform Linux Subsystem ffi What steps will reproduce the bug? repro.c #include
Open Graph Description: Version main Platform Linux Subsystem ffi What steps will reproduce the bug? repro.c #include
X Description: Version main Platform Linux Subsystem ffi What steps will reproduce the bug? repro.c #include <stdint.h> int32_t add2(int32_t a, int32_t b) { return a + b; } repro.js import { DynamicLibrary,...
Opengraph URL: https://github.com/nodejs/node/issues/64970
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ffi: getFunction() and the functions accessor rebuild wrappers on every call","articleBody":"### Version\n\nmain\n\n### Platform\n\n```text\nLinux\n```\n\n### Subsystem\n\nffi\n\n### What steps will reproduce the bug?\n\n`repro.c`\n```c\n#include \u003cstdint.h>\n\nint32_t add2(int32_t a, int32_t b) {\n return a + b;\n}\n```\n\n`repro.js`\n```js\nimport { DynamicLibrary, suffix } from 'node:ffi';\nimport { stat } from 'node:fs/promises';\n\nconst libPath = new URL(`./repro.${suffix}`, import.meta.url).pathname;\nawait stat(libPath);\n\nconst lib = new DynamicLibrary(libPath);\nlib.getFunction('add2', { arguments: ['i32', 'i32'], return: 'i32' });\n\nconsole.log('same wrapper on two reads:', lib.functions.add2 === lib.functions.add2);\n\n// Idiomatic call through the accessor, which re-resolves on every iteration.\nconst before = process.memoryUsage.rss();\nlet sum = 0;\nfor (let i = 0; i \u003c 20000; i++) sum += lib.functions.add2(20, 22);\nconst after = process.memoryUsage.rss();\n\nconsole.log(`sum: ${sum}`);\nconsole.log(`rss growth: ${((after - before) / 1024 / 1024).toFixed(1)} MiB`);\n\nlib.close();\n```\n\nCommands to run:\n```console\n$ cc -shared -fPIC -O2 -o repro.so repro.c\n\n$ node --experimental-ffi repro.js\n```\n\n### How often does it reproduce? Is there a required condition?\n\nAlways\n\n### What is the expected behavior? Why is that the expected behavior?\n\nReading an already-resolved function off `library.functions` returns the same wrapper each time, and calling through the accessor in a loop does not grow memory.\n\n### What do you see instead?\n\n```console\nsame wrapper on two reads: false\nsum: 840000\nrss growth: 56.0 MiB\n```\n\nEach read builds a new wrapper, so `lib.functions.add2 !== lib.functions.add2`, and the loop allocates a fresh trampoline page plus a new `FFIFunctionInfo` per iteration, holding all of them until GC.\n\n\n\n### Additional information\n\n_No response_","author":{"url":"https://github.com/trivikr","@type":"Person","name":"trivikr"},"datePublished":"2026-08-02T23:10:50Z","url":"https://github.com/nodejs/node/issues/64970"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:cfa66f75-e334-8627-f5e7-40c46f1108a2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8068:3CE69D:2FED22:3E12BC:6AED1DB3 |
| html-safe-nonce | daa7aaafccf51ae67f4cba27400fdec75e70c06ea58743bb1f95fa3cea0b6cd4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDY4OjNDRTY5RDoyRkVEMjI6M0UxMkJDOjZBRUQxREIzIiwidmlzaXRvcl9pZCI6IjE3NTc1NTIwOTQ1MDAxNjcwOTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 96f3788c2327a33a2d6832566d31db89159dbcec0b67dda1e516c568ebc63a30 |
| hovercard-subject-tag | issue:5044927444 |
| 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/nodejs/node/64970/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1c97c3f326713c8e46edaf6fb8ffb5934237374a7b6e950b4379e097bac71e05/nodejs/node/issues/64970 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1c97c3f326713c8e46edaf6fb8ffb5934237374a7b6e950b4379e097bac71e05/nodejs/node/issues/64970 |
| og:image:alt | Version main Platform Linux Subsystem ffi What steps will reproduce the bug? repro.c #include |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trivikr |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3e0f7f63e50c6fe879198a93a0a023a648ecc0a5e9bc3b5e033ac57172237e6 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| 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 | 707a24b346ac8a82878b9c084efaa38a997660cb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width