Reserve space upfront when possible, to avoid re-allocations.
parent
fba88859e1
commit
12a1a35784
24
src/dsc.h
24
src/dsc.h
|
@ -162,9 +162,9 @@ public:
|
||||||
int n;
|
int n;
|
||||||
int elemsAllocated;
|
int elemsAllocated;
|
||||||
|
|
||||||
void AllocForOneMore() {
|
void ReserveMore(int howMuch) {
|
||||||
if(n >= elemsAllocated) {
|
if(n + howMuch > elemsAllocated) {
|
||||||
elemsAllocated = (elemsAllocated + 32)*2;
|
elemsAllocated = n + howMuch;
|
||||||
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(elem[0]));
|
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(elem[0]));
|
||||||
for(int i = 0; i < n; i++) {
|
for(int i = 0; i < n; i++) {
|
||||||
new(&newElem[i]) T(std::move(elem[i]));
|
new(&newElem[i]) T(std::move(elem[i]));
|
||||||
|
@ -175,6 +175,12 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AllocForOneMore() {
|
||||||
|
if(n >= elemsAllocated) {
|
||||||
|
ReserveMore((elemsAllocated + 32)*2 - n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Add(const T *t) {
|
void Add(const T *t) {
|
||||||
AllocForOneMore();
|
AllocForOneMore();
|
||||||
new(&elem[n++]) T(*t);
|
new(&elem[n++]) T(*t);
|
||||||
|
@ -285,9 +291,9 @@ public:
|
||||||
return t->h;
|
return t->h;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Add(T *t) {
|
void ReserveMore(int howMuch) {
|
||||||
if(n >= elemsAllocated) {
|
if(n + howMuch > elemsAllocated) {
|
||||||
elemsAllocated = (elemsAllocated + 32)*2;
|
elemsAllocated = n + howMuch;
|
||||||
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(elem[0]));
|
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(elem[0]));
|
||||||
for(int i = 0; i < n; i++) {
|
for(int i = 0; i < n; i++) {
|
||||||
new(&newElem[i]) T(std::move(elem[i]));
|
new(&newElem[i]) T(std::move(elem[i]));
|
||||||
|
@ -296,7 +302,13 @@ public:
|
||||||
MemFree(elem);
|
MemFree(elem);
|
||||||
elem = newElem;
|
elem = newElem;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Add(T *t) {
|
||||||
|
if(n >= elemsAllocated) {
|
||||||
|
ReserveMore((elemsAllocated + 32)*2 - n);
|
||||||
|
}
|
||||||
|
|
||||||
int first = 0, last = n;
|
int first = 0, last = n;
|
||||||
// We know that we must insert within the closed interval [first,last]
|
// We know that we must insert within the closed interval [first,last]
|
||||||
while(first != last) {
|
while(first != last) {
|
||||||
|
|
|
@ -217,7 +217,10 @@ void SolveSpaceUI::GenerateAll(Generate type, bool andFindFree, bool genForBBox)
|
||||||
// Don't lose our numerical guesses when we regenerate.
|
// Don't lose our numerical guesses when we regenerate.
|
||||||
IdList<Param,hParam> prev = {};
|
IdList<Param,hParam> prev = {};
|
||||||
SK.param.MoveSelfInto(&prev);
|
SK.param.MoveSelfInto(&prev);
|
||||||
|
SK.param.ReserveMore(prev.n);
|
||||||
|
int oldEntityCount = SK.entity.n;
|
||||||
SK.entity.Clear();
|
SK.entity.Clear();
|
||||||
|
SK.entity.ReserveMore(oldEntityCount);
|
||||||
|
|
||||||
for(i = 0; i < SK.groupOrder.n; i++) {
|
for(i = 0; i < SK.groupOrder.n; i++) {
|
||||||
Group *g = SK.GetGroup(SK.groupOrder.elem[i]);
|
Group *g = SK.GetGroup(SK.groupOrder.elem[i]);
|
||||||
|
|
|
@ -664,6 +664,7 @@ void SShell::MakeFromAssemblyOf(SShell *a, SShell *b) {
|
||||||
|
|
||||||
// First, copy over all the curves. Note which shell (a or b) each curve
|
// First, copy over all the curves. Note which shell (a or b) each curve
|
||||||
// came from, but assign it a new ID.
|
// came from, but assign it a new ID.
|
||||||
|
curve.ReserveMore(a->curve.n + b->curve.n);
|
||||||
SCurve *c, cn;
|
SCurve *c, cn;
|
||||||
for(i = 0; i < 2; i++) {
|
for(i = 0; i < 2; i++) {
|
||||||
ab = (i == 0) ? a : b;
|
ab = (i == 0) ? a : b;
|
||||||
|
@ -677,6 +678,7 @@ void SShell::MakeFromAssemblyOf(SShell *a, SShell *b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Likewise copy over all the surfaces.
|
// Likewise copy over all the surfaces.
|
||||||
|
surface.ReserveMore(a->surface.n + b->surface.n);
|
||||||
SSurface *s, sn;
|
SSurface *s, sn;
|
||||||
for(i = 0; i < 2; i++) {
|
for(i = 0; i < 2; i++) {
|
||||||
ab = (i == 0) ? a : b;
|
ab = (i == 0) ? a : b;
|
||||||
|
|
|
@ -741,6 +741,7 @@ SCurve SCurve::FromTransformationOf(SCurve *a, Vector t,
|
||||||
ret.surfB = a->surfB;
|
ret.surfB = a->surfB;
|
||||||
|
|
||||||
SCurvePt *p;
|
SCurvePt *p;
|
||||||
|
ret.pts.ReserveMore(a->pts.n);
|
||||||
for(p = a->pts.First(); p; p = a->pts.NextAfter(p)) {
|
for(p = a->pts.First(); p; p = a->pts.NextAfter(p)) {
|
||||||
SCurvePt pp = *p;
|
SCurvePt pp = *p;
|
||||||
if(needScale) {
|
if(needScale) {
|
||||||
|
|
|
@ -165,6 +165,7 @@ SSurface SSurface::FromTransformationOf(SSurface *a, Vector t, Quaternion q, dou
|
||||||
|
|
||||||
if(includingTrims) {
|
if(includingTrims) {
|
||||||
STrimBy *stb;
|
STrimBy *stb;
|
||||||
|
ret.trim.ReserveMore(a->trim.n);
|
||||||
for(stb = a->trim.First(); stb; stb = a->trim.NextAfter(stb)) {
|
for(stb = a->trim.First(); stb; stb = a->trim.NextAfter(stb)) {
|
||||||
STrimBy n = *stb;
|
STrimBy n = *stb;
|
||||||
if(needScale) {
|
if(needScale) {
|
||||||
|
@ -834,7 +835,7 @@ void SShell::MakeFromTransformationOf(SShell *a,
|
||||||
Vector t, Quaternion q, double scale)
|
Vector t, Quaternion q, double scale)
|
||||||
{
|
{
|
||||||
booleanFailed = false;
|
booleanFailed = false;
|
||||||
|
surface.ReserveMore(a->surface.n);
|
||||||
SSurface *s;
|
SSurface *s;
|
||||||
for(s = a->surface.First(); s; s = a->surface.NextAfter(s)) {
|
for(s = a->surface.First(); s; s = a->surface.NextAfter(s)) {
|
||||||
SSurface n;
|
SSurface n;
|
||||||
|
@ -842,6 +843,7 @@ void SShell::MakeFromTransformationOf(SShell *a,
|
||||||
surface.Add(&n); // keeping the old ID
|
surface.Add(&n); // keeping the old ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
curve.ReserveMore(a->curve.n);
|
||||||
SCurve *c;
|
SCurve *c;
|
||||||
for(c = a->curve.First(); c; c = a->curve.NextAfter(c)) {
|
for(c = a->curve.First(); c; c = a->curve.NextAfter(c)) {
|
||||||
SCurve n;
|
SCurve n;
|
||||||
|
|
|
@ -47,6 +47,7 @@ void SolveSpaceUI::PushFromCurrentOnto(UndoStack *uk) {
|
||||||
|
|
||||||
UndoState *ut = &(uk->d[uk->write]);
|
UndoState *ut = &(uk->d[uk->write]);
|
||||||
*ut = {};
|
*ut = {};
|
||||||
|
ut->group.ReserveMore(SK.group.n);
|
||||||
for(i = 0; i < SK.group.n; i++) {
|
for(i = 0; i < SK.group.n; i++) {
|
||||||
Group *src = &(SK.group.elem[i]);
|
Group *src = &(SK.group.elem[i]);
|
||||||
Group dest = *src;
|
Group dest = *src;
|
||||||
|
@ -76,17 +77,21 @@ void SolveSpaceUI::PushFromCurrentOnto(UndoStack *uk) {
|
||||||
for(i = 0; i < SK.groupOrder.n; i++) {
|
for(i = 0; i < SK.groupOrder.n; i++) {
|
||||||
ut->groupOrder.Add(&(SK.groupOrder.elem[i]));
|
ut->groupOrder.Add(&(SK.groupOrder.elem[i]));
|
||||||
}
|
}
|
||||||
|
ut->request.ReserveMore(SK.request.n);
|
||||||
for(i = 0; i < SK.request.n; i++) {
|
for(i = 0; i < SK.request.n; i++) {
|
||||||
ut->request.Add(&(SK.request.elem[i]));
|
ut->request.Add(&(SK.request.elem[i]));
|
||||||
}
|
}
|
||||||
|
ut->constraint.ReserveMore(SK.constraint.n);
|
||||||
for(i = 0; i < SK.constraint.n; i++) {
|
for(i = 0; i < SK.constraint.n; i++) {
|
||||||
Constraint *src = &(SK.constraint.elem[i]);
|
Constraint *src = &(SK.constraint.elem[i]);
|
||||||
Constraint dest = *src;
|
Constraint dest = *src;
|
||||||
ut->constraint.Add(&dest);
|
ut->constraint.Add(&dest);
|
||||||
}
|
}
|
||||||
|
ut->param.ReserveMore(SK.param.n);
|
||||||
for(i = 0; i < SK.param.n; i++) {
|
for(i = 0; i < SK.param.n; i++) {
|
||||||
ut->param.Add(&(SK.param.elem[i]));
|
ut->param.Add(&(SK.param.elem[i]));
|
||||||
}
|
}
|
||||||
|
ut->style.ReserveMore(SK.style.n);
|
||||||
for(i = 0; i < SK.style.n; i++) {
|
for(i = 0; i < SK.style.n; i++) {
|
||||||
ut->style.Add(&(SK.style.elem[i]));
|
ut->style.Add(&(SK.style.elem[i]));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue