Hi there,
I'm a senior software engineer who also has clue about biology/medicine and is a specialist for algorithm design and code optimization.
I've recently had a talk with ChatGPT about how I might contribute to medical research in my spare time. ChatGPT gave me the advice to contribute to COPASI, as there are supposedly many code passages that could be optimized.
I've only taken a short glance into the code of COPASI so far, but I immediately found a code passage that could be optimized:
// static
void CMathContainer::createRelocation(const size_t & newSize, const size_t & oldSize,
CMath::sRelocate & currentRelocation,
std::vector< CMath::sRelocate > & relocations,
const bool & modifiedAtEnd)
{
if (newSize != oldSize)
{
// Size modifications are made at the end of the current section
if (modifiedAtEnd)
{
currentRelocation.pValueEnd = currentRelocation.pValueEnd + std::min(newSize, oldSize);
currentRelocation.pObjectEnd = currentRelocation.pObjectEnd + std::min(newSize, oldSize);
if (currentRelocation.pValueStart != currentRelocation.pValueEnd)
{
relocations.push_back(currentRelocation);
}
currentRelocation.pValueStart = currentRelocation.pValueEnd - std::min(newSize, oldSize) + oldSize;
currentRelocation.pObjectStart = currentRelocation.pObjectEnd - std::min(newSize, oldSize) + oldSize;
currentRelocation.pValueEnd = currentRelocation.pValueStart;
currentRelocation.pObjectEnd = currentRelocation.pObjectStart;
currentRelocation.offset += (newSize - oldSize);
}
// Size modifications are made at the beginning of the current section
else
{
if (currentRelocation.pValueStart != currentRelocation.pValueEnd)
{
relocations.push_back(currentRelocation);
}
currentRelocation.pValueEnd += oldSize;
currentRelocation.pObjectEnd += oldSize;
currentRelocation.pValueStart = currentRelocation.pValueEnd - std::min(newSize, oldSize);
currentRelocation.pObjectStart = currentRelocation.pObjectEnd - std::min(newSize, oldSize);
currentRelocation.offset += (newSize - oldSize);
}
}
else if (newSize > 0)
{
currentRelocation.pValueEnd += newSize;
currentRelocation.pObjectEnd += newSize;
}
}
In this code, std::min(newSize, oldSize) is evaluated twice or even four times, although it would suffice to evaluate it only once as the values of newSize and oldSize don't change. I'd propose the following solution:
// static
void CMathContainer::createRelocation(const size_t & newSize, const size_t & oldSize,
CMath::sRelocate & currentRelocation,
std::vector< CMath::sRelocate > & relocations,
const bool & modifiedAtEnd)
{
if (newSize != oldSize)
{
size_t minSize =
std::min(newSize, oldSize);
// Size modifications are made at the end of the current section
if (modifiedAtEnd)
{
currentRelocation.pValueEnd = currentRelocation.pValueEnd +
minSize
;
currentRelocation.pObjectEnd = currentRelocation.pObjectEnd +
minSize
;
if (currentRelocation.pValueStart != currentRelocation.pValueEnd)
{
relocations.push_back(currentRelocation);
}
currentRelocation.pValueStart = currentRelocation.pValueEnd -
minSize
+ oldSize;
currentRelocation.pObjectStart = currentRelocation.pObjectEnd -
minSize
+ oldSize;
currentRelocation.pValueEnd = currentRelocation.pValueStart;
currentRelocation.pObjectEnd = currentRelocation.pObjectStart;
currentRelocation.offset += (newSize - oldSize);
}
// Size modifications are made at the beginning of the current section
else
{
if (currentRelocation.pValueStart != currentRelocation.pValueEnd)
{
relocations.push_back(currentRelocation);
}
currentRelocation.pValueEnd += oldSize;
currentRelocation.pObjectEnd += oldSize;
currentRelocation.pValueStart = currentRelocation.pValueEnd -
minSize
;
currentRelocation.pObjectStart = currentRelocation.pObjectEnd -
minSize
;
currentRelocation.offset += (newSize - oldSize);
}
}
else if (newSize > 0)
{
currentRelocation.pValueEnd += newSize;
currentRelocation.pObjectEnd += newSize;
}
}
Cheers
Claus