-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modernize CommandHistory and switch to int32 #15782
Modernize CommandHistory and switch to int32 #15782
Conversation
return _commands; | ||
} | ||
|
||
[[nodiscard]] HRESULT CommandHistory::RetrieveNth(const Index index, std::span<wchar_t> buffer, size_t& commandSize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh we have Get and Retrieve. Can we rename Retrieve
to Copy
? They're both synonyms otherwise, and I hate methods whose names are synonyms but behaviors are totally unrelated.
ALSO THIS UPDATES STATE WHERE GET DOESN'T.
AGH.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't mind, I'd rather not make such a change right now. It would make the diff quite a bit larger, which might not be a good timing, given that I'll very soon rewrite every other caller of these functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows Driver Frameworks likewise have Get and Retrieve; see https://learn.microsoft.com/windows-hardware/drivers/wdf/kernel-mode-driver-framework-architecture#object-properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh, I'll accept it!
|
||
LastDisplayed = iDisp; | ||
return str; | ||
_Dec(LastDisplayed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We lost the increment/move to next command version (491-486 Left). When did it happen? Why is it OK to remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally didn't intend to refactor this function, but it really puzzled me what it does. Hence the rewrite - it's now much more straightforward IMO.
I only understood it when I approached the function from a "first principles" kind of viewpoint, asking myself what a remove function would have to do:
- Ensure that the given index is in bounds
- Remove the given index
- Since all items after the given index have shifted one slot to the left, any index referring to those items also needs to shift one to the left
That last point is what your question is about: If the LastDisplayed
value is after the given index, the item it was referring to has shifted to the left in the vector, and so LastDisplayed
must be decremented.
The previous code was only that complex because the history used to be a ring buffer and there used to be not 1 but 3 indices that had to be shifted around. And it was possible that the items would shift to the right (due to it being a ring buffer) and so LastDisplayed
had to be incremented.
Due to the switch to a std::vector
none of that is necessary anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AAAH FECK IT WAS A RING BUFFER
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open Q about the increment codepath
abb51ee
to
cfc4f89
Compare
And this one got |
31f7961
to
aa4fdda
Compare
c6e5f79
into
main
This commit slightly modernizes
CommandHistory
by leaning more heavilyon the STL container functionalities. For one, it uses for-range
iterations to loop through
_commands
instead of usingGetNth
on every iteration. Another major improvement however is that
the code previously copied entire
CommandHistory
instances out ofthe linked list
s_historyLists
, then removed the slot and copied(not moved!) that instance into the front again. Now it uses the
splice
function fromstd::list
to do it inO(1)
and virtuallycost-free.
Another major improvement (and the one I'm personally interested in)
is the switch from
SHORT
toint32_t
. This will greatly simplifythe implementation of the future
COOKED_READ_DATA
class, as thelarger integer type will remove worries about over/underflow.
For instance, we can then just blindly increment/decrement the history
position and then only later clamp it to the expected range.
Validation Steps Performed