# messing with branch prediction

- Thread: https://externals.io/message/24149
- Original archive: https://news-web.php.net/php.internals/24149
- Started by Nuno Lopes on 2006-06-18T13:49:47+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #24149 — Nuno Lopes — 2006-06-18T13:49:47+00:00

URL: https://externals.io/message/24149#24149

Hello,

Yesterday I was playing with branch prediction in the Zend VM executor. 
Although I didn't have any noticeable performance improvement within the 
bench.php script, I've put the patch on-line, because someone else might 
want to mess with it too :) (but with care..)

patch: http://mega.ist.utl.pt/~ncpl/zend_branch_prediction.txt
gcc docs: 
http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005fexpect-2440

the zend.h part of the patch is based on the similar macros (with the same 
name, too) found in the Linux kernel.

I made & tested the patch in a Centrino laptop with gcc 3.4, so it might be 
the cause of getting no relevant performance increase. On CPUs with longer 
pipelines (like Pentium 4/NetBurst architecture), the performance increase 
might be noticeable.


Happy hacking :)
Nuno

---

## #24150 — Ilia Alshanetsky — 2006-06-19T02:02:34+00:00

URL: https://externals.io/message/24149#24150 · In reply to: #24149

Looks like an interesting patch, if it does not cost us any  
performance and only offers either no gain or a small gain I'd say it  
would be worth inclusion. Perhaps branch prediction will work better  
with newer versions of gcc like 4.0 or 4.1




> Hello,
>
> Yesterday I was playing with branch prediction in the Zend VM  
> executor. Although I didn't have any noticeable performance  
> improvement within the bench.php script, I've put the patch on- 
> line, because someone else might want to mess with it too :) (but  
> with care..)
>
> patch: http://mega.ist.utl.pt/~ncpl/zend_branch_prediction.txt
> gcc docs: http://gcc.gnu.org/onlinedocs/gcc/Other- 
> Builtins.html#index-g_t_005f_005fbuiltin_005fexpect-2440
>
> the zend.h part of the patch is based on the similar macros (with  
> the same name, too) found in the Linux kernel.
>
> I made & tested the patch in a Centrino laptop with gcc 3.4, so it  
> might be the cause of getting no relevant performance increase. On  
> CPUs with longer pipelines (like Pentium 4/NetBurst architecture),  
> the performance increase might be noticeable.
>
>
> Happy hacking :)
> Nuno
> -- 
> 
> 
>
>

Ilia Alshanetsky

---

## #24152 — Zeev Suraski — 2006-06-19T07:46:27+00:00

URL: https://externals.io/message/24149#24152 · In reply to: #24149

Definitely looks nice.  I actually like how it makes the code more 
readable hinting which branches are rare.

Zeev

At 16:49 18/06/2006, Nuno Lopes wrote:
>Hello,
>
>Yesterday I was playing with branch prediction in the Zend VM 
>executor. Although I didn't have any noticeable performance 
>improvement within the bench.php script, I've put the patch on-line, 
>because someone else might want to mess with it too :) (but with care..)
>
>patch: http://mega.ist.utl.pt/~ncpl/zend_branch_prediction.txt
>gcc docs: 
>http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005fexpect-2440
>
>the zend.h part of the patch is based on the similar macros (with 
>the same name, too) found in the Linux kernel.
>
>I made & tested the patch in a Centrino laptop with gcc 3.4, so it 
>might be the cause of getting no relevant performance increase. On 
>CPUs with longer pipelines (like Pentium 4/NetBurst architecture), 
>the performance increase might be noticeable.
>
>
>Happy hacking :)
>Nuno

---

## #24218 — Dmitry Stogov — 2006-06-26T11:20:44+00:00

URL: https://externals.io/message/24149#24218 · In reply to: #24149

The patch looks good.

I already did some experiments with __builtin_expect() in the past, but
didn't get any visible speedup.

As I understand __builtin_expect() helps only in case if branch is not in
BTB and static branch prediction is used.
In case if branch instruction was already executed (and didn't go out from
BTB), __builtin_expect() has no effect.
It just reorders basic-blocks according to static branch prediction
algorithm.

Thanks. Dmitry.

> -----Original Message-----
> From: Nuno Lopes [mailto:nlopess@php.net] 
> Sent: Sunday, June 18, 2006 5:50 PM
> To: PHPdev
> Cc: andi@php.net; dmitry@php.net
> Subject: messing with branch prediction
> 
> 
> Hello,
> 
> Yesterday I was playing with branch prediction in the Zend VM 
> executor. 
> Although I didn't have any noticeable performance improvement 
> within the 
> bench.php script, I've put the patch on-line, because someone 
> else might 
> want to mess with it too :) (but with care..)
> 
> patch: http://mega.ist.utl.pt/~ncpl/zend_branch_prediction.txt
> gcc docs: 
> http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-g_
> t_005f_005fbuiltin_005fexpect-2440
> 
> the zend.h part of the patch is based on the similar macros 
> (with the same 
> name, too) found in the Linux kernel.
> 
> I made & tested the patch in a Centrino laptop with gcc 3.4, 
> so it might be 
> the cause of getting no relevant performance increase. On 
> CPUs with longer 
> pipelines (like Pentium 4/NetBurst architecture), the 
> performance increase 
> might be noticeable.
> 
> 
> Happy hacking :)
> Nuno
