nvk-struct-cache

Automatic Structure caching for node-vulkan

Usage no npm install needed!

<script type="module">
  import nvkStructCache from 'https://cdn.skypack.dev/nvk-struct-cache';
</script>

README

nvk-struct-cache

🍣 A Rollup plugin for automated Structure caching for node-vulkan

Imagine the following situation:

let commandBuffers = [...Array(8)].map(() => new VkCommandBuffer());
for (let ii = 0; ii < commandBuffers.length; ++ii) {
  let commandBufferBeginInfo = new VkCommandBufferBeginInfo();
  vkBeginCommandBuffer(commandBuffers[ii], cmdBufferBeginInfo);
};

This results in 8 allocations of VkCommandBufferBeginInfo structures. When this code gets executed in frequently used code sections, the heap pressure will be high.

Now nvk has a mechanism to simulate stack allocation:

let commandBuffers = [...Array(8)].map(() => new VkCommandBuffer());
for (let ii = 0; ii < commandBuffers.length; ++ii) {
  let commandBufferBeginInfo = VkCommandBufferBeginInfo("0x0"); // 0x0 inserted by this package
  vkBeginCommandBuffer(commandBuffers[ii], cmdBufferBeginInfo);
};

On the first iteration of the loop, a VkCommandBufferBeginInfo structure is allocated on the heap but also gets cached internally. Whenever this code gets executed again, the cached structure will be used instead of a new one.

Install

Using npm:

npm install nvk-struct-cache --save-dev

Usage

Create a rollup.config.js configuration file and import the plugin:

import structCache from "nvk-struct-cache";

export default {
  input: "src/index.mjs",
  output: {
    file: "./bundle.js",
    format: "cjs"
  },
  plugins: [
    structCache({ })
  ]
};

Then call rollup either via the CLI or the API.