# Reason for ZEND_JIT_COUNTER_INIT set to 32531?

- Thread: https://externals.io/message/116878
- Original archive: https://news-web.php.net/php.internals/116878
- Started by tao.su@intel.com on 2022-01-13T10:01:40+00:00
- Messages: 6
- 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

---

## #116878 — tao.su@intel.com — 2022-01-13T10:01:40+00:00

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

Hi Internal,
I am not sure this is a correct mail list for asking source code related question.
If not, I am sorry. Appreciate you guys' help.

I am reading PHP8 JIT related source code and found that zend_jit.h has a magic definition
ZEND_JIT_COUNTER_INIT as 32531 which I do not quite understand how this number was chosen and why?
Anybody can provide some historic information?
#define ZEND_JIT_COUNTER_INIT         32531

A code example using this definition (looks like the 2nd parameter computes into a fixed number if hot_func is set by php.ini)
ZEND_OPCODE_TAIL_CALL_EX(zend_jit_trace_counter_helper,
                ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));


=======================================
Tony Su (Su, Tao)
make a 'lazy' programmer diligently with efficiency

---

## #116883 — Dmitry Stogov — 2022-01-17T05:36:52+00:00

URL: https://externals.io/message/116878#116883 · In reply to: #116878

This it's my choice.
This number is common for all JIT counters (hot_loop, hot_func, hot_return).
It's value was selected to provide the best precision when different
counters have different thresholds.
Unfortunately, I can't remember the exact equations or logic I used at that
time.

Thanks. Dmitry.



> Hi Internal,
> I am not sure this is a correct mail list for asking source code related
> question.
> If not, I am sorry. Appreciate you guys' help.
>
> I am reading PHP8 JIT related source code and found that zend_jit.h has a
> magic definition
> ZEND_JIT_COUNTER_INIT as 32531 which I do not quite understand how this
> number was chosen and why?
> Anybody can provide some historic information?
> #define ZEND_JIT_COUNTER_INIT         32531
>
> A code example using this definition (looks like the 2nd parameter
> computes into a fixed number if hot_func is set by php.ini)
> ZEND_OPCODE_TAIL_CALL_EX(zend_jit_trace_counter_helper,
>                 ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) /
> JIT_G(hot_func)));
>
>
> =======================================
> Tony Su (Su, Tao)
> make a 'lazy' programmer diligently with efficiency

---

## #116884 — tao.su@intel.com — 2022-01-17T07:49:36+00:00

URL: https://externals.io/message/116878#116884 · In reply to: #116883

Thanks Dmitry for the information.

With a few debug sessions, I found that 32531 is working well 
for any thresholds, and is also a big prime number :-).


-----Original Message-----
From: Dmitry Stogov <dmitrystogov@gmail.com> 
Sent: Monday, January 17, 2022 1:37 PM
To: Su, Tao <tao.su@intel.com>
Cc: PHP internals <internals@lists.php.net>
Subject: Re: [PHP-DEV] Reason for ZEND_JIT_COUNTER_INIT set to 32531?

This it's my choice.
This number is common for all JIT counters (hot_loop, hot_func, hot_return).
It's value was selected to provide the best precision when different counters have different thresholds.
Unfortunately, I can't remember the exact equations or logic I used at that time.

Thanks. Dmitry.



> Hi Internal,
> I am not sure this is a correct mail list for asking source code 
> related question.
> If not, I am sorry. Appreciate you guys' help.
>
> I am reading PHP8 JIT related source code and found that zend_jit.h 
> has a magic definition ZEND_JIT_COUNTER_INIT as 32531 which I do not 
> quite understand how this number was chosen and why?
> Anybody can provide some historic information?
> #define ZEND_JIT_COUNTER_INIT         32531
>
> A code example using this definition (looks like the 2nd parameter 
> computes into a fixed number if hot_func is set by php.ini) 
> ZEND_OPCODE_TAIL_CALL_EX(zend_jit_trace_counter_helper,
>                 ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / 
> JIT_G(hot_func)));
>
>
> =======================================
> Tony Su (Su, Tao)
> make a 'lazy' programmer diligently with efficiency

