Skip to content

Commit 1a403eb

Browse files
tanriolBenBE
authored andcommitted
ProcessList_buildTree: lookup parent via hashtable
While this change does not significantly affect performance, it removes the internal requirement to have the process list sorted by PID.
1 parent a3a7958 commit 1a403eb

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

ProcessList.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -433,36 +433,24 @@ static void ProcessList_buildTree(ProcessList* this) {
433433
}
434434

435435
pid_t ppid = Process_getParentPid(process);
436-
437-
// Bisect the process vector to find parent
438-
int l = 0;
439-
int r = size;
436+
bool isRoot = false;
440437

441438
// If PID corresponds with PPID (e.g. "kernel_task" (PID:0, PPID:0)
442-
// on Mac OS X 10.11.6) cancel bisecting and regard this process as
443-
// root.
439+
// on Mac OS X 10.11.6) regard this process as root.
444440
if (process->pid == ppid)
445-
r = 0;
441+
isRoot = true;
446442

447443
// On Linux both the init process (pid 1) and the root UMH kernel thread (pid 2)
448444
// use a ppid of 0. As that PID can't exist, we can skip searching for it.
449445
if (!ppid)
450-
r = 0;
451-
452-
while (l < r) {
453-
int c = (l + r) / 2;
454-
pid_t pid = ((Process*)Vector_get(this->processes, c))->pid;
455-
if (ppid == pid) {
456-
break;
457-
} else if (ppid < pid) {
458-
r = c;
459-
} else {
460-
l = c + 1;
461-
}
462-
}
446+
isRoot = true;
447+
448+
// Lookup the parent via the processTable hashtable not modified in buildTree
449+
if (ProcessList_findProcess(this, ppid) == NULL)
450+
isRoot = true;
463451

464452
// If parent not found, then construct the tree with this node as root
465-
if (l >= r) {
453+
if (isRoot) {
466454
process = (Process*)Vector_take(this->processes, i);
467455
process->indent = 0;
468456
process->tree_depth = 0;

0 commit comments

Comments
 (0)