Skip to content

Commit 08166b2

Browse files
charlieviethBenBE
authored andcommitted
ProcessList: fix quadratic process removal when scanning
This commit changes ProcessList_scan to lazily remove Processes by index, which is known, instead of performing a brute-force search by pid and immediately reclaiming the lost vector space via compaction. Searching by pid is potentially quadratic in ProcessList_scan because the process we are searching for is always at the back of the vector (the scan starts from the back of the vector). Additionally, removal via Vector_remove immediately reclaims space (by sliding elements down). With these changes process removal in ProcessList_scan is now linear. Changes: * ProcessList: add new ProcessList_removeIndex function to remove by index * Vector: add Vector_softRemove and Vector_compact functions to support lazy removal/deletion of entries Vector_softRemove Vector_compact * Vector: replace Vector_count with Vector_countEquals since it only used for consistency assertions.
1 parent c7413fd commit 08166b2

File tree

5 files changed

+127
-35
lines changed

5 files changed

+127
-35
lines changed

Process.c

-7
Original file line numberDiff line numberDiff line change
@@ -1095,13 +1095,6 @@ bool Process_sendSignal(Process* this, Arg sgn) {
10951095
return kill(this->pid, sgn.i) == 0;
10961096
}
10971097

1098-
int Process_pidCompare(const void* v1, const void* v2) {
1099-
const Process* p1 = (const Process*)v1;
1100-
const Process* p2 = (const Process*)v2;
1101-
1102-
return SPACESHIP_NUMBER(p1->pid, p2->pid);
1103-
}
1104-
11051098
int Process_compare(const void* v1, const void* v2) {
11061099
const Process* p1 = (const Process*)v1;
11071100
const Process* p2 = (const Process*)v2;

Process.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,11 @@ bool Process_changePriorityBy(Process* this, Arg delta);
394394

395395
bool Process_sendSignal(Process* this, Arg sgn);
396396

397-
int Process_pidCompare(const void* v1, const void* v2);
397+
static inline int Process_pidEqualCompare(const void* v1, const void* v2) {
398+
const pid_t p1 = ((const Process*)v1)->pid;
399+
const pid_t p2 = ((const Process*)v2)->pid;
400+
return p1 != p2; /* return zero when equal */
401+
}
398402

399403
int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key);
400404

ProcessList.c

+29-19
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void ProcessList_printHeader(const ProcessList* this, RichString* header) {
158158
}
159159

160160
void ProcessList_add(ProcessList* this, Process* p) {
161-
assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1);
161+
assert(Vector_indexOf(this->processes, p, Process_pidEqualCompare) == -1);
162162
assert(Hashtable_get(this->processTable, p->pid) == NULL);
163163
p->processList = this;
164164

@@ -168,33 +168,40 @@ void ProcessList_add(ProcessList* this, Process* p) {
168168
Vector_add(this->processes, p);
169169
Hashtable_put(this->processTable, p->pid, p);
170170

171-
assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1);
171+
assert(Vector_indexOf(this->processes, p, Process_pidEqualCompare) != -1);
172172
assert(Hashtable_get(this->processTable, p->pid) != NULL);
173-
assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
173+
assert(Vector_countEquals(this->processes, Hashtable_count(this->processTable)));
174174
}
175175

176-
void ProcessList_remove(ProcessList* this, const Process* p) {
177-
assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1);
178-
assert(Hashtable_get(this->processTable, p->pid) != NULL);
179-
180-
const Process* pp = Hashtable_remove(this->processTable, p->pid);
181-
assert(pp == p); (void)pp;
182-
176+
// ProcessList_removeIndex removes Process p from the list's map and soft deletes
177+
// it from its vector. Vector_compact *must* be called once the caller is done
178+
// removing items.
179+
static void ProcessList_removeIndex(ProcessList* this, const Process* p, int idx) {
183180
pid_t pid = p->pid;
184-
int idx = Vector_indexOf(this->processes, p, Process_pidCompare);
185-
assert(idx != -1);
186181

187-
if (idx >= 0) {
188-
Vector_remove(this->processes, idx);
189-
}
182+
assert(p == (Process*)Vector_get(this->processes, idx));
183+
assert(Hashtable_get(this->processTable, pid) != NULL);
184+
185+
Hashtable_remove(this->processTable, pid);
186+
Vector_softRemove(this->processes, idx);
190187

191188
if (this->following != -1 && this->following == pid) {
192189
this->following = -1;
193190
Panel_setSelectionColor(this->panel, PANEL_SELECTION_FOCUS);
194191
}
195192

196193
assert(Hashtable_get(this->processTable, pid) == NULL);
197-
assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
194+
assert(Vector_countEquals(this->processes, Hashtable_count(this->processTable)));
195+
}
196+
197+
void ProcessList_remove(ProcessList* this, const Process* p) {
198+
int idx = Vector_indexOf(this->processes, p, Process_pidEqualCompare);
199+
assert(0 <= idx && idx < Vector_size(this->processes));
200+
if (idx >= 0) {
201+
ProcessList_removeIndex(this, p, idx);
202+
// This function is public so must not require callers to compact the Vector
203+
Vector_compact(this->processes);
204+
}
198205
}
199206

200207
static void ProcessList_buildTreeBranch(ProcessList* this, pid_t pid, int level, int indent, bool show) {
@@ -429,7 +436,7 @@ Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting,
429436
Process* proc = (Process*) Hashtable_get(this->processTable, pid);
430437
*preExisting = proc != NULL;
431438
if (proc) {
432-
assert(Vector_indexOf(this->processes, proc, Process_pidCompare) != -1);
439+
assert(Vector_indexOf(this->processes, proc, Process_pidEqualCompare) != -1);
433440
assert(proc->pid == pid);
434441
} else {
435442
proc = constructor(this->settings);
@@ -484,7 +491,7 @@ void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) {
484491
if (p->tombStampMs > 0) {
485492
// remove tombed process
486493
if (this->monotonicMs >= p->tombStampMs) {
487-
ProcessList_remove(this, p);
494+
ProcessList_removeIndex(this, p, i);
488495
}
489496
} else if (p->updated == false) {
490497
// process no longer exists
@@ -493,11 +500,14 @@ void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) {
493500
p->tombStampMs = this->monotonicMs + 1000 * this->settings->highlightDelaySecs;
494501
} else {
495502
// immediately remove
496-
ProcessList_remove(this, p);
503+
ProcessList_removeIndex(this, p, i);
497504
}
498505
}
499506
}
500507

508+
// Compact the processes vector in case of any deletions
509+
Vector_compact(this->processes);
510+
501511
// Set UID column width based on max UID.
502512
Process_setUidColumnWidth(maxUid);
503513
}

Vector.c

+74-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
2929
this->items = 0;
3030
this->type = type;
3131
this->owner = owner;
32+
this->dirty_index = -1;
33+
this->dirty_count = 0;
3234
return this;
3335
}
3436

@@ -44,10 +46,21 @@ void Vector_delete(Vector* this) {
4446
free(this);
4547
}
4648

49+
static inline bool Vector_isDirty(const Vector* this) {
50+
if (this->dirty_count > 0) {
51+
assert(0 <= this->dirty_index && this->dirty_index < this->items);
52+
assert(this->dirty_count <= this->items);
53+
return true;
54+
}
55+
assert(this->dirty_index == -1);
56+
return false;
57+
}
58+
4759
#ifndef NDEBUG
4860

4961
static bool Vector_isConsistent(const Vector* this) {
5062
assert(this->items <= this->arraySize);
63+
assert(!Vector_isDirty(this));
5164

5265
if (this->owner) {
5366
for (int i = 0; i < this->items; i++) {
@@ -60,15 +73,14 @@ static bool Vector_isConsistent(const Vector* this) {
6073
return true;
6174
}
6275

63-
unsigned int Vector_count(const Vector* this) {
64-
unsigned int items = 0;
76+
bool Vector_countEquals(const Vector* this, unsigned int expectedCount) {
77+
unsigned int n = 0;
6578
for (int i = 0; i < this->items; i++) {
6679
if (this->array[i]) {
67-
items++;
80+
n++;
6881
}
6982
}
70-
assert(items == (unsigned int)this->items);
71-
return items;
83+
return n == expectedCount;
7284
}
7385

7486
Object* Vector_get(const Vector* this, int idx) {
@@ -88,13 +100,16 @@ int Vector_size(const Vector* this) {
88100
void Vector_prune(Vector* this) {
89101
assert(Vector_isConsistent(this));
90102
if (this->owner) {
91-
for (int i = 0; i < this->items; i++)
103+
for (int i = 0; i < this->items; i++) {
92104
if (this->array[i]) {
93105
Object_delete(this->array[i]);
94-
//this->array[i] = NULL;
106+
this->array[i] = NULL;
95107
}
108+
}
96109
}
97110
this->items = 0;
111+
this->dirty_index = -1;
112+
this->dirty_count = 0;
98113
}
99114

100115
//static int comparisons = 0;
@@ -242,6 +257,58 @@ Object* Vector_remove(Vector* this, int idx) {
242257
}
243258
}
244259

260+
Object* Vector_softRemove(Vector* this, int idx) {
261+
assert(idx >= 0 && idx < this->items);
262+
263+
Object* removed = this->array[idx];
264+
assert(removed);
265+
if (removed) {
266+
this->array[idx] = NULL;
267+
268+
this->dirty_count++;
269+
if (this->dirty_index < 0 || idx < this->dirty_index) {
270+
this->dirty_index = idx;
271+
}
272+
273+
if (this->owner) {
274+
Object_delete(removed);
275+
return NULL;
276+
}
277+
}
278+
279+
return removed;
280+
}
281+
282+
void Vector_compact(Vector* this) {
283+
if (!Vector_isDirty(this)) {
284+
return;
285+
}
286+
287+
const int size = this->items;
288+
assert(0 <= this->dirty_index && this->dirty_index < size);
289+
assert(this->array[this->dirty_index] == NULL);
290+
291+
int idx = this->dirty_index;
292+
293+
/* one deletion: use memmove, which should be faster */
294+
if (this->dirty_count == 1) {
295+
memmove(&this->array[idx], &this->array[idx + 1], (this->items - idx) * sizeof(this->array[0]));
296+
} else {
297+
/* multiple deletions */
298+
for (int i = idx + 1; i < size; i++) {
299+
if (this->array[i]) {
300+
this->array[idx++] = this->array[i];
301+
}
302+
}
303+
}
304+
305+
this->items -= this->dirty_count;
306+
this->dirty_index = -1;
307+
this->dirty_count = 0;
308+
309+
assert(Vector_isConsistent(this));
310+
}
311+
245312
void Vector_moveUp(Vector* this, int idx) {
246313
assert(idx >= 0 && idx < this->items);
247314
assert(Vector_isConsistent(this));

Vector.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ typedef struct Vector_ {
2222
int arraySize;
2323
int growthRate;
2424
int items;
25+
/* lowest index of a pending soft remove/delete operation,
26+
used to speed up compaction */
27+
int dirty_index;
28+
/* count of soft deletes, required for Vector_count to work in debug mode */
29+
int dirty_count;
2530
bool owner;
2631
} Vector;
2732

@@ -44,6 +49,15 @@ Object* Vector_take(Vector* this, int idx);
4449

4550
Object* Vector_remove(Vector* this, int idx);
4651

52+
/* Vector_softRemove marks the item at index idx for deletion without
53+
reclaiming any space. If owned, the item is immediately freed.
54+
55+
Vector_compact must be called to reclaim space.*/
56+
Object* Vector_softRemove(Vector* this, int idx);
57+
58+
/* Vector_compact reclaims space free'd up by Vector_softRemove, if any. */
59+
void Vector_compact(Vector* this);
60+
4761
void Vector_moveUp(Vector* this, int idx);
4862

4963
void Vector_moveDown(Vector* this, int idx);
@@ -54,7 +68,11 @@ void Vector_set(Vector* this, int idx, void* data_);
5468

5569
Object* Vector_get(const Vector* this, int idx);
5670
int Vector_size(const Vector* this);
57-
unsigned int Vector_count(const Vector* this);
71+
72+
/* Vector_countEquals returns true if the number of non-NULL items
73+
in the Vector is equal to expectedCount. This is only for debugging
74+
and consistency checks. */
75+
bool Vector_countEquals(const Vector* this, unsigned int expectedCount);
5876

5977
#else /* NDEBUG */
6078

0 commit comments

Comments
 (0)