---

## #116885 — Dmitry Stogov — 2022-01-17T12:12:19+00:00

URL: https://externals.io/message/116878#116885 · In reply to: #116884

Oh, probably this is just the biggest prime number that fits into a signed
16-bit number :)



> Thanks Dmitry for the information.
>
> With a few debug sessions, I found that 32531 is working well
> for any thresholds, and is also a big prime number :-).
>
>
> -----Original Message-----
> From: Dmitry Stogov <dmitrystogov@gmail.com>
> Sent: Monday, January 17, 2022 1:37 PM
> To: Su, Tao <tao.su@intel.com>
> Cc: PHP internals <internals@lists.php.net>
> Subject: Re: [PHP-DEV] Reason for ZEND_JIT_COUNTER_INIT set to 32531?
>
> This it's my choice.
> This number is common for all JIT counters (hot_loop, hot_func,
> hot_return).
> It's value was selected to provide the best precision when different
> counters have different thresholds.
> Unfortunately, I can't remember the exact equations or logic I used at
> that time.
>
> Thanks. Dmitry.
>
> 
>
> > […]

---

## #116887 — Larry Garfield — 2022-01-17T17:13:45+00:00

URL: https://externals.io/message/116878#116887 · In reply to: #116885

> Oh, probably this is just the biggest prime number that fits into a signed
> 16-bit number :)

Seems like the sort of thing that should be documented inline.  (Also, why it needs to be a prime number.)

--Larry Garfield

> 
>
> > […]

---

## #116889 — tao.su@intel.com — 2022-01-18T01:54:31+00:00

URL: https://externals.io/message/116878#116889 · In reply to: #116885

Interesting! I would like to dig deeper or ask google for more information.
I had an impression that I once read a paper about this kind of magic number, but not sure where it was from.

@Dmitry Stogov<mailto:dmitrystogov@gmail.com> With your permission, if I found anything, I would like to add a few comments into the source code file for records.

From: Dmitry Stogov <dmitrystogov@gmail.com>
Sent: Monday, January 17, 2022 8:12 PM
To: Su, Tao <tao.su@intel.com>
Cc: PHP internals <internals@lists.php.net>
Subject: Re: [PHP-DEV] Reason for ZEND_JIT_COUNTER_INIT set to 32531?

Oh, probably this is just the biggest prime number that fits into a signed 16-bit number :)


Thanks Dmitry for the information.

With a few debug sessions, I found that 32531 is working well
for any thresholds, and is also a big prime number :-).


-----Original Message-----
From: Dmitry Stogov <dmitrystogov@gmail.com<mailto:dmitrystogov@gmail.com>>
Sent: Monday, January 17, 2022 1:37 PM
To: Su, Tao <tao.su@intel.com<mailto:tao.su@intel.com>>
Cc: PHP internals <internals@lists.php.net<mailto:internals@lists.php.net>>
Subject: Re: [PHP-DEV] Reason for ZEND_JIT_COUNTER_INIT set to 32531?

This it's my choice.
This number is common for all JIT counters (hot_loop, hot_func, hot_return).
It's value was selected to provide the best precision when different counters have different thresholds.
Unfortunately, I can't remember the exact equations or logic I used at that time.

Thanks. Dmitry.



> Hi Internal,
> I am not sure this is a correct mail list for asking source code
> related question.
> If not, I am sorry. Appreciate you guys' help.
>
> I am reading PHP8 JIT related source code and found that zend_jit.h
> has a magic definition ZEND_JIT_COUNTER_INIT as 32531 which I do not
> quite understand how this number was chosen and why?
> Anybody can provide some historic information?
> #define ZEND_JIT_COUNTER_INIT         32531
>
> A code example using this definition (looks like the 2nd parameter
> computes into a fixed number if hot_func is set by php.ini)
> ZEND_OPCODE_TAIL_CALL_EX(zend_jit_trace_counter_helper,
>                 ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) /
> JIT_G(hot_func)));
>
>
> =======================================
> Tony Su (Su, Tao)
> make a 'lazy' programmer diligently with efficiency
