gbranden pushed a commit to branch master
in repository groff.

commit 5164bc1a0dd747df4d1b8b36c4df0f8c5da14234
Author: G. Branden Robinson <[email protected]>
AuthorDate: Wed Jun 10 05:54:04 2026 -0500

    [libgroff]: Rewrite `string::move()`.
    
    * src/libs/libgroff/string.cpp: Continue refactoring to implement new
      invariant: any existing `string` object is backed by "clean" storage.
    
      (string::move): Rewrite.  Use `sfree_alloc()`, which also backs
      `operator=()`, on destination string to perform replacement
      allocation.  Use memcpy(3) to copy contents from source string to
      destination.  Use member function `clear()` on source string.  Add
      assert(3)ion enforcing new invariant.
    
    Performance analysis
    ====================
    Impact seems negligible.  I timed a full groff build from "distclean" to
    completion, using a single core.
    
    Before
    ------
    real    2m48.029s
    user    2m53.073s
    sys     0m21.077s
    
    After
    -----
    real    2m48.352s
    user    2m53.379s
    sys     0m21.279s
---
 ChangeLog                    | 11 +++++++++++
 src/libs/libgroff/string.cpp | 10 ++++------
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4e506cd15..1b1904395 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2026-06-10  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/string.cpp: Continue refactoring to
+       implement new invariant: any existing `string` object is backed
+       by "clean" storage.
+       (string::move): Rewrite.  Use `sfree_alloc()`, which also backs
+       `operator=()`, on destination string to perform replacement
+       allocation.  Use memcpy(3) to copy contents from source string
+       to destination.  Use member function `clear()` on source string.
+       Add assert(3)ion enforcing new invariant.
+
 2026-06-10  G. Branden Robinson <[email protected]>
 
        * src/libs/libgroff/string.cpp (string::operator=)
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index 6fca589be..891070488 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -231,13 +231,11 @@ string &string::operator=(char c)
 
 void string::move(string &s)
 {
-  sfree(ptr);
-  ptr = s.ptr;
+  sfree_alloc(ptr, sz, s.len, &sz);
+  memcpy(ptr, s.ptr, s.len);
   len = s.len;
-  sz = s.sz;
-  s.ptr = 0 /* nullptr */;
-  s.len = 0;
-  s.sz = 0;
+  s.clear();
+  assert(ptr != 0 /* nullptr */);
 }
 
 void string::grow1()

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to