Warning, /sdk/clazy/docs/checks/README-reserve-candidates.md is written in an unsupported language. File is not indexed.
0001 # reserve-candidates 0002 0003 0004 Finds places that could use a `reserve()` call. 0005 Whenever you know how many elements a container will hold you should reserve 0006 space in order to avoid repeated memory allocations. 0007 0008 #### Trivial example missing reserve() 0009 0010 QList<int> ages; 0011 // list.reserve(people.size()); 0012 for (auto person : people) 0013 list << person.age(); 0014 0015 Example where reserve shouldn't be used: 0016 0017 QList<int> list; 0018 for (int i = 0; i < 1000; ++i) { 0019 // reserve() will be called 1000 times, meaning 1000 allocations 0020 // whilst without a reserve the internal exponential growth algorithm would do a better job 0021 list.reserve(list.size() + 2); 0022 for (int j = 0; j < 2; ++j) { 0023 list << m; 0024 } 0025 } 0026 0027 #### Supported containers 0028 `QVector`, `std::vector`, `QList`, `QSet` and `QVarLengthArray` 0029 0030 #### Pitfalls 0031 Rate of false-positives is around 15%. Don't go blindly calling `reserve()` without proper analysis. 0032 In doubt don't use it, all containers have a growth curve and usually only do log(N) allocations 0033 when you append N items.