Skip to content

Commit 0978460

Browse files
committed
panel: reuse single rich str instead two alloc rich str
Reallocation rich string inside body for loop may be impractical with freeing inside, same applies to new and old rich strings, it would be more logical to use one object and reuse it to save memory
1 parent 8211b10 commit 0978460

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Panel.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ void Panel_splice(Panel* this, Vector* from) {
219219
this->needsRedraw = true;
220220
}
221221

222+
static void RichString_reset(RichString* this) {
223+
this->chlen = 0;
224+
this->highlightAttr = 0;
225+
RichString_setChar(this, 0, 0);
226+
}
227+
222228
void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelected, bool hideFunctionBar) {
223229
assert (this != NULL);
224230

@@ -278,10 +284,11 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect
278284
: CRT_colors[PANEL_SELECTION_UNFOCUS];
279285

280286
if (this->needsRedraw || force_redraw) {
287+
RichString_begin(item);
281288
int line = 0;
282289
for (int i = first; line < h && i < upTo; i++) {
283290
const Object* itemObj = Vector_get(this->items, i);
284-
RichString_begin(item);
291+
RichString_reset(&item);
285292
Object_display(itemObj, &item);
286293
int itemLen = RichString_sizeVal(item);
287294
int amt = MINIMUM(itemLen - scrollH, this->w);
@@ -298,37 +305,39 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect
298305
RichString_printoffnVal(item, y + line, x, scrollH, amt);
299306
if (item.highlightAttr)
300307
attrset(CRT_colors[RESET_COLOR]);
301-
RichString_delete(&item);
302308
line++;
303309
}
310+
RichString_delete(&item);
304311
while (line < h) {
305312
mvhline(y + line, x, ' ', this->w);
306313
line++;
307314
}
308315

309316
} else {
317+
RichString_begin(rs);
318+
310319
const Object* oldObj = Vector_get(this->items, this->oldSelected);
311-
RichString_begin(old);
312-
Object_display(oldObj, &old);
313-
int oldLen = RichString_sizeVal(old);
314-
const Object* newObj = Vector_get(this->items, this->selected);
315-
RichString_begin(new);
316-
Object_display(newObj, &new);
317-
int newLen = RichString_sizeVal(new);
318-
this->selectedLen = newLen;
320+
Object_display(oldObj, &rs);
321+
int oldLen = RichString_sizeVal(rs);
319322
mvhline(y + this->oldSelected - first, x + 0, ' ', this->w);
320323
if (scrollH < oldLen)
321-
RichString_printoffnVal(old, y + this->oldSelected - first, x,
324+
RichString_printoffnVal(rs, y + this->oldSelected - first, x,
322325
scrollH, MINIMUM(oldLen - scrollH, this->w));
326+
327+
const Object* newObj = Vector_get(this->items, this->selected);
328+
RichString_reset(&rs);
329+
Object_display(newObj, &rs);
330+
int newLen = RichString_sizeVal(rs);
331+
this->selectedLen = newLen;
323332
attrset(selectionColor);
324333
mvhline(y + this->selected - first, x + 0, ' ', this->w);
325-
RichString_setAttr(&new, selectionColor);
334+
RichString_setAttr(&rs, selectionColor);
326335
if (scrollH < newLen)
327-
RichString_printoffnVal(new, y + this->selected - first, x,
336+
RichString_printoffnVal(rs, y + this->selected - first, x,
328337
scrollH, MINIMUM(newLen - scrollH, this->w));
329338
attrset(CRT_colors[RESET_COLOR]);
330-
RichString_delete(&new);
331-
RichString_delete(&old);
339+
340+
RichString_delete(&rs);
332341
}
333342

334343
if (focus && (this->needsRedraw || force_redraw || !this->wasFocus)) {

0 commit comments

Comments
 (0)