# [PATCH] string_offset access optimization

- Thread: https://externals.io/message/49078
- Original archive: https://news-web.php.net/php.internals/49078
- Started by Dmitry Stogov on 2010-07-15T09:10:14+00:00
- Messages: 5
- 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

---

## #49078 — Dmitry Stogov — 2010-07-15T09:10:14+00:00

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

Hi,

Recently I noticed that reading of string offset is performed in two 
steps. At first special string_offset variant of temporary_variable is 
created in zend_fetch_dimension_address_read() and then the real string 
value is created in _get_zval_ptr_var_string_offset().

I think we can create the real string in the first place. This makes 50% 
speed-up on string offset reading operation and allows to eliminate some 
checks and conditional brunches in VM.

The patch is attached (don't forget to regenerate zend_vm_execute.h to 
test it). However it changes behavior in one bogus case.
The following code now will emit "b" (currently it generates a fatal 
error - cannot use string offset as an array).

$str = "abs";
var_dumo($str[1][0]);

I think it's not a problem at all.
"b" makes sense because "abs"[1] -> "b" and "b"[0] -> "b".

I'm going to commit the patch in case of no objections.

Thanks. Dmitry.

---

## #49082 — Felipe Pena — 2010-07-15T22:18:34+00:00

URL: https://externals.io/message/49078#49082 · In reply to: #49078

Hi Dmitry,

2010/7/15 Dmitry Stogov <dmitry@zend.com>

> Hi,
>
> Recently I noticed that reading of string offset is performed in two steps.
> At first special string_offset variant of temporary_variable is created in
> zend_fetch_dimension_address_read() and then the real string value is
> created in _get_zval_ptr_var_string_offset().
>
> I think we can create the real string in the first place. This makes 50%
> speed-up on string offset reading operation and allows to eliminate some
> checks and conditional brunches in VM.
>
> The patch is attached (don't forget to regenerate zend_vm_execute.h to test
> it). However it changes behavior in one bogus case.
> The following code now will emit "b" (currently it generates a fatal error
> - cannot use string offset as an array).
>
> $str = "abs";
> var_dumo($str[1][0]);
>
> I think it's not a problem at all.
> "b" makes sense because "abs"[1] -> "b" and "b"[0] -> "b".
>
> I'm going to commit the patch in case of no objections.
>
> Thanks. Dmitry.
>


> The patch looks good, I did some tests, not found anything strange.
Good simplyfication! :)

-- 
Regards,
Felipe Pena

---

## #49085 — Stas Malyshev — 2010-07-16T07:47:34+00:00

URL: https://externals.io/message/49078#49085 · In reply to: #49078

Hi!

> $str = "abs";
> var_dumo($str[1][0]);
>
> I think it's not a problem at all.
> "b" makes sense because "abs"[1] ->  "b" and "b"[0] ->  "b".

Totally makes sense, but it'd be a bit strange that $str[1][0] works but 
$str[1][0] = "a" does not.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #49087 — Ferenc Kovacs — 2010-07-16T07:58:36+00:00

URL: https://externals.io/message/49078#49087 · In reply to: #49085

> Hi!
>
> > […]
>
> Totally makes sense, but it'd be a bit strange that $str[1][0] works but
> $str[1][0] = "a" does not.
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>
> --
> 
> 
>
>

Isn't this the same problem as the below example of the type juggling?

"" == 0 // true
"0" == 0 // true
"0" == "" // false

Tyrael

---

## #49088 — Dmitry Stogov — 2010-07-16T08:07:19+00:00

URL: https://externals.io/message/49078#49088 · In reply to: #49085

Hi Stas,


> Hi!
>
> > […]
>
> Totally makes sense, but it'd be a bit strange that $str[1][0] works but
> $str[1][0] = "a" does not.

$str[1][0] = "a"; doesn't have to modify $str.

var_dump($str[1][1]) will return empty string

$str[1][1] = "a"; can't modify $str at all.

So I think it is not a problem.

Thanks. Dmitry.
