unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Alex Gramiak <agrambot@gmail.com>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: emacs-devel@gnu.org
Subject: Re: Using __builtin_expect (likely/unlikely macros)
Date: Mon, 15 Apr 2019 18:16:02 -0600	[thread overview]
Message-ID: <87h8aykdod.fsf@gmail.com> (raw)
In-Reply-To: <6919a4c8-df76-ea1e-34db-1fa62a360e5a@cs.ucla.edu> (Paul Eggert's message of "Sun, 14 Apr 2019 21:41:02 -0700")

[-- Attachment #1: Type: text/plain, Size: 934 bytes --]

Paul Eggert <eggert@cs.ucla.edu> writes:

> Alex Gramiak wrote:
>> Though I don't think that these macros, used sparingly around code such
>> as emacs_abort, would make it harder to read/write/maintain.
>
> These macro calls would not help near calls to emacs_abort, as it should already
> be obvious to a careful human reader that the jump to emacs_abort is the road
> less traveled. (That's also obvious to GCC, since emacs_abort is _Noreturn.)

To human readers, yes, but from what I can tell, GCC is mixed on this.
When applying the following diff that surrounds emacs_abort in
bytecode.c and xdisp.c, the assembly in both applied/unapplied is the
same for bytecode.c, but not for xdisp.c (even without the LIKELY
cases). I tested using gcc -O2 -S.

Also, I tried putting a LIKELY in lisp_h_CHECK_TYPE (seems like a nice
place for one) and that yielded different assembly for fns.c, even
though wrong_type_argument is _Noreturn.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: likely --]
[-- Type: text/x-patch, Size: 6351 bytes --]

diff --git a/src/bytecode.c b/src/bytecode.c
index 40977799bf..ce1a7bd254 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -39,6 +39,7 @@ along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
 #ifndef BYTE_CODE_SAFE
 # define BYTE_CODE_SAFE false
 #endif
+#define unlikely(expr) (__builtin_expect (expr, 0))
 
 /* Define BYTE_CODE_METER to generate a byte-op usage histogram.  */
 /* #define BYTE_CODE_METER */
@@ -404,7 +405,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
       int op;
       enum handlertype type;
 
-      if (BYTE_CODE_SAFE && ! (stack_base <= top && top < stack_lim))
+      if (unlikely (BYTE_CODE_SAFE && ! (stack_base <= top && top < stack_lim)))
 	emacs_abort ();
 
 #ifdef BYTE_CODE_METER
@@ -664,9 +665,9 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 	op_branch:
 	  op -= pc - bytestr_data;
 	op_relative_branch:
-	  if (BYTE_CODE_SAFE
+	  if (unlikely (BYTE_CODE_SAFE
 	      && ! (bytestr_data - pc <= op
-		    && op < bytestr_data + bytestr_length - pc))
+		    && op < bytestr_data + bytestr_length - pc)))
 	    emacs_abort ();
 	  quitcounter += op < 0;
 	  if (!quitcounter)
@@ -1397,7 +1398,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 	       number of cases is less, which uses a simple vector for linear
 	       search as the jump table.  */
             Lisp_Object jmp_table = POP;
-	    if (BYTE_CODE_SAFE && !HASH_TABLE_P (jmp_table))
+	    if (unlikely (BYTE_CODE_SAFE && !HASH_TABLE_P (jmp_table)))
               emacs_abort ();
             Lisp_Object v1 = POP;
             ptrdiff_t i;
@@ -1426,7 +1427,7 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 	    if (i >= 0)
 	      {
 		Lisp_Object val = HASH_VALUE (h, i);
-		if (BYTE_CODE_SAFE && !FIXNUMP (val))
+		if (unlikely (BYTE_CODE_SAFE && !FIXNUMP (val)))
 		  emacs_abort ();
 		op = XFIXNUM (val);
 		goto op_branch;
@@ -1436,8 +1437,8 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 
 	CASE_DEFAULT
 	CASE (Bconstant):
-	  if (BYTE_CODE_SAFE
-	      && ! (Bconstant <= op && op < Bconstant + const_length))
+	  if (unlikely (BYTE_CODE_SAFE
+                        && ! (Bconstant <= op && op < Bconstant + const_length)))
 	    emacs_abort ();
 	  PUSH (vectorp[op - Bconstant]);
 	  NEXT;
diff --git a/src/xdisp.c b/src/xdisp.c
index a88fc698b8..9872f69cb0 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -344,7 +344,8 @@ along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
 
 /* Holds the list (error).  */
 static Lisp_Object list_of_error;
-
+#define UNLIKELY(expr) (__builtin_expect (expr, 0))
+#define LIKELY(expr) (__builtin_expect (expr, 1))
 #ifdef HAVE_WINDOW_SYSTEM
 
 /* Test if overflow newline into fringe.  Called with iterator IT
@@ -8322,7 +8323,7 @@ compute_stop_pos_backwards (struct it *it)
       reseat_1 (it, pos, false);
       compute_stop_pos (it);
       /* We must advance forward, right?  */
-      if (it->stop_charpos <= charpos)
+      if (UNLIKELY (it->stop_charpos <= charpos))
 	emacs_abort ();
     }
   while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
@@ -8371,7 +8372,7 @@ handle_stop_backwards (struct it *it, ptrdiff_t charpos)
 	it->current.string_pos = string_pos (charpos, it->string);
       compute_stop_pos (it);
       /* We must advance forward, right?  */
-      if (it->stop_charpos <= it->prev_stop)
+      if (UNLIKELY (it->stop_charpos <= it->prev_stop))
 	emacs_abort ();
       charpos = it->stop_charpos;
     }
@@ -11443,7 +11444,7 @@ pop_message_unwind (void)
 void
 check_message_stack (void)
 {
-  if (!NILP (Vmessage_stack))
+  if (UNLIKELY (!NILP (Vmessage_stack)))
     emacs_abort ();
 }
 
@@ -15495,7 +15496,7 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
       /* Need to compute x that corresponds to GLYPH.  */
       for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
 	{
-	  if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
+	  if (UNLIKELY (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]))
 	    emacs_abort ();
 	  x += g->pixel_width;
 	}
@@ -16827,9 +16828,9 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
 
   /* Some sanity checks.  */
   CHECK_WINDOW_END (w);
-  if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
+  if (UNLIKELY (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint)))
     emacs_abort ();
-  if (BYTEPOS (opoint) < CHARPOS (opoint))
+  if (UNLIKELY (BYTEPOS (opoint) < CHARPOS (opoint)))
     emacs_abort ();
 
   if (mode_line_update_needed (w))
@@ -19318,9 +19319,9 @@ try_window_id (struct window *w)
       adjust_window_ends (w, last_text_row, false);
       eassert (w->window_end_bytepos >= 0);
     }
-  else if (first_unchanged_at_end_row == NULL
-	   && last_text_row == NULL
-	   && last_text_row_at_end == NULL)
+  else if (LIKELY (first_unchanged_at_end_row == NULL
+                   && last_text_row == NULL
+                   && last_text_row_at_end == NULL))
     {
       /* Displayed to end of window, but no line containing text was
 	 displayed.  Lines were deleted at the end of the window.  */
@@ -21015,7 +21016,7 @@ find_row_edges (struct it *it, struct glyph_row *row,
 	   which puts the iterator at the beginning of the next line, in
 	   the logical order. */
 	row->maxpos = it->current.pos;
-      else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
+      else if (LIKELY (max_pos == min_pos && it->method != GET_FROM_BUFFER))
 	/* A line that is entirely from a string/image/stretch...  */
 	row->maxpos = row->minpos;
       else
@@ -25210,7 +25211,7 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
 		}
 	      break;
 	    }
-	  else if (x + glyph->pixel_width >= it->first_visible_x)
+	  else if (LIKELY (x + glyph->pixel_width >= it->first_visible_x))
 	    {
 	      /* Glyph is at least partially visible.  */
 	      ++it->hpos;
@@ -27847,7 +27848,7 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
 	  spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 	}
     }
-  else if (what == IT_TRUNCATION)
+  else if (LIKELY (what == IT_TRUNCATION))
     {
       /* Truncation glyph.  */
       SET_GLYPH_FROM_CHAR (glyph, '$');

  reply	other threads:[~2019-04-16  0:16 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-15  0:15 Using __builtin_expect (likely/unlikely macros) Alex Gramiak
2019-04-15  1:18 ` Paul Eggert
2019-04-15  3:11   ` Alex Gramiak
2019-04-15  4:41     ` Paul Eggert
2019-04-16  0:16       ` Alex Gramiak [this message]
2019-04-16  2:34         ` Eli Zaretskii
2019-04-16  5:33           ` Alex Gramiak
2019-04-16 15:23             ` Eli Zaretskii
2019-04-16 15:47               ` Alex Gramiak
2019-04-16  3:42         ` Paul Eggert
2019-04-16 13:05           ` Stefan Monnier
2019-04-16 15:22             ` Paul Eggert
2019-04-16 16:10               ` Alex Gramiak
2019-04-16 17:54                 ` Paul Eggert
2019-04-16 20:50                   ` Alex Gramiak
2019-04-16 21:11                     ` Alex Gramiak
2019-04-16 21:27                     ` Stefan Monnier
2019-04-16 21:27                     ` Konstantin Kharlamov
2019-04-18  8:25                       ` Paul Eggert
2019-04-18  8:43                         ` Konstantin Kharlamov
2019-04-18 13:47                         ` Andy Moreton
2019-04-18 17:27                           ` Paul Eggert
2019-04-18 17:56                             ` Andy Moreton
2019-04-18 19:32                               ` Paul Eggert
2019-04-19 13:45                         ` Alex Gramiak
2019-04-19 13:58                           ` Konstantin Kharlamov
2019-04-19 14:45                             ` Alex Gramiak
2019-04-19 17:33                           ` Paul Eggert
2019-04-19 20:53                             ` Alex Gramiak
2019-04-20  0:05                               ` Alan Mackenzie
2019-04-20  0:42                                 ` Paul Eggert
2019-04-20 19:46                                   ` Alan Mackenzie
2019-04-20 15:29                             ` Andy Moreton
2019-04-20 15:57                               ` Paul Eggert
2019-04-20 16:03                                 ` Eli Zaretskii
2019-04-20 16:11                                   ` Paul Eggert
2019-04-20 16:18                                     ` Eli Zaretskii
2019-04-20 16:57                                       ` Paul Eggert
2019-04-20 17:22                                         ` Eli Zaretskii
2019-04-20 19:13                                           ` Paul Eggert
2019-04-20 16:28                                 ` Óscar Fuentes
2019-04-20 18:58                                   ` Paul Eggert
2019-04-20 19:35                                     ` Óscar Fuentes
2019-04-20 22:54                                       ` Paul Eggert
2020-04-15  3:14   ` John Carter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h8aykdod.fsf@gmail.com \
    --to=agrambot@gmail.com \
    --cc=eggert@cs.ucla.edu \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).