Skip to content

Commit

Permalink
Implement naive small string optimization for undo action by using st…
Browse files Browse the repository at this point in the history
…ructure padding.

See https://sourceforge.net/p/scintilla/feature-requests/1458/,
most action is only single byte, just store them in the padding.
  • Loading branch information
zufuliu committed Jun 9, 2023
1 parent 18aa425 commit 29e8b14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 9 additions & 4 deletions scintilla/src/CellBuffer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "VectorISA.h"

#include "Position.h"
#include "UniqueString.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
Expand Down Expand Up @@ -333,14 +332,20 @@ class LineVector final : public ILineVector {
};

void Action::Create(ActionType at_, Sci::Position position_, const char *data_, Sci::Position lenData_, bool mayCoalesce_) {
position = position_;
at = at_;
mayCoalesce = mayCoalesce_;
position = position_;
lenData = lenData_;
data = nullptr;
char *ptr = nullptr;
if (lenData_) {
data = UniqueCopy(data_, lenData_);
char *text = smallData;
if (lenData_ > smallSize) {
text = new char[lenData_];
ptr = text;
}
memcpy(text, data_, lenData_);
}
data.reset(ptr);
}

void Action::Clear() noexcept {
Expand Down
4 changes: 3 additions & 1 deletion scintilla/src/CellBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ enum class ActionType : uint8_t { insert, remove, start, container };
* Actions are used to store all the information required to perform one undo/redo step.
*/
class Action {
static constexpr Sci::Position smallSize = sizeof(size_t) - 2;
public:
ActionType at = ActionType::insert;
bool mayCoalesce = false;
char smallData[smallSize]{};
Sci::Position position = 0;
Sci::Position lenData = 0;
std::unique_ptr<char[]> data;
Expand All @@ -44,7 +46,7 @@ class Action {
void Create(ActionType at_, Sci::Position position_ = 0, const char *data_ = nullptr, Sci::Position lenData_ = 0, bool mayCoalesce_ = true);
void Clear() noexcept;
const char *Data() const noexcept {
return data.get();
return (lenData <= smallSize) ? smallData : data.get();
}
};

Expand Down

0 comments on commit 29e8b14

Please sign in to comment.