Skip to content

Commit

Permalink
Disable style buffer for Text File and 2nd Text File.
Browse files Browse the repository at this point in the history
This reduced memory usage when loading big text/log files, issue #125.
  • Loading branch information
zufuliu committed Sep 17, 2020
1 parent d128883 commit 0ae71f9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
13 changes: 13 additions & 0 deletions scintilla/src/CellBuffer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,19 @@ void CellBuffer::Allocate(Sci::Position newSize) {
}
}

bool CellBuffer::EnsureStyleBuffer(bool hasStyles_) {
if (hasStyles != hasStyles_) {
hasStyles = hasStyles_;
if (hasStyles_) {
style.InsertValue(0, substance.Length(), 0);
} else {
style.DeleteAll();
}
return true;
}
return false;
}

void CellBuffer::SetUTF8Substance(bool utf8Substance_) noexcept {
utf8Substance = utf8Substance_;
}
Expand Down
3 changes: 2 additions & 1 deletion scintilla/src/CellBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class UndoHistory {
*/
class CellBuffer {
private:
const bool hasStyles;
bool hasStyles;
const bool largeDocument;
SplitVector<char> substance;
SplitVector<char> style;
Expand Down Expand Up @@ -154,6 +154,7 @@ class CellBuffer {

Sci::Position Length() const noexcept;
void Allocate(Sci::Position newSize);
bool EnsureStyleBuffer(bool hasStyles_);
void SetUTF8Substance(bool utf8Substance_) noexcept;
int GetLineEndTypes() const noexcept {
return utf8LineEnds;
Expand Down
5 changes: 4 additions & 1 deletion scintilla/src/Document.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,10 @@ void Document::StyleToAdjustingLineDuration(Sci::Position pos) {
durationStyleOneLine.AddSample(actions, epStyling.Duration());
}

void Document::LexerChanged() {
void Document::LexerChanged(bool hasStyles_) {
if (cb.EnsureStyleBuffer(hasStyles_)) {
endStyled = 0;
}
// Tell the watchers the lexer has changed.
for (const auto &watcher : watchers) {
watcher.watcher->NotifyLexerChanged(this, watcher.userData);
Expand Down
2 changes: 1 addition & 1 deletion scintilla/src/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class Document : PerLine, public IDocument, public ILoader {
}
void EnsureStyledTo(Sci::Position pos);
void StyleToAdjustingLineDuration(Sci::Position pos);
void LexerChanged();
void LexerChanged(bool hasStyles_);
int GetStyleClock() const noexcept {
return styleClock;
}
Expand Down
3 changes: 2 additions & 1 deletion scintilla/src/EditModel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ EditModel::EditModel() : braces{} {
hotspot = Range(Sci::invalidPosition);
hoverIndicatorPos = Sci::invalidPosition;
wrapWidth = LineLayout::wrapWidthInfinite;
pdoc = new Document(SC_DOCUMENTOPTION_DEFAULT);
// before setting a lexer, style buffer is useless.
pdoc = new Document(SC_DOCUMENTOPTION_STYLES_NONE);
pdoc->AddRef();
pcs = ContractionStateCreate(pdoc->IsLarge());
}
Expand Down
4 changes: 2 additions & 2 deletions scintilla/src/ScintillaBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ void LexState::SetInstance(ILexer5 *instance_) {
instance = nullptr;
}
instance = instance_;
pdoc->LexerChanged();
pdoc->LexerChanged(true);
}

LexState *ScintillaBase::DocumentLexState() {
Expand All @@ -667,7 +667,7 @@ void LexState::SetLexerModule(const LexerModule *lex) {
instance = lexCurrent->Create();
interfaceVersion = instance->Version();
}
pdoc->LexerChanged();
pdoc->LexerChanged(lex->GetLanguage() != SCLEX_NULL);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ void EditSetNewText(LPCSTR lpstrText, DWORD cbText, Sci_Line lineCount) {
#if defined(_WIN64)
// enable conversion between line endings
if (bLargeFileMode || cbText + lineCount >= MAX_NON_UTF8_SIZE) {
int options = SciCall_GetDocumentOptions();
if (!(options & SC_DOCUMENTOPTION_TEXT_LARGE)) {
options |= SC_DOCUMENTOPTION_TEXT_LARGE;
HANDLE pdoc = SciCall_CreateDocument(cbText + 1, options);
const int mask = SC_DOCUMENTOPTION_TEXT_LARGE | SC_DOCUMENTOPTION_STYLES_NONE;
const int options = SciCall_GetDocumentOptions();
if ((options & mask) != mask) {
HANDLE pdoc = SciCall_CreateDocument(cbText + 1, options | mask);
EditReplaceDocument(pdoc);
bLargeFileMode = TRUE;
}
Expand Down Expand Up @@ -972,7 +972,8 @@ BOOL EditLoadFile(LPWSTR pszFile, BOOL bSkipEncodingDetection, EditFileIOStatus
#if defined(_WIN64)
// less than 1/3 available physical memory:
// 1. The buffers we allocated below or when saving file, depends on encoding.
// 2. Scintilla's content buffer and style buffer, see CellBuffer class. The style buffer can be disabled by using SCLEX_NULL and SC_DOCUMENTOPTION_STYLES_NONE.
// 2. Scintilla's content buffer and style buffer, see CellBuffer class.
// The style buffer is disabled when using SCLEX_NULL (Text File, 2nd Text File).
// 3. Extra memory when moving gaps on editing, it may requires more than 2/3 physical memory.
// large file TODO: https://github.com/zufuliu/notepad2/issues/125
// [ ] [> 4 GiB] use SetFilePointerEx() and ReadFile()/WriteFile() to read/write file.
Expand Down

0 comments on commit 0ae71f9

Please sign in to comment.