Skip to content

Commit 93f976e

Browse files
committed
flint: reserve vector capacity to reduce allocations
We mostly know the required vector size for the expression pointers. Reserve their size to avoid unnecessary allocations. I don't measure a performance improvement from this, but it is good practice.
1 parent 77e3d7f commit 93f976e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

sources/flintwrap.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void flint_final_cleanup_master(void) {
3030
WORD* flint_div(PHEAD WORD *a, WORD *b, const WORD must_fit_term) {
3131
// Extract expressions
3232
vector<WORD *> e;
33+
e.reserve(2);
3334
e.push_back(a);
3435
e.push_back(b);
3536
const bool with_arghead = false;
@@ -91,6 +92,7 @@ WORD* flint_factorize_dollar(PHEAD WORD *argin) {
9192
WORD* flint_gcd(PHEAD WORD *a, WORD *b, const WORD must_fit_term) {
9293
// Extract expressions
9394
vector<WORD *> e;
95+
e.reserve(2);
9496
e.push_back(a);
9597
e.push_back(b);
9698
const bool with_arghead = false;
@@ -111,6 +113,7 @@ WORD* flint_gcd(PHEAD WORD *a, WORD *b, const WORD must_fit_term) {
111113
WORD* flint_inverse(PHEAD WORD *a, WORD *b) {
112114
// Extract expressions
113115
vector<WORD *> e;
116+
e.reserve(2);
114117
e.push_back(a);
115118
e.push_back(b);
116119
const flint::var_map_t var_map = flint::get_variables(e, false, false);
@@ -131,6 +134,7 @@ WORD* flint_inverse(PHEAD WORD *a, WORD *b) {
131134
WORD* flint_mul(PHEAD WORD *a, WORD *b) {
132135
// Extract expressions
133136
vector<WORD *> e;
137+
e.reserve(2);
134138
e.push_back(a);
135139
e.push_back(b);
136140
const flint::var_map_t var_map = flint::get_variables(e, false, false);
@@ -159,6 +163,7 @@ WORD* flint_ratfun_add(PHEAD WORD *t1, WORD *t2) {
159163

160164
// Extract expressions: the num and den of both prf
161165
vector<WORD *> e;
166+
e.reserve(4);
162167
for (WORD *t=t1+FUNHEAD; t<t1+t1[1];) {
163168
e.push_back(t);
164169
NEXTARG(t);
@@ -224,6 +229,7 @@ int flint_ratfun_normalize(PHEAD WORD *term) {
224229
// Extract all variables in the polyfuns
225230
// Collect pointers to each relevant argument
226231
vector<WORD *> e;
232+
e.reserve(4); // There could be more than 4 args in principle, but 4 is probably common.
227233
for (WORD *t=term+1; t<tstop; t+=t[1]) {
228234
if (*t == AR.PolyFun) {
229235
for (WORD *t2 = t+FUNHEAD; t2<t+t[1];) {
@@ -259,6 +265,7 @@ int flint_ratfun_normalize(PHEAD WORD *term) {
259265
WORD* flint_rem(PHEAD WORD *a, WORD *b, const WORD must_fit_term) {
260266
// Extract expressions
261267
vector<WORD *> e;
268+
e.reserve(2);
262269
e.push_back(a);
263270
e.push_back(b);
264271
const bool with_arghead = false;

0 commit comments

Comments
 (0)