vtkAbstractArray: Replace most of GetVoidPointer instances#
vtkAbstractArray::GetVoidPointer() is a function that returns a raw pointer to the underlying data of a VTK array.
This function was added in VTK’s early days when it only supported vtkAOSDataArrayTemplate arrays. Since then,
VTK has added support for multiple other array types, either explicit (e.g., vtkSOADataArrayTemplate,
vtkScaledSOADataArrayTemplate, etc.) or implicit (e.g., vtkAffineArray, vtkConstantArray, etc.). These array types
don’t store their data in a contiguous block of memory, so using GetVoidPointer() leads to duplication of data in
an internal vtkAOSDataArrayTemplate array, which leads to memory usage increase and unnecessary copying overhead.
In attempt to modernize VTK and reduce unnecessary data duplication and copying overhead, ~75% (428/574) of the
GetVoidPointer() usages have been removed using one of the following techniques:
If array is
vtkAOSDataArrayTemplate, useGetPointer()instead ofGetVoidPointer().If array is
vtkSOADataArrayTemplate, and only a single component is needed, useGetComponentArrayPointer()instead ofGetVoidPointer().If array is
vtkDataArrayand all tuples need to be filled, useFillinstead ofmemset()withGetVoidPointer().If array is
vtkDataArrayand all tuples need to be copied, useDeepCopy()/ShallowCopy()instead ofmemcpy()withGetVoidPointer().If array is
vtkAbstractArrayand some tuples need to be copied, useInsertTuples*()methods instead ofGetVoidPointer()andmemcpy().If two
vtkAbstractArrays need to be compared, usevtkTestUtilities::CompareAbstractArray()instead ofGetVoidPointer()andstd::equal().If array is
vtkAbstractArrayand values are needed asvtkVariant, useGetVariantValue()instead ofvtkExtraExtendedTemplateMacrowithGetVoidPointer().If array is
vtkDataArrayand access to the tuples/values is needed, usevtkArrayDispatchandvtk::DataArray(Value/Tuple)Range(), instead ofvtkTemplateMacrowithGetVoidPointer().If array is
vtkDataArray, the data type is known, and raw pointer to data is absolutely needed, useauto aos = array->ToAOSDataArray(), and then usevtkAOSDataArrayTemplate<Type>::FastDownCast(aos)->GetPointer(), instead ofGetVoidPointer(), being aware that this may lead to data duplication if the array is notvtkAOSDataArrayTemplate.
Moreover, 17% (25/146) of the remaining GetVoidPointer() usages have been marked as safe using the clang-tidy lint
(bugprone-unsafe-functions) when one of the following conditions is met:
If array is
vtkDataArray, but usingHasStandardMemoryLayout, it has been verified that it actually isvtkAOSDataArrayTemplate, thenGetVoidPointer()is safe to use. This usually happens when the array was created usingvtkDataArray::CreateDataArray()orvtkAbstractArray::CreateArray()by passing a standard VTK data type.If array is
vtkDataArray, the data type is NOT known, but raw pointer to data is absolutely needed, useauto aos = array->ToAOSDataArray(), and then useaos->GetVoidPointer(), which is safe, being aware that this may lead to data duplication if the array is notvtkAOSDataArrayTemplate.
In the future, GetVoidPointer() will be marked as an unsafe function using clang-tidy, and its usage will be warned
as unsafe, unless explicitly marked as safe using the (bugprone-unsafe-functions) lint, upon guaranteeing that it is
indeed safe to use it in that specific context.