Skip to content

perf: optimize instanced buffer batching - #269

Merged
llsansun merged 4 commits into
cocos:v4.0.0from
llsansun:perf/optimize-instanced-buffer-clean
Aug 14, 2026
Merged

perf: optimize instanced buffer batching#269
llsansun merged 4 commits into
cocos:v4.0.0from
llsansun:perf/optimize-instanced-buffer-clean

Conversation

@llsansun

@llsansun llsansun commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Re: #

Changelog

  • Optimized instanced buffer batching by caching compatible instance groups to avoid repeatedly scanning all existing instance groups.
  • Reduced instance buffer upload size by uploading only the valid instance data range instead of the whole allocated buffer.
  • Refactored instance creation and append logic to make the instanced buffer update path clearer and easier to maintain.

Performance

This change optimizes the existing instancing path without changing when instancing is enabled. Materials still need to enable USE_INSTANCING explicitly.

Before this change, each incoming instanced model searched through all existing instance groups linearly to find a compatible group. As the number of instance groups increased, the CPU cost of InstancedBuffer.merge() grew accordingly.

After this change, compatible instance groups are cached by a key generated from the input assembler, lighting map, reflection probe textures, reflection probe type, and instance attribute stride. This allows the merge path to quickly find candidate groups and only scan groups that share the same compatibility key.

In addition, buffer uploads now use the valid instance data size, instance.count * instance.stride, instead of uploading the whole allocated backing buffer. This avoids uploading unused capacity when the instance buffer has grown but is not fully occupied.

Before profiling result:
1

3

After profiling result:
2


4


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

Test Plan

  • Verified a scene with many mesh renderers using the same mesh and material with USE_INSTANCING enabled.
  • Verified that all instances render correctly before and after this change.
  • Verified that non-instancing materials are not affected.
  • Verified that instances with different IA, lighting map, reflection probe textures, reflection probe type, or instance attribute stride are not merged together.
  • Verified that exceeding one instanced buffer capacity creates another compatible instance group correctly.
  • Verified that partial buffer upload works correctly after the buffer grows and the valid instance count becomes smaller in later frames.
  • Compared Chrome DevTools CPU profiles before and after this change and confirmed that InstancedBuffer.merge() cost is reduced.

@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1015126 bytes 1015084 bytes 👍 -42 bytes
2D All (legacy pipeline) 2682844 bytes 2682802 bytes 👍 -42 bytes
2D All (new pipeline) 2774681 bytes 2774639 bytes 👍 -42 bytes
(2D + 3D) All 10032007 bytes 10031965 bytes 👍 -42 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16868425 bytes 16868383 bytes 👍 -42 bytes

Interface Check Report

This pull request does not change any public interfaces !

@llsansun

Copy link
Copy Markdown
Contributor Author

@cocos-robot run test cases

@github-actions

Copy link
Copy Markdown

@llsansun, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@llsansun, Please check the result of run test cases:

Task Details

@llsansun

Copy link
Copy Markdown
Contributor Author

@cocos-robot run test cases

@llsansun
llsansun merged commit f948b59 into cocos:v4.0.0 Aug 14, 2026
14 checks passed
@github-actions

Copy link
Copy Markdown

@llsansun, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@llsansun, Please check the result of run test cases:

Task Details

@llsansun
llsansun deleted the perf/optimize-instanced-buffer-clean branch September 2, 2026 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants