Skip to content

Commit daca893

Browse files
perf: remove entire exeTimes vector to save time
1 parent 843a74c commit daca893

1 file changed

Lines changed: 16 additions & 25 deletions

File tree

main.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,27 +1533,24 @@ UPDATE: This is done now!!
15331533
DWORD targetpid = pid; // the function already passes pid into us, but
15341534
// just to be safe that pid doesn't get overwritten in the loop below
15351535
std::string exeName = "Unknown/Dead Process";
1536-
std::vector<std::string> exeNames;
1537-
std::vector<ULONGLONG> exeTimes; // sorry for the crap code but idk how to make multidimensional arrays yet 😭😭😭
1536+
std::vector<std::string> exeNames; // sorry for the crap code but idk how to make multidimensional arrays yet 😭😭😭
15381537
std::vector<DWORD> pidNames; // hopefully the compiler can fix it
15391538
std::vector<DWORD> parentPids;
1539+
ULONGLONG creationTime = GetProcessCreationTime(pid);
15401540
bool found = false;
15411541
while (pid != 0 && pid != 4) {
15421542
found = false;
15431543
auto it = pidMap.find(pid);
15441544
if (it != pidMap.end()) {
15451545
const PROCESSENTRY32& entry = it->second;
15461546
// Without comments, this literally looks like alien gibberish so lemme explain
1547-
1548-
ULONGLONG creationTime = GetProcessCreationTime(pid); // this stores the creation time of the CURRENT pid (not parent)
1549-
exeTimes.emplace_back(creationTime); // immediately stores the above to the list
1547+
15501548
exeName = WideToString(entry.szExeFile); //this stores the NAME of the current pid, converted to something that the terminal won't choke and die on
15511549
exeNames.emplace_back(exeName); // this adds the above to the name list
15521550
pidNames.emplace_back(pid); // this adds the current pid (no need to store in var as already passed into if)
15531551

15541552
parentPid = entry.th32ParentProcessID; // this gets the pid of the PARENT pid (if there hopefully is one)
15551553
parentPids.emplace_back(entry.th32ParentProcessID); // adds above to list
1556-
ULONGLONG parentTime = GetProcessCreationTime(parentPid); // this gets the creation time of that one
15571554

15581555
if (parentPid == 0 || parentPid == 4 || parentTime == 0 || parentTime >= creationTime) {
15591556
// we can't be sure if the parent actually exists and windows isn't lying to us,
@@ -1573,6 +1570,12 @@ UPDATE: This is done now!!
15731570
// tells us that our target pid is it's parent. This time, we don't have to worry about
15741571
// Checking if the parent is alive, because, well, since the target IS the parent,
15751572
// it must be alive.
1573+
// now we need to reverse all the vector lists we made so
1574+
// that the ancestry tree is correctly diisplayed from root to children like witr
1575+
// in c++20 there is a new way to reverse called ranges or smth but i won't use that
1576+
std::reverse(exeNames.begin(), exeNames.end());
1577+
std::reverse(pidNames.begin(), pidNames.end());
1578+
std::reverse(parentPids.begin(), parentPids.end());
15761579
int children = 0; // i wonder what would happen if you could set an emoji as var name
15771580
for (const auto& pair : pidMap) {
15781581
const PROCESSENTRY32& entry = pair.second;
@@ -1585,14 +1588,9 @@ UPDATE: This is done now!!
15851588

15861589
if (entry.th32ParentProcessID == targetpid) {
15871590
exeName = WideToString(entry.szExeFile); // this stores the name of our pid we're looking at in a var
1588-
exeNames.emplace(exeNames.begin(), exeName); // this adds this to the front of the list
1589-
// in this case, we are adding stuff to the front of the list, since we're looking at children
1590-
// you might've noticed this doesn't have an emplace_front() like emplace_back() since
1591-
// it's inefficient and the creators of the vector lib didn't do it
1592-
pidNames.emplace(pidNames.begin(), entry.th32ProcessID);
1593-
ULONGLONG childTime = GetProcessCreationTime(entry.th32ProcessID);
1594-
exeTimes.emplace(exeTimes.begin(), childTime); // we don't even use this but we need to keep all the vectors the same length
1595-
parentPids.emplace(parentPids.begin(), entry.th32ProcessID); // just fill it up, we aren't using it
1591+
exeNames.emplace_back(exeName); =
1592+
pidNames.emplace_back(entry.th32ProcessID);
1593+
parentPids.emplace_back(entry.th32ProcessID); // just fill it up, we aren't using it
15961594
children++; // keeps track of how many children we have (that sounds wrong when you say it)
15971595

15981596
}
@@ -1603,15 +1601,8 @@ UPDATE: This is done now!!
16031601
}
16041602

16051603

1606-
CloseHandle(hSnapshot); // we're only closing the handle until we finish messing with the snapshot
1607-
//phew thankfully we're done with that mess
1608-
// now we need to reverse all the vector lists we made so
1609-
// that the ancestry tree is correctly diisplayed from root to children like witr
1610-
// in c++20 there is a new way to reverse called ranges or smth but i won't use that
1611-
std::reverse(exeNames.begin(), exeNames.end());
1612-
std::reverse(exeTimes.begin(), exeTimes.end());
1613-
std::reverse(pidNames.begin(), pidNames.end());
1614-
std::reverse(parentPids.begin(), parentPids.end());
1604+
1605+
16151606
// now get the size of one of the lists to know how many we got (they should all be the same length)
16161607
size_t nameSize = exeNames.size();
16171608

@@ -2137,7 +2128,7 @@ int main(int argc, char* argv[]) {
21372128
// snapshot of all processes in the system first so we can pass it to every function from there on
21382129

21392130
HANDLE hshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2140-
if (INVALID_HANDLE_VALUE == hshot) {return {};}
2131+
if (INVALID_HANDLE_VALUE == hshot) {return 1;}
21412132
PIDinspect(pids, trash, hshot);
21422133
CloseHandle(hshot);
21432134
} else {
@@ -2158,7 +2149,7 @@ int main(int argc, char* argv[]) {
21582149
else if (arg[0] != '-') { // if it doesn't start with -- or -
21592150
std::string procName = arg;
21602151
HANDLE hshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2161-
if (INVALID_HANDLE_VALUE == hshot) {return {};}
2152+
if (INVALID_HANDLE_VALUE == hshot) {return 1;}
21622153
ProcInfos r = findMyProc(procName.c_str(), hshot);
21632154
if (!r.pids.empty()) {
21642155
std::vector<DWORD> dwPids(r.pids.begin(), r.pids.end());

0 commit comments

Comments
 (0)