Commit 5d48ab8c authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Performance improvement hackathon: improve process comparison routines,

disable useless code in release builds such as runtime type-checking on
dynamic data structures and process fields that are not being computed,
faster(?) method for verifying the process owner (still need to ensure
correctness), don't destroy and create process objects for hidden kernel
threads over and over. Phew. I shouldn't be doing all this today, but I
could not resist.
parent 4c41e78b
...@@ -18,6 +18,7 @@ in the source distribution for its full text. ...@@ -18,6 +18,7 @@ in the source distribution for its full text.
#include "Vector.h" #include "Vector.h"
#include "UsersTable.h" #include "UsersTable.h"
#include "Hashtable.h" #include "Hashtable.h"
#include "String.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -50,11 +51,15 @@ in the source distribution for its full text. ...@@ -50,11 +51,15 @@ in the source distribution for its full text.
#endif #endif
#ifndef MAX_READ #ifndef MAX_READ
#define MAX_READ 8192 #define MAX_READ 2048
#endif #endif
#ifdef DEBUG
typedef int(*vxscanf)(void*, const char*, va_list);
#endif
typedef struct ProcessList_ { typedef struct ProcessList_ {
Vector* processes; Vector* processes;
Vector* processes2; Vector* processes2;
......
...@@ -47,8 +47,8 @@ ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation ori ...@@ -47,8 +47,8 @@ ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation ori
this->y2 = y2; this->y2 = y2;
this->fuBar = NULL; this->fuBar = NULL;
this->orientation = orientation; this->orientation = orientation;
this->items = Vector_new(PANEL_CLASS, owner, DEFAULT_SIZE); this->items = Vector_new(PANEL_CLASS, owner, DEFAULT_SIZE, NULL);
this->fuBars = Vector_new(FUNCTIONBAR_CLASS, true, DEFAULT_SIZE); this->fuBars = Vector_new(FUNCTIONBAR_CLASS, true, DEFAULT_SIZE, NULL);
this->itemCount = 0; this->itemCount = 0;
this->owner = owner; this->owner = owner;
return this; return this;
...@@ -130,7 +130,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { ...@@ -130,7 +130,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
if (this->fuBar) if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL); FunctionBar_draw(this->fuBar, NULL);
int ch; int ch = 0;
while (!quit) { while (!quit) {
int items = this->itemCount; int items = this->itemCount;
for (int i = 0; i < items; i++) { for (int i = 0; i < items; i++) {
......
...@@ -25,11 +25,15 @@ typedef struct Signal_ { ...@@ -25,11 +25,15 @@ typedef struct Signal_ {
}*/ }*/
#ifdef DEBUG
char* SIGNAL_CLASS = "Signal"; char* SIGNAL_CLASS = "Signal";
#else
#define SIGNAL_CLASS NULL
#endif
Signal* Signal_new(char* name, int number) { Signal* Signal_new(char* name, int number) {
Signal* this = malloc(sizeof(Signal)); Signal* this = malloc(sizeof(Signal));
((Object*)this)->class = SIGNAL_CLASS; Object_setClass(this, SIGNAL_CLASS);
((Object*)this)->display = Signal_display; ((Object*)this)->display = Signal_display;
((Object*)this)->delete = Signal_delete; ((Object*)this)->delete = Signal_delete;
this->name = name; this->name = name;
......
...@@ -26,7 +26,11 @@ typedef struct Signal_ { ...@@ -26,7 +26,11 @@ typedef struct Signal_ {
} Signal; } Signal;
#ifdef DEBUG
extern char* SIGNAL_CLASS; extern char* SIGNAL_CLASS;
#else
#define SIGNAL_CLASS NULL
#endif
Signal* Signal_new(char* name, int number); Signal* Signal_new(char* name, int number);
......
...@@ -14,6 +14,10 @@ in the source distribution for its full text. ...@@ -14,6 +14,10 @@ in the source distribution for its full text.
#include "debug.h" #include "debug.h"
/*{
#define String_startsWith(s, match) (strstr((s), (match)) == (s))
}*/
inline void String_delete(char* s) { inline void String_delete(char* s) {
free(s); free(s);
} }
...@@ -102,10 +106,6 @@ inline int String_eq(char* s1, char* s2) { ...@@ -102,10 +106,6 @@ inline int String_eq(char* s1, char* s2) {
return (strcmp(s1, s2) == 0); return (strcmp(s1, s2) == 0);
} }
inline int String_startsWith(char* s, char* match) {
return (strstr(s, match) == s);
}
char** String_split(char* s, char sep) { char** String_split(char* s, char sep) {
const int rate = 10; const int rate = 10;
char** out = (char**) malloc(sizeof(char*) * rate); char** out = (char**) malloc(sizeof(char*) * rate);
......
...@@ -17,6 +17,8 @@ in the source distribution for its full text. ...@@ -17,6 +17,8 @@ in the source distribution for its full text.
#include "debug.h" #include "debug.h"
#define String_startsWith(s, match) (strstr((s), (match)) == (s))
inline void String_delete(char* s); inline void String_delete(char* s);
inline char* String_copy(char* orig); inline char* String_copy(char* orig);
...@@ -39,8 +41,6 @@ void String_printPointer(void* p); ...@@ -39,8 +41,6 @@ void String_printPointer(void* p);
inline int String_eq(char* s1, char* s2); inline int String_eq(char* s1, char* s2);
inline int String_startsWith(char* s, char* match);
char** String_split(char* s, char sep); char** String_split(char* s, char sep);
void String_freeArray(char** s); void String_freeArray(char** s);
......
...@@ -41,7 +41,7 @@ static int tbEvents[3] = {KEY_F(4), KEY_F(5), 27}; ...@@ -41,7 +41,7 @@ static int tbEvents[3] = {KEY_F(4), KEY_F(5), 27};
TraceScreen* TraceScreen_new(Process* process) { TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen)); TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
this->process = process; this->process = process;
this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true); this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true, ListItem_compare);
this->bar = FunctionBar_new(3, tbFunctions, tbKeys, tbEvents); this->bar = FunctionBar_new(3, tbFunctions, tbKeys, tbEvents);
this->tracing = true; this->tracing = true;
return this; return this;
......
...@@ -24,6 +24,7 @@ typedef void(*Vector_procedure)(void*); ...@@ -24,6 +24,7 @@ typedef void(*Vector_procedure)(void*);
typedef struct Vector_ { typedef struct Vector_ {
Object **array; Object **array;
Object_Compare compare;
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
...@@ -33,7 +34,7 @@ typedef struct Vector_ { ...@@ -33,7 +34,7 @@ typedef struct Vector_ {
}*/ }*/
Vector* Vector_new(char* vectorType_, bool owner, int size) { Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compare) {
Vector* this; Vector* this;
if (size == DEFAULT_SIZE) if (size == DEFAULT_SIZE)
...@@ -45,6 +46,7 @@ Vector* Vector_new(char* vectorType_, bool owner, int size) { ...@@ -45,6 +46,7 @@ Vector* Vector_new(char* vectorType_, bool owner, int size) {
this->items = 0; this->items = 0;
this->vectorType = vectorType_; this->vectorType = vectorType_;
this->owner = owner; this->owner = owner;
this->compare = compare;
return this; return this;
} }
...@@ -58,6 +60,8 @@ void Vector_delete(Vector* this) { ...@@ -58,6 +60,8 @@ void Vector_delete(Vector* this) {
free(this); free(this);
} }
#ifdef DEBUG
static inline bool Vector_isConsistent(Vector* this) { static inline bool Vector_isConsistent(Vector* this) {
if (this->owner) { if (this->owner) {
for (int i = 0; i < this->items; i++) for (int i = 0; i < this->items; i++)
...@@ -69,6 +73,8 @@ static inline bool Vector_isConsistent(Vector* this) { ...@@ -69,6 +73,8 @@ static inline bool Vector_isConsistent(Vector* this) {
} }
} }
#endif
void Vector_prune(Vector* this) { void Vector_prune(Vector* this) {
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
int i; int i;
...@@ -83,27 +89,18 @@ void Vector_prune(Vector* this) { ...@@ -83,27 +89,18 @@ void Vector_prune(Vector* this) {
} }
void Vector_sort(Vector* this) { void Vector_sort(Vector* this) {
assert(this->compare);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
int i, j; Object_Compare compare = this->compare;
for (i = 1; i < this->items; i++) { /* Insertion sort works best on mostly-sorted arrays. */
for (int i = 1; i < this->items; i++) {
int j;
void* t = this->array[i]; void* t = this->array[i];
for (j = i-1; j >= 0 && this->array[j]->compare(this->array[j], t) < 0; j--) for (j = i-1; j >= 0 && compare(this->array[j], t) > 0; j--)
this->array[j+1] = this->array[j]; this->array[j+1] = this->array[j];
this->array[j+1] = t; this->array[j+1] = t;
} }
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
/*
for (int i = 0; i < this->items; i++) {
for (int j = i+1; j < this->items; j++) {
if (this->array[j]->compare(this->array[j], t) < 0) {
void* tmp = this->array[i];
this->array[i] = this->array[j];
this->array[j] = tmp;
}
}
}
*/
} }
static void Vector_checkArraySize(Vector* this) { static void Vector_checkArraySize(Vector* this) {
...@@ -230,16 +227,14 @@ void Vector_add(Vector* this, void* data_) { ...@@ -230,16 +227,14 @@ void Vector_add(Vector* this, void* data_) {
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
} }
inline int Vector_indexOf(Vector* this, void* search_) { inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare) {
assert(((Object*)search_)->class == this->vectorType); assert(((Object*)search_)->class == this->vectorType);
assert(this->compare);
Object* search = search_; Object* search = search_;
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
for (int i = 0; i < this->items; i++) {
int i;
for (i = 0; i < this->items; i++) {
Object* o = (Object*)this->array[i]; Object* o = (Object*)this->array[i];
if (o && o->compare(o, search) == 0) if (o && compare(search, o) == 0)
return i; return i;
} }
return -1; return -1;
......
...@@ -26,6 +26,7 @@ typedef void(*Vector_procedure)(void*); ...@@ -26,6 +26,7 @@ typedef void(*Vector_procedure)(void*);
typedef struct Vector_ { typedef struct Vector_ {
Object **array; Object **array;
Object_Compare compare;
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
...@@ -34,10 +35,14 @@ typedef struct Vector_ { ...@@ -34,10 +35,14 @@ typedef struct Vector_ {
} Vector; } Vector;
Vector* Vector_new(char* vectorType_, bool owner, int size); Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compare);
void Vector_delete(Vector* this); void Vector_delete(Vector* this);
#ifdef DEBUG
#endif
void Vector_prune(Vector* this); void Vector_prune(Vector* this);
void Vector_sort(Vector* this); void Vector_sort(Vector* this);
...@@ -62,7 +67,7 @@ void Vector_merge(Vector* this, Vector* v2); ...@@ -62,7 +67,7 @@ void Vector_merge(Vector* this, Vector* v2);
void Vector_add(Vector* this, void* data_); void Vector_add(Vector* this, void* data_);
inline int Vector_indexOf(Vector* this, void* search_); inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare);
void Vector_foreach(Vector* this, Vector_procedure f); void Vector_foreach(Vector* this, Vector_procedure f);
......
...@@ -245,7 +245,7 @@ int main(int argc, char** argv) { ...@@ -245,7 +245,7 @@ int main(int argc, char** argv) {
CRT_init(settings->delay, settings->colorScheme); CRT_init(settings->delay, settings->colorScheme);
panel = Panel_new(0, headerHeight, COLS, LINES - headerHeight - 2, PROCESS_CLASS, false); panel = Panel_new(0, headerHeight, COLS, LINES - headerHeight - 2, PROCESS_CLASS, false, NULL);
Panel_setRichHeader(panel, ProcessList_printHeader(pl)); Panel_setRichHeader(panel, ProcessList_printHeader(pl));
char* searchFunctions[3] = {"Next ", "Exit ", " Search: "}; char* searchFunctions[3] = {"Next ", "Exit ", " Search: "};
...@@ -506,7 +506,7 @@ int main(int argc, char** argv) { ...@@ -506,7 +506,7 @@ int main(int argc, char** argv) {
} }
case 'u': case 'u':
{ {
Panel* usersPanel = Panel_new(0, 0, 0, 0, LISTITEM_CLASS, true); Panel* usersPanel = Panel_new(0, 0, 0, 0, LISTITEM_CLASS, true, ListItem_compare);
Panel_setHeader(usersPanel, "Show processes of:"); Panel_setHeader(usersPanel, "Show processes of:");
UsersTable_foreach(ut, addUserToList, usersPanel); UsersTable_foreach(ut, addUserToList, usersPanel);
Vector_sort(usersPanel->items); Vector_sort(usersPanel->items);
...@@ -569,7 +569,7 @@ int main(int argc, char** argv) { ...@@ -569,7 +569,7 @@ int main(int argc, char** argv) {
case '.': case '.':
case KEY_F(6): case KEY_F(6):
{ {
Panel* sortPanel = Panel_new(0,0,0,0,LISTITEM_CLASS,true); Panel* sortPanel = Panel_new(0, 0, 0, 0, LISTITEM_CLASS, true, ListItem_compare);
Panel_setHeader(sortPanel, "Sort by"); Panel_setHeader(sortPanel, "Sort by");
char* fuFunctions[2] = {"Sort ", "Cancel "}; char* fuFunctions[2] = {"Sort ", "Cancel "};
ProcessField* fields = pl->fields; ProcessField* fields = pl->fields;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment