From ec7bc82f9c63b6ec533f7489e67b1c1b18d08dd5 Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Tue, 24 Sep 2013 09:42:30 +0400 Subject: [PATCH] Optimize glyph row clearing and copying routines. * dispextern.h (struct glyph_row): Change layout of struct glyph_row to help copy_row_except_pointers. Adjust comment. * dispnew.c (null_row): Remove. (clear_glyph_row): Use offsetof and memset to find and clear just the members that need clearing. Adjust comment. (copy_row_except_pointers): Likewise for copying. --- src/ChangeLog | 10 ++++++++ src/dispextern.h | 18 ++++++++------ src/dispnew.c | 65 +++++++++++------------------------------------- 3 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 303eb4f9176..28b4da176ae 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2013-09-24 Dmitry Antipov + + Optimize glyph row clearing and copying routines. + * dispextern.h (struct glyph_row): Change layout of struct + glyph_row to help copy_row_except_pointers. Adjust comment. + * dispnew.c (null_row): Remove. + (clear_glyph_row): Use offsetof and memset to find and clear + just the members that need clearing. Adjust comment. + (copy_row_except_pointers): Likewise for copying. + 2013-09-24 Paul Eggert Some minor cleanups of recently-added bool vector code. diff --git a/src/dispextern.h b/src/dispextern.h index 84111bd9958..5ddc177bcf0 100644 --- a/src/dispextern.h +++ b/src/dispextern.h @@ -792,7 +792,10 @@ enum glyph_row_area Rows in window matrices on frames having no frame matrices point to glyphs allocated from the heap via xmalloc; glyphs[LEFT_MARGIN_AREA] is the start address of the allocated - glyph structure array. */ + glyph structure array. + + NOTE: layout of first four members of this structure is important, + see clear_glyph_row and copy_row_except_pointers to check why. */ struct glyph_row { @@ -812,8 +815,13 @@ struct glyph_row removed some day, so don't use it in new code. */ struct glyph *glyphs[1 + LAST_AREA]; - /* Number of glyphs actually filled in areas. */ - short used[LAST_AREA]; + /* Number of glyphs actually filled in areas. This could have size + LAST_AREA, but it's 1 + LAST_AREA to simplify offset calculations. */ + short used[1 + LAST_AREA]; + + /* Hash code. This hash code is available as soon as the row + is constructed, i.e. after a call to display_line. */ + unsigned hash; /* Window-relative x and y-position of the top-left corner of this row. If y < 0, this means that eabs (y) pixels of the row are @@ -846,10 +854,6 @@ struct glyph_row in last row when checking if row is fully visible. */ int extra_line_spacing; - /* Hash code. This hash code is available as soon as the row - is constructed, i.e. after a call to display_line. */ - unsigned hash; - /* First position in this row. This is the text position, including overlay position information etc, where the display of this row started, and can thus be less than the position of the first diff --git a/src/dispnew.c b/src/dispnew.c index 4ec989280ea..f7e3fa54441 100644 --- a/src/dispnew.c +++ b/src/dispnew.c @@ -832,41 +832,17 @@ clear_window_matrices (struct window *w, bool desired_p) See dispextern.h for an overall explanation of glyph rows. ***********************************************************************/ -/* Clear glyph row ROW. Do it in a way that makes it robust against - changes in the glyph_row structure, i.e. addition or removal of - structure members. */ - -static struct glyph_row null_row; +/* Clear glyph row ROW. NOTE: this code relies on the current + layout of `glyphs' and `used' fields of `struct glyph_row'. */ void clear_glyph_row (struct glyph_row *row) { - struct glyph *p[1 + LAST_AREA]; - - /* Save pointers. */ - p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA]; - p[TEXT_AREA] = row->glyphs[TEXT_AREA]; - p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA]; - p[LAST_AREA] = row->glyphs[LAST_AREA]; - - /* Clear. */ - *row = null_row; - - /* Restore pointers. */ - row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA]; - row->glyphs[TEXT_AREA] = p[TEXT_AREA]; - row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA]; - row->glyphs[LAST_AREA] = p[LAST_AREA]; - -#if 0 /* At some point, some bit-fields of struct glyph were not set, - which made glyphs unequal when compared with GLYPH_EQUAL_P. - Redisplay outputs such glyphs, and flickering effects were - the result. This also depended on the contents of memory - returned by xmalloc. If flickering happens again, activate - the code below. If the flickering is gone with that, chances - are that the flickering has the same reason as here. */ - memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]); -#endif + const size_t off = offsetof (struct glyph_row, used); + + eassert (off == sizeof row->glyphs); + /* Zero everything except pointers in `glyphs'. */ + memset ((char *) row + off, 0, sizeof *row - off); } @@ -1005,29 +981,18 @@ swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b) } -/* Copy glyph row structure FROM to glyph row structure TO, except - that glyph pointers, the `used' counts, and the hash values in the - structures are left unchanged. */ +/* Copy glyph row structure FROM to glyph row structure TO, except that + glyph pointers, the `used' counts, and the hash values in the structures + are left unchanged. NOTE: this code relies on the current layout of + `glyphs', `used', `hash' and `x' fields of `struct glyph_row'. */ static void copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from) { - struct glyph *pointers[1 + LAST_AREA]; - short used[LAST_AREA]; - unsigned hashval; - - /* Save glyph pointers of TO. */ - memcpy (pointers, to->glyphs, sizeof to->glyphs); - memcpy (used, to->used, sizeof to->used); - hashval = to->hash; - - /* Do a structure assignment. */ - *to = *from; - - /* Restore original pointers of TO. */ - memcpy (to->glyphs, pointers, sizeof to->glyphs); - memcpy (to->used, used, sizeof to->used); - to->hash = hashval; + const size_t off = offsetof (struct glyph_row, x); + + eassert (off == sizeof to->glyphs + sizeof to->used + sizeof to->hash); + memcpy ((char *) to + off, (char *) from + off, sizeof *to - off); } -- 2.39.2