Fix #114742: Draw: Buffers never shrink #114857

Merged
Miguel Pozo merged 5 commits from pragma37/blender:pull-draw-memory-housekeeping into main 2023-11-20 12:23:22 +01:00
Member

The buffers from the new Draw Manager increase their size as needed, but they never shrink.

This adds a new optimize_size function to StorageArrayBuffer that can downsize the buffer following the same heuristic as get_or_resize. And applies it automatically on StorageVectorBuffer::clear().

Notes:
It may be best to use StorageVectorBuffer where possible, so this can be handled in a more automatic and error-proof way.

The way this is handled may cause a performance loss if the number of objects is always changing in the scene in a way that causes resizes every frame. It may be best to leave more room for max_size even if that means increasing memory use?
For example: size_t max_size = power_of_2_max_u(required_size * 2);

Also, in the case of StorageVectorBuffer::push_update() we should only push up to item_len_. But that may be best left for 4.1.

The buffers from the new Draw Manager increase their size as needed, but they never shrink. This adds a new `optimize_size` function to `StorageArrayBuffer` that can downsize the buffer following the same heuristic as `get_or_resize`. And applies it automatically on `StorageVectorBuffer::clear()`. Notes: It may be best to use `StorageVectorBuffer` where possible, so this can be handled in a more automatic and error-proof way. The way this is handled may cause a performance loss if the number of objects is always changing in the scene in a way that causes resizes every frame. It may be best to leave more room for `max_size` even if that means increasing memory use? For example: `size_t max_size = power_of_2_max_u(required_size * 2);` Also, in the case of `StorageVectorBuffer::push_update()` we should only push up to `item_len_`. But that may be best left for 4.1.
Miguel Pozo added this to the 4.0 milestone 2023-11-14 18:49:49 +01:00
Miguel Pozo added the
Module
EEVEE & Viewport
label 2023-11-14 18:49:49 +01:00
Miguel Pozo requested review from Clément Foucault 2023-11-14 18:50:24 +01:00
Miguel Pozo requested review from Jeroen Bakker 2023-11-14 18:50:34 +01:00
Miguel Pozo force-pushed pull-draw-memory-housekeeping from 2c856692d1 to 4b1defa78d 2023-11-14 18:52:19 +01:00 Compare
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR114857) when ready.
Clément Foucault requested changes 2023-11-14 20:02:52 +01:00
Clément Foucault left a comment
Member

I think the heuristic would be to use a hysteresis threshold in the draw manager itself since the counts are stored there.

Usually, try following what the BLI Vector API does in terms of convention.

We cannot use StorageVectorBuffer since it would mean all items in it are stored contiguously which might not the case of most of the draw manager buffers (some buffers have holes IIRC).

I think the heuristic would be to use a hysteresis threshold in the draw manager itself since the counts are stored there. Usually, try following what the BLI Vector API does in terms of convention. We cannot use `StorageVectorBuffer` since it would mean all items in it are stored contiguously which might not the case of most of the draw manager buffers (some buffers have holes IIRC).
@ -367,2 +367,4 @@
}
/* Downsize the buffer if current size is much larger than the required size. */
void optimize_size(int64_t required_size)

Rename to resize_to_next_power_of_2(int64_t new_size).
Don't forget to remove the check. There is already a check in resize() if size doesn't change.

Rename to `resize_to_next_power_of_2(int64_t new_size)`. Don't forget to remove the check. There is already a check in `resize()` if size doesn't change.
Author
Member

But that could actually grow the buffer if it was resized manually.
Also, I think we may want to eventually use a different heuristic.
At some point, twice the size can be just too much.

But that could actually grow the buffer if it was resized manually. Also, I think we may want to eventually use a different heuristic. At some point, twice the size can be just too much.

If the user set the size directly then it there's no incentive to use optimize_size. The resize_to_next_power_of_2 is just to use the same heuristic as get_or_resize.

I think we could make it linear after a certain point, but that would require more testing in order to judge what are the best values.

If the user set the size directly then it there's no incentive to use `optimize_size`. The `resize_to_next_power_of_2` is just to use the same heuristic as `get_or_resize`. I think we could make it linear after a certain point, but that would require more testing in order to judge what are the best values.
@ -404,0 +412,4 @@
* Set item count to zero.
* If do_optimize_size is true, it may resize the buffer,
* but never to a capacity less than the current count.
* Otherwise it wont not free memory or resize the buffer.

wont not

Double negation.

> wont not Double negation.
pragma37 marked this conversation as resolved
@ -404,2 +415,3 @@
* Otherwise it wont not free memory or resize the buffer.
*/
void clear()
void clear(bool do_optimize_size = true)

Use clear_and_trim() for function name. Split to another function for better semantic and to avoid bugs caused by the change of behavior (but does not free memory or resize the buffer is vastly different from it may resize the buffer).

Change the usage on a per case basis (in separate commits to allow bisecting) and validate it works fine.

Use `clear_and_trim()` for function name. Split to another function for better semantic and to avoid bugs caused by the change of behavior (`but does not free memory or resize the buffer` is vastly different from `it may resize the buffer`). Change the usage on a per case basis (in separate commits to allow bisecting) and validate it works fine.
pragma37 marked this conversation as resolved
@ -543,2 +546,4 @@
void clear()
{
group_buf_.optimize_size(group_count_);
command_buf_.optimize_size(group_count_ * 2);

This group_count_ * 2 is not explained and not obvious.

This `group_count_ * 2` is not explained and not obvious.
pragma37 marked this conversation as resolved
Miguel Pozo changed title from Fix #114742: Draw: Buffers never shrink to Fix #114742: Draw: Buffers never shrink 2023-11-17 17:27:11 +01:00
pragma37 changed target branch from blender-v4.0-release to main 2023-11-17 17:27:17 +01:00
Miguel Pozo force-pushed pull-draw-memory-housekeeping from 90b9c45e58 to 1a775040a8 2023-11-17 17:30:18 +01:00 Compare
Clément Foucault approved these changes 2023-11-17 23:05:38 +01:00
Miguel Pozo merged commit 4dc1c23384 into main 2023-11-20 12:23:22 +01:00
Miguel Pozo deleted branch pull-draw-memory-housekeeping 2023-11-20 12:23:24 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#114857
No description provided.