Skip to content

Fix: Set Buffer data via rvalue instead of lvalue#1817

Merged
aljazkonec1 merged 2 commits into
developfrom
fix/setSegmentationMask_memory_handling
May 27, 2026
Merged

Fix: Set Buffer data via rvalue instead of lvalue#1817
aljazkonec1 merged 2 commits into
developfrom
fix/setSegmentationMask_memory_handling

Conversation

@aljazkonec1

@aljazkonec1 aljazkonec1 commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Purpose

Segmentation mask data is stored in std::shared_ptr<Memory> data. When setting the segmask via lvalue, it uses the following function:

    if(data->getMaxSize() >= d.size()) {
        // TODO(themarpe) - has to set offset as well
        memcpy(data->getData().data(), d.data(), d.size());
    } else {
        // allocate new holder
        data = std::make_shared<VectorMemory>(std::move(d));
    }
}

If the new d is smaller than the current VectorMemory size, then data->getSize() will return old memory size.

Setting a segmentation mask should replace the underlying memory or perform copy-on-write, not mutate shared storage in place. So setting via rvalue is needed:

void Buffer::setData(std::vector<std::uint8_t>&& d) {
   // allocate new holder
   data = std::make_shared<VectorMemory>(std::move(d));
   // *mem = std::move(d);
   // data = mem;
}

Specification

None / not applicable

Dependencies & Potential Impact

None / not applicable

Deployment Plan

None / not applicable

Testing & Validation

None / not applicable

AI Usage

Assisted-by: codex:5.4 xhigh

Submitted code was reviewed by a human: YES

The author is taking the responsibility for the contribution: YES

Summary by CodeRabbit

  • Refactor
    • Improved internal handling of segmentation masks and image detections to reduce memory copies and improve performance when setting or converting mask data.

Review Change Stack

@aljazkonec1 aljazkonec1 requested a review from moratom May 26, 2026 15:57
@aljazkonec1 aljazkonec1 self-assigned this May 26, 2026
@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5ac92084-4c18-4b1b-a996-18a8695f712e

📥 Commits

Reviewing files that changed from the base of the PR and between 537548a and 0b094c5.

📒 Files selected for processing (2)
  • src/pipeline/datatype/ImgDetectionsT.cpp
  • src/pipeline/datatype/SegmentationMask.cpp
📜 Recent review details
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-03-24T22:39:04.364Z
Learnt from: MaticTonin
Repo: luxonis/depthai-core PR: 1732
File: src/pipeline/Pipeline.cpp:705-705
Timestamp: 2026-03-24T22:39:04.364Z
Learning: Do not flag the `!= ""` part of the auto-calibration condition as redundant when it appears in `PipelineImpl::build()` (or closely related pipeline build logic). If the code uses `utility::getEnvAs<std::string>(..., default)` with a default such as `"ON_START"`, the explicit empty-string guard may still be intentional to treat an explicitly empty env var as “OFF/disabled” (or to avoid special-casing elsewhere). Only consider removing `!= ""` if the codebase has an explicit, enforceable guarantee that `DEPTHAI_AUTOCALIBRATION` can never be set to an empty string (e.g., via validated parsing/CI checks); otherwise, keep the guard.

Applied to files:

  • src/pipeline/datatype/ImgDetectionsT.cpp
  • src/pipeline/datatype/SegmentationMask.cpp
🔇 Additional comments (2)
src/pipeline/datatype/ImgDetectionsT.cpp (1)

31-32: LGTM!

Also applies to: 44-44, 92-92

src/pipeline/datatype/SegmentationMask.cpp (1)

8-8: LGTM!

Also applies to: 47-48, 245-245


📝 Walkthrough

Walkthrough

Replace in-place copies with move semantics for locally-constructed segmentation-mask buffers in ImgDetectionsT and SegmentationMask; <utility> is included where needed. No public APIs changed.

Changes

Move semantics optimization for segmentation mask handling

Layer / File(s) Summary
ImgDetectionsT segmentation-mask move optimization
src/pipeline/datatype/ImgDetectionsT.cpp
Adds <utility> and updates setSegmentationMask(const std::vector<uint8_t>&, ...), setSegmentationMask(dai::ImgFrame&), and setCvSegmentationMask(cv::Mat) to forward locally-constructed vectors into setData via std::move instead of copying.
SegmentationMask move optimization
src/pipeline/datatype/SegmentationMask.cpp
Adds <utility> and updates setMask(const std::vector<uint8_t>&, size_t, size_t) and setCvMask(cv::Mat) to move local buffers into setData rather than passing lvalues.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit nudges vectors light,
Hops them swiftly out of sight,
std::move hugs data tight and snug,
No extra copies, not a shrug. 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix: Set Buffer data via rvalue instead of lvalue' clearly and specifically summarizes the main change across both modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/setSegmentationMask_memory_handling

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pipeline/datatype/SegmentationMask.cpp`:
- Around line 46-47: The file uses std::move in this translation unit (e.g., in
the code around setData and other call sites like setData(std::move(...)) in
SegmentationMask.cpp) but does not include <utility>, relying on transitive
includes; add `#include` <utility> to SegmentationMask.cpp near the other includes
to make the dependency explicit, and if SegmentationMask.hpp itself uses
std::move or is included by other units, add `#include` <utility> to
SegmentationMask.hpp as well to avoid transitive-include fragility.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 793452da-d8eb-4114-b775-2adc9cf0eb75

📥 Commits

Reviewing files that changed from the base of the PR and between 5fe9eaa and 537548a.

📒 Files selected for processing (2)
  • src/pipeline/datatype/ImgDetectionsT.cpp
  • src/pipeline/datatype/SegmentationMask.cpp
📜 Review details
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-03-24T22:39:04.364Z
Learnt from: MaticTonin
Repo: luxonis/depthai-core PR: 1732
File: src/pipeline/Pipeline.cpp:705-705
Timestamp: 2026-03-24T22:39:04.364Z
Learning: Do not flag the `!= ""` part of the auto-calibration condition as redundant when it appears in `PipelineImpl::build()` (or closely related pipeline build logic). If the code uses `utility::getEnvAs<std::string>(..., default)` with a default such as `"ON_START"`, the explicit empty-string guard may still be intentional to treat an explicitly empty env var as “OFF/disabled” (or to avoid special-casing elsewhere). Only consider removing `!= ""` if the codebase has an explicit, enforceable guarantee that `DEPTHAI_AUTOCALIBRATION` can never be set to an empty string (e.g., via validated parsing/CI checks); otherwise, keep the guard.

Applied to files:

  • src/pipeline/datatype/SegmentationMask.cpp
  • src/pipeline/datatype/ImgDetectionsT.cpp
🔇 Additional comments (1)
src/pipeline/datatype/ImgDetectionsT.cpp (1)

7-7: LGTM!

Also applies to: 31-33, 44-46, 92-94

Comment thread src/pipeline/datatype/SegmentationMask.cpp Outdated
@aljazkonec1 aljazkonec1 added the testable PR is ready to be tested - run vanilla tests label May 27, 2026
@aljazkonec1 aljazkonec1 removed the testable PR is ready to be tested - run vanilla tests label May 27, 2026
@aljazkonec1 aljazkonec1 merged commit fd2a406 into develop May 27, 2026
1 check passed
@aljazkonec1 aljazkonec1 deleted the fix/setSegmentationMask_memory_handling branch May 27, 2026 10:51
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