• R/O
  • SSH
  • HTTPS

yash: 提交


Commit MetaInfo

修訂4086 (tree)
時間2020-09-26 12:23:47
作者magicant

Log Message

The pl_truncate function

The pl_remove function is not so fast because it internally uses
memmove. Removing last elements can be done more quickly.

Change Summary

差異

--- yash/trunk/expand.c (revision 4085)
+++ yash/trunk/expand.c (revision 4086)
@@ -1456,7 +1456,7 @@
14561456 size_t newlen = dest->length;
14571457 if (!shopt_emptylastfield && newlen - oldlen >= 2 * 2 &&
14581458 dest->contents[newlen - 2] == dest->contents[newlen - 1])
1459- pl_remove(dest, newlen - 2, 2);
1459+ pl_truncate(dest, newlen - 2);
14601460
14611461 assert(dest->length - oldlen >= 2);
14621462 return (wchar_t *) &s[ifswhitestartindex];
--- yash/trunk/job.c (revision 4085)
+++ yash/trunk/job.c (revision 4086)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* job.c: job control */
3-/* (C) 2007-2019 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -191,7 +191,7 @@
191191 while (tail > 1 && joblist.contents[tail - 1] == NULL)
192192 tail--;
193193 assert(tail > 0);
194- pl_remove(&joblist, tail, SIZE_MAX);
194+ pl_truncate(&joblist, tail);
195195 }
196196 }
197197
--- yash/trunk/lineedit/editing.c (revision 4085)
+++ yash/trunk/lineedit/editing.c (revision 4086)
@@ -501,7 +501,7 @@
501501 {
502502 for (size_t i = undo_index; i < undo_history.length; i++)
503503 free(undo_history.contents[i]);
504- pl_remove(&undo_history, undo_index, SIZE_MAX);
504+ pl_truncate(&undo_history, undo_index);
505505
506506 // No need to check for overflow in `len + 1' here. Should overflow occur,
507507 // the buffer would not have been allocated successfully.
--- yash/trunk/path.c (revision 4085)
+++ yash/trunk/path.c (revision 4086)
@@ -1284,7 +1284,7 @@
12841284 if (isdir) {
12851285 rp = prev;
12861286 /* result[index] = L'\0'; */
1287- pl_remove(&clist, clist.length - 1, 1);
1287+ pl_truncate(&clist, clist.length - 1);
12881288 path += 2;
12891289 continue;
12901290 } else {
--- yash/trunk/plist.h (revision 4085)
+++ yash/trunk/plist.h (revision 4086)
@@ -53,6 +53,8 @@
5353 __attribute__((nonnull));
5454 extern plist_T *pl_ensuremax(plist_T *list, size_t max)
5555 __attribute__((nonnull));
56+static inline plist_T *pl_truncate(plist_T *list, size_t newlength)
57+ __attribute__((nonnull));
5658 extern plist_T *pl_clear(plist_T *list, void freer(void *elem))
5759 __attribute__((nonnull(1)));
5860 extern plist_T *pl_replace(
@@ -134,6 +136,20 @@
134136 return a;
135137 }
136138
139+/* Shrinks the length of the pointer list to `newlength'.
140+ * `newlength' must not be larger than the current length.
141+ * `maxlength' of the list is not changed.
142+ * It's the caller's responsibility to free the objects pointed by the removed
143+ * pointers. */
144+plist_T *pl_truncate(plist_T *list, size_t newlength)
145+{
146+#ifdef assert
147+ assert(newlength <= list->length);
148+#endif
149+ list->contents[list->length = newlength] = NULL;
150+ return list;
151+}
152+
137153 /* Inserts the first `n' elements of array `a' at offset `i' in pointer list
138154 * `list'.
139155 * NULL elements in `a' are not treated specially.
Show on old repository browser