Skip to content

[CLEANUP] Don't store array length as a property #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged

Conversation

JakeQZ
Copy link
Collaborator

@JakeQZ JakeQZ commented Feb 19, 2025

Since PHP 5, if not earlier, the length of an array is internally stored by PHP and can be accessed in O(1) time.
Maintaining both the array and its length in class properties is error-prone (particularly regarding possible future code changes). When used in a loop, \count($array) is more repetitively expensive than $arrayLength,
but the latter can be set up as a local variable as and when needed.

Reference:
https://stackoverflow.com/questions/5835241/is-phps-count-function-o1-or-on-for-arrays

This change also ensures that the characters are always a (dazzling) array.

Resolves #953.

@coveralls
Copy link

coveralls commented Feb 19, 2025

Coverage Status

coverage: 51.682% (+0.03%) from 51.654%
when pulling 321fcc9 on cleanup/length
into ee3d57c on main.

@JakeQZ JakeQZ force-pushed the cleanup/length branch 3 times, most recently from 264d15c to 521415c Compare February 19, 2025 01:56
@oliverklee
Copy link
Collaborator

#958 is merged now.

@oliverklee
Copy link
Collaborator

This PR now is in need of rebasing.

@JakeQZ JakeQZ force-pushed the cleanup/length branch 3 times, most recently from d26ead4 to 1ff05e9 Compare February 20, 2025 11:29
@JakeQZ
Copy link
Collaborator Author

JakeQZ commented Feb 20, 2025

This PR now is in need of rebasing.

Done. Was tricky, and at times I wondered if restarting from scratch would be easier.

Since PHP 5, if not earlier, the length of an array is internally stored by PHP
and can be accessed in O(1) time.
Maintaining both the array and its length in class properties is error-prone
(particularly regarding possible future code changes).
When used in a loop, `\count($array)` is more repetitively expensive than
`$arrayLength`,
but the latter can be set up as a local variable as and when needed.

Reference:
https://stackoverflow.com/questions/5835241/is-phps-count-function-o1-or-on-for-arrays

This change also ensures that the characters are always a (dazzling) array.

Resolves #953.
@JakeQZ JakeQZ requested a review from oliverklee February 20, 2025 18:31
@oliverklee oliverklee merged commit 981464c into main Feb 20, 2025
21 checks passed
@oliverklee oliverklee deleted the cleanup/length branch February 20, 2025 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove ParserState::$length
3 participants