Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory allocation #410

Merged
merged 6 commits into from
Jan 7, 2025
Merged

Conversation

tizianoGuadagnino
Copy link
Collaborator

@tizianoGuadagnino tizianoGuadagnino commented Dec 4, 2024

Motivation

After doing some heaptrack analysis in PRBonn/kinematic-icp/pull/22, it turns out that our beloved system performs an insane amount of unnecessary allocations.

This PR

This change can be synthesized with "don't let that vector to reallocate for each point".

std::vector<Voxel> GetAdjacentVoxels(const Voxel &voxel, int adjacent_voxels = 1) {
    std::vector<Voxel> voxel_neighborhood; // <--- NO RESERVE HAS BEEN CALLED
    for (int i = voxel.x() - adjacent_voxels; i < voxel.x() + adjacent_voxels + 1; ++i) {
        for (int j = voxel.y() - adjacent_voxels; j < voxel.y() + adjacent_voxels + 1; ++j) {
            for (int k = voxel.z() - adjacent_voxels; k < voxel.z() + adjacent_voxels + 1; ++k) {
                voxel_neighborhood.emplace_back(i, j, k);
            }
        }
    }
    return voxel_neighborhood;
}

This code does not preallocate memory, and even worse, this reallocation happens for each point in the scan for which we are computing associations. The funny thing is that we perfectly know how these voxel offsets look, so I just precompute them. The result in terms of the number of allocations is incredible.

Results

Memory allocations

pr_image1

@tizianoGuadagnino tizianoGuadagnino changed the title Reduce allocations -> Improve runtime Improve runtime by reshaping TBB Data Association Dec 4, 2024
@tizianoGuadagnino tizianoGuadagnino changed the title Improve runtime by reshaping TBB Data Association Reduce memory allocation Dec 4, 2024
@tizianoGuadagnino tizianoGuadagnino merged commit 4c2f57f into main Jan 7, 2025
19 checks passed
@tizianoGuadagnino tizianoGuadagnino deleted the tiziano/reduce_allocations branch January 7, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants