Author Topic: Debuggergdb.cpp redundant code  (Read 5507 times)

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 679
Debuggergdb.cpp redundant code
« on: March 19, 2022, 03:07:07 am »
In src\plugins\contrib-wip\debuggerGDB_MI\debuggergdb.cpp the OnTimer function is:
Code
void DebuggerGDB::OnTimer(cb_unused wxTimerEvent& event)
{
    // send any buffered (previous) output
    ParseOutput(wxEmptyString);

    CheckIfConsoleIsClosed();

    wxWakeUpIdle();
}
The ParseOutput function is:
Code
void DebuggerGDB::ParseOutput(const wxString& output)
{
    if (!output.IsEmpty() && m_State.HasDriver())
    {
        m_State.GetDriver()->ParseOutput(output);
    }
}
So my conclusion is that the OnTimer line
Code
ParseOutput(wxEmptyString);
is redundant. Have I missed something?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Debuggergdb.cpp redundant code
« Reply #1 on: March 19, 2022, 03:31:55 am »
From my point of view, you are correct.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2813
Re: Debuggergdb.cpp redundant code
« Reply #2 on: March 19, 2022, 06:01:55 am »
That's just one of those laughable moments that make me think: "I hope I didn't do that!"

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 679
Re: Debuggergdb.cpp redundant code
« Reply #3 on: March 19, 2022, 06:58:45 am »
Another code snippet to check out, but this one will not slow things down as much as the previous one:

Code
        buffer.Remove(idx);
        // remove the '>>>>>>' part of the prompt (or whats left of it)
        int cnt = 6; // max 6 '>'
        while (!buffer.empty() && buffer.Last() == _T('>') && cnt--)
            buffer.RemoveLast();
        if (!buffer.empty() && buffer.Last() == _T('\n'))
            buffer.RemoveLast();
        cmd->ParseOutput(buffer.Left(idx));
Check the first line and then ask yourself why the last line is not
Code
cmd->ParseOutput(buffer);


Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Debuggergdb.cpp redundant code
« Reply #4 on: March 20, 2022, 12:53:50 am »
Another code snippet to check out, but this one will not slow things down as much as the previous one:

Code
        buffer.Remove(idx);
        // remove the '>>>>>>' part of the prompt (or whats left of it)
        int cnt = 6; // max 6 '>'
        while (!buffer.empty() && buffer.Last() == _T('>') && cnt--)
            buffer.RemoveLast();
        if (!buffer.empty() && buffer.Last() == _T('\n'))
            buffer.RemoveLast();
        cmd->ParseOutput(buffer.Left(idx));
Check the first line and then ask yourself why the last line is not
Code
cmd->ParseOutput(buffer);

The first line is: Removes all characters from the string starting at idx, but it just returned the modified buffer, so the buffer is not changed. Right?

The last line is: Removes all the characters after the idx, and pass to the ParseOutput function.

So, the first line did nothing?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 679
Re: Debuggergdb.cpp redundant code
« Reply #5 on: March 20, 2022, 01:59:56 am »
The
Code
buffer.Remove(idx);
removes the characters in the buffer, so it removed from the idx offset to the end.
The RemoveLast calls remove the '>' if they are there.

So the last line calling with only the Left idx characters is redundant as the first line removed the characters after idx.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Debuggergdb.cpp redundant code
« Reply #6 on: March 20, 2022, 02:34:53 am »
The
Code
buffer.Remove(idx);
removes the characters in the buffer, so it removed from the idx offset to the end.
The RemoveLast calls remove the '>' if they are there.

So the last line calling with only the Left idx characters is redundant as the first line removed the characters after idx.

OK, you are correct!
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.