From f6a9859562ddeb04e882e4a3d15adbf64345452a Mon Sep 17 00:00:00 2001 From: Zufu Liu Date: Sun, 17 Jul 2022 14:09:52 +1000 Subject: Feature [feature-requests:#1441] Line state optimized to avoid excess allocations by always allocating for every line. --- doc/ScintillaDoc.html | 4 +++- doc/ScintillaHistory.html | 14 ++++++++++++++ src/Document.cxx | 2 +- src/PerLine.cxx | 17 +++++++++-------- src/PerLine.h | 2 +- test/unit/testPerLine.cxx | 12 ++++++------ 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html index 1529fc13c..b1d2b5e09 100644 --- a/doc/ScintillaDoc.html +++ b/doc/ScintillaDoc.html @@ -2900,7 +2900,9 @@ struct Sci_TextToFindFull {

SCI_GETMAXLINESTATE → int
- This returns the last line that has any line state.

+ This returns the last line that has any line state. + This has been made less useful by an optimization that always allocates for all lines if any line's state was set. + It can still distinguish cases where line state was never set for any lines.

Style definition

diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html index 8d7952b2d..1ac7da9b0 100644 --- a/doc/ScintillaHistory.html +++ b/doc/ScintillaHistory.html @@ -573,6 +573,20 @@

Releases

+

+ Release 5.2.5 +

+

Release 5.2.4

diff --git a/src/Document.cxx b/src/Document.cxx index 8e80502e0..5fd2f79e5 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -2424,7 +2424,7 @@ void Document::SetLexInterface(std::unique_ptr pLexInterface) noex } int SCI_METHOD Document::SetLineState(Sci_Position line, int state) { - const int statePrevious = States()->SetLineState(line, state); + const int statePrevious = States()->SetLineState(line, state, LinesTotal()); if (state != statePrevious) { const DocModification mh(ModificationFlags::ChangeLineState, LineStart(line), 0, 0, nullptr, static_cast(line)); diff --git a/src/PerLine.cxx b/src/PerLine.cxx index 2ea3f0ce4..f3fddcf27 100644 --- a/src/PerLine.cxx +++ b/src/PerLine.cxx @@ -262,15 +262,13 @@ void LineLevels::ClearLevels() { } int LineLevels::SetLevel(Sci::Line line, int level, Sci::Line lines) { - int prev = 0; + int prev = level; if ((line >= 0) && (line < lines)) { if (!levels.Length()) { ExpandLevels(lines + 1); } prev = levels[line]; - if (prev != level) { - levels[line] = level; - } + levels[line] = level; } return prev; } @@ -312,10 +310,13 @@ void LineState::RemoveLine(Sci::Line line) { } } -int LineState::SetLineState(Sci::Line line, int state) { - lineStates.EnsureLength(line + 1); - const int stateOld = lineStates[line]; - lineStates[line] = state; +int LineState::SetLineState(Sci::Line line, int state, Sci::Line lines) { + int stateOld = state; + if ((line >= 0) && (line < lines)) { + lineStates.EnsureLength(lines + 1); + stateOld = lineStates[line]; + lineStates[line] = state; + } return stateOld; } diff --git a/src/PerLine.h b/src/PerLine.h index 3f7defa3c..ef9e36932 100644 --- a/src/PerLine.h +++ b/src/PerLine.h @@ -111,7 +111,7 @@ public: void InsertLines(Sci::Line line, Sci::Line lines) override; void RemoveLine(Sci::Line line) override; - int SetLineState(Sci::Line line, int state); + int SetLineState(Sci::Line line, int state, Sci::Line lines); int GetLineState(Sci::Line line); Sci::Line GetMaxLineState() const noexcept; }; diff --git a/test/unit/testPerLine.cxx b/test/unit/testPerLine.cxx index 5209c194e..74881f40e 100644 --- a/test/unit/testPerLine.cxx +++ b/test/unit/testPerLine.cxx @@ -226,11 +226,11 @@ TEST_CASE("LineState") { } SECTION("SetLineState") { - REQUIRE(0 == ls.SetLineState(1, 200)); + REQUIRE(0 == ls.SetLineState(1, 200, 2)); REQUIRE(0 == ls.GetLineState(0)); REQUIRE(200 == ls.GetLineState(1)); REQUIRE(0 == ls.GetLineState(2)); - REQUIRE(0 == ls.SetLineState(2, 400)); + REQUIRE(0 == ls.SetLineState(2, 400, 3)); REQUIRE(0 == ls.GetLineState(0)); REQUIRE(200 == ls.GetLineState(1)); REQUIRE(400 == ls.GetLineState(2)); @@ -244,11 +244,11 @@ TEST_CASE("LineState") { SECTION("InsertRemoveLine") { REQUIRE(0 == ls.GetMaxLineState()); - ls.SetLineState(1, 1); - ls.SetLineState(2, 2); - REQUIRE(3 == ls.GetMaxLineState()); - ls.InsertLine(2); + ls.SetLineState(1, 1, 3); + ls.SetLineState(2, 2, 3); REQUIRE(4 == ls.GetMaxLineState()); + ls.InsertLine(2); + REQUIRE(5 == ls.GetMaxLineState()); REQUIRE(0 == ls.GetLineState(0)); REQUIRE(1 == ls.GetLineState(1)); REQUIRE(2 == ls.GetLineState(2)); -- cgit v1.2.3