InsightSoftwareConsortium / InsightSoftwareConsortium/ITK

`NumericTraits<VariableLengthVector<T>>::SetLength` shall avoid allocations

Open
#2,312 3 comments 0 reactions 0 assignees View on GitHub
status:Use_Milestone_Backlog type:Performance
Dominant language
C++
Stars
1.7k
Forks
748
Avg merge
1d 1h
Merged PRs (30d)
64

Description

### Description

Instead of `m.SetSize(s);`, `NumericTraits>::SetLength()` should use either

- `m.SetSize(s, false);`
- or the more explicit `SetSize(s, ShrinkToFit(), KeepOldValues());`
- or may be other policies, I'm not sure what a better default would be.

### Impact analysis

Any generic filter that uses any scalar pixel, or even `VariableLengthVector` pixels and resize them in a loop, could avoid unwelcomed deallocations+reallocations. Of course, the filter could be written differently.

### Expected behavior

Don't deallocate + reallocate the exact same amount of memory for an existing VLV pixel.

The exact performance improvement will highly depend on the filters using the resizing function from the trait.

### Actual behavior

When I've optimized VLV internals a few years ago, and provided reallocation policies at the time, I did not want to change default behaviours in places I wasn't sure. In particular I've decided that the default behaviour of `vlv.SetSize(42);` would be the same as before: deallocate and reallocate. I'm still not sure for this case as the end user can always use the other flavours instead.

However, when using the generic `NumericTraits>::SetLength()`, there is no way to specify the reallocation policy, and I think now this is not a god default behaviour.

### Versions

From 4.? till the current version I see on master (5.2rc02)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.