-
Notifications
You must be signed in to change notification settings - Fork 27
minimize save/restore allocations #909
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ class CellMap | |
|
||
|
||
public: | ||
CellMap(Box<cell_index_t, dim> box) | ||
CellMap(Box<cell_index_t, dim> box = {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification 🛠️ Refactor suggestion Potential issues with empty The recent change allows creating a
Only one existing check for an empty Recommendations:
🔗 Analysis chainConsider the implications of allowing default construction with an empty Box The addition of a default argument Consider the following:
To verify the impact of this change, please run the following script: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for potential issues with empty Box usage
# Test: Search for methods that use box_ member
echo "Methods potentially affected by empty Box:"
rg --type cpp -n 'box_\.' src/core/utilities/cellmap.hpp
# Test: Check cellIndexes_ initialization
echo "\ncellIndexes_ initialization:"
rg --type cpp -n 'cellIndexes_.*box' src/core/utilities/cellmap.hpp
# Test: Search for existing null checks or empty box handling
echo "\nExisting empty box handling:"
rg --type cpp -n 'box_\.isEmpty\(\)|box_\.empty\(\)' src/core/utilities/cellmap.hpp
Length of output: 679 |
||
: box_{box} | ||
, cellIndexes_{box.shape().template toArray<std::uint32_t>()} | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef PHARE_CORE_VECTOR_HPP | ||
#define PHARE_CORE_VECTOR_HPP | ||
|
||
#include <vector> | ||
#include <cstdint> | ||
#include "core/utilities/span.hpp" | ||
|
||
namespace PHARE::core | ||
{ | ||
|
||
|
||
template<typename T, bool copy_old_ = true> | ||
struct MinimizingVector | ||
{ | ||
template<bool copy_old = copy_old_> | ||
auto& get(std::size_t const& s) | ||
{ | ||
if (s < v.capacity() * percentile) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential precision loss due to implicit conversion in comparison In line 18: if (s < v.capacity() * percentile) The expression Consider explicitly casting the result to if (s < static_cast<std::size_t>(v.capacity() * percentile)) This ensures that both sides of the comparison are of the same type, avoiding potential precision issues. |
||
++_c; | ||
else | ||
_c = 0; | ||
|
||
if (_c == period) | ||
{ | ||
std::vector<T> r(v.capacity() * realloc_to); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implicit conversion from In line 25: std::vector<T> r(v.capacity() * realloc_to); The expression Apply this fix to ensure the size is correctly calculated: - std::vector<T> r(v.capacity() * realloc_to);
+ std::vector<T> r(static_cast<std::size_t>(v.capacity() * realloc_to)); Alternatively, you can use rounding functions if partial capacities should influence the size: std::vector<T> r(static_cast<std::size_t>(std::ceil(v.capacity() * realloc_to))); |
||
if constexpr (copy_old) | ||
r = v; | ||
v = std::move(r); | ||
_c = 0; | ||
} | ||
|
||
v.resize(s); | ||
return v; | ||
} | ||
|
||
auto size() const { return v.size(); } | ||
auto capacity() const { return v.capacity(); } | ||
auto& operator()() { return v; } | ||
auto& operator()() const { return v; } | ||
auto& operator()(std::size_t const& s) { return get(s); } | ||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Overloading Overloading the function call operator to access or modify the internal vector can be confusing, as it's unconventional for object manipulation. Lines 38-40: auto& operator()() { return v; }
auto& operator()() const { return v; }
auto& operator()(std::size_t const& s) { return get(s); } Consider using explicit member function names for clarity:
Refactored example: auto& data() { return v; }
auto& data() const { return v; }
auto& resize(std::size_t const& s) { return get(s); } This makes the interface more intuitive and improves code expressiveness. |
||
|
||
double const percentile = .80; | ||
double const realloc_to = .90; | ||
std::size_t const period = 100; | ||
|
||
std::vector<T> v{}; | ||
std::uint16_t _c = 0; | ||
}; | ||
|
||
|
||
|
||
|
||
} // namespace PHARE::core | ||
|
||
#endif // PHARE_CORE_VECTOR_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure safe copying of particle data with capacity checks
When using
std::copy
to transfer particle data, it's important to ensure that the destination buffers have sufficient capacity to prevent buffer overflows. Consider adding assertions to verify the capacities before copying.Apply this diff to add capacity checks:
📝 Committable suggestion