~eliasnaur/gio-patches

gio: widget: cache Editor's result of its String method v1 PROPOSED

~pierrec
~pierrec: 1
 widget: cache Editor's result of its String method

 2 files changed, 49 insertions(+), 5 deletions(-)
#501692 apple.yml success
#501693 freebsd.yml success
#501694 linux.yml success
#501695 openbsd.yml success
Le jeu. 6 mai 2021 à 09:17, Elias Naur <mail@eliasnaur.com> a écrit :
Next
Le jeu. 6 mai 2021 à 11:27, Elias Naur <mail@eliasnaur.com> a écrit :
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/22539/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gio] widget: cache Editor's result of its String method Export this patch

~pierrec
From: pierre <pierre.curto@gmail.com>

When Editor.String is called multiple times without any change
to the editor, avoid allocating a new string on each call.
Signed-off-by: pierre <pierre.curto@gmail.com>
---
-

 widget/buffer.go      | 19 ++++++++++++++-----
 widget/editor_test.go | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/widget/buffer.go b/widget/buffer.go
index e658d56..6d14ad1 100644
--- a/widget/buffer.go
+++ b/widget/buffer.go
@@ -20,6 +20,9 @@ type editBuffer struct {
	// changed tracks whether the buffer content
	// has changed since the last call to Changed.
	changed bool
	// The cached string to be returned if the buffer has not changed.
	cache    string
	obsolete bool // whether or not the cache is obsolete
}

const minSpace = 5
@@ -43,6 +46,7 @@ func (e *editBuffer) deleteRunes(caret, runes int) int {
		e.gapend += s
		e.changed = e.changed || s > 0
	}
	e.obsolete = e.obsolete || e.changed
	return caret
}

@@ -143,11 +147,15 @@ func (e *editBuffer) ReadRune() (rune, int, error) {
}

func (e *editBuffer) String() string {
	var b strings.Builder
	b.Grow(e.len())
	b.Write(e.text[:e.gapstart])
	b.Write(e.text[e.gapend:])
	return b.String()
	if e.obsolete {
		var b strings.Builder
		b.Grow(e.len())
		b.Write(e.text[:e.gapstart])
		b.Write(e.text[e.gapend:])
		e.cache = b.String()
		e.obsolete = false
	}
	return e.cache
}

func (e *editBuffer) prepend(caret int, s string) {
@@ -155,6 +163,7 @@ func (e *editBuffer) prepend(caret int, s string) {
	copy(e.text[caret:], s)
	e.gapstart += len(s)
	e.changed = e.changed || len(s) > 0
	e.obsolete = e.obsolete || e.changed
}

func (e *editBuffer) runeBefore(idx int) (rune, int) {
diff --git a/widget/editor_test.go b/widget/editor_test.go
index 2babdbf..0633967 100644
--- a/widget/editor_test.go
+++ b/widget/editor_test.go
@@ -476,6 +476,41 @@ func TestSelectMove(t *testing.T) {
	testKey(key.NameDownArrow)
}

func TestEditor_Text(t *testing.T) {
	var ed Editor
	s1 := "hello"
	ed.SetText(s1)
	if got, want := ed.Text(), s1; got != want {
		t.Errorf("got %q; want %q", got, want)
	}
	ed.SetCaret(len(s1), len(s1))
	s2 := " world"
	ed.Insert(s2)
	txt := s1 + s2
	if got, want := ed.Text(), txt; got != want {
		t.Errorf("got %q; want %q", got, want)
	}

	allocs := testing.AllocsPerRun(1, func() {
		if got, want := ed.Text(), txt; got != want {
			t.Errorf("got %q; want %q", got, want)
		}
	})
	if allocs != 0 {
		t.Errorf("expected no allocs, got %f", allocs)
	}

	ed.Insert("!")
	allocs = testing.AllocsPerRun(1, func() {
		if got, want := ed.Text(), txt+"!"; got != want {
			t.Errorf("got %q; want %q", got, want)
		}
	})
	if allocs != 1 {
		t.Errorf("expected alloc, got %f", allocs)
	}
}

func textWidth(e *Editor, lineNum, colStart, colEnd int) float32 {
	var w fixed.Int26_6
	advances := e.lines[lineNum].Layout.Advances
-- 
2.30.2
builds.sr.ht
gio/patches: SUCCESS in 20m38s

[widget: cache Editor's result of its String method][0] from [~pierrec][1]

[0]: https://lists.sr.ht/~eliasnaur/gio-patches/patches/22539
[1]: mailto:pierre.curto@gmail.com

✓ #501692 SUCCESS gio/patches/apple.yml   https://builds.sr.ht/~eliasnaur/job/501692
✓ #501695 SUCCESS gio/patches/openbsd.yml https://builds.sr.ht/~eliasnaur/job/501695
✓ #501693 SUCCESS gio/patches/freebsd.yml https://builds.sr.ht/~eliasnaur/job/501693
✓ #501694 SUCCESS gio/patches/linux.yml   https://builds.sr.ht/~eliasnaur/job/501694