1
1
mirror of https://github.com/walles/moar.git synced 2024-09-19 16:08:02 +03:00

Improve memory usage for large files

Test:
* Open a 35MB text file
* Search it for some string it doesn't contain

Memory usage before this change: 2.3GB
Memory usage after this change:  0.5GB

The trick is to only provide cells from Lines on demand, since we only
need a few of them when scrolling.

And not storing all of those cells is where the gain in memory usage
comes from.
This commit is contained in:
Johan Walles 2021-04-24 16:24:34 +02:00
parent c8bc7b0161
commit c8a8cb4517
2 changed files with 6 additions and 9 deletions

View File

@ -21,10 +21,8 @@ var sgrSequencePattern = regexp.MustCompile("\x1b\\[([0-9;]*m)")
// A Line represents a line of text that can / will be paged
type Line struct {
done bool
raw *string
plain *string
cells []twin.Cell
}
// NewLine creates a new Line from a (potentially ANSI / man page formatted) string
@ -32,14 +30,13 @@ func NewLine(raw string) *Line {
return &Line{
raw: &raw,
plain: nil,
cells: nil,
}
}
// Tokens returns a representation of the string split into styled tokens
func (line *Line) Tokens() []twin.Cell {
line.parse()
return line.cells
cells, _ := cellsFromString(*line.raw)
return cells
}
// Plain returns a plain text representation of the initial string
@ -49,13 +46,12 @@ func (line *Line) Plain() string {
}
func (line *Line) parse() {
if line.done {
if line.plain != nil {
// Already done
return
}
line.cells, line.plain = cellsFromString(*line.raw)
line.done = true
_, line.plain = cellsFromString(*line.raw)
}
// SetManPageFormatFromEnv parses LESS_TERMCAP_xx environment variables and

View File

@ -2,6 +2,7 @@ package m
import (
"fmt"
"github.com/walles/moar/twin"
)
@ -18,7 +19,7 @@ func (p *Pager) Page() error {
return
}
// FIXME: Consider moving this logic into the twin package.
// FIXME: Consider moving this logic into the twin package, or into Pager.
_, height := p.screen.Size()
lines := p.reader.GetLines(p.firstLineOneBased, height-1).lines
for _, line := range lines {