[PHP] Use IDL namer in generator and add prefixed size, clear, and createBytesVector in builder#8202
Closed
razvanalex wants to merge 7 commits intogoogle:masterfrom
Closed
[PHP] Use IDL namer in generator and add prefixed size, clear, and createBytesVector in builder#8202razvanalex wants to merge 7 commits intogoogle:masterfrom
razvanalex wants to merge 7 commits intogoogle:masterfrom
Conversation
Some languages (such as PHP) are more restrictive and don't allow the use of keywords, despite their casing in variables, functions, classes, etc. names. This commit adds the keywords_case_sensitive config for IDL namer. By default it is set to true, as of the languages that use the IDL namer don't require this check.
Some languages use the IDL interface to generate the method/varible/etc names. This commit adds the IDL namer to php, basically continuing the workf from PR google#7491. Now, each method uses namer_.Method, while the legacy stuff is kept for backwards compatibility. At the moment, PHP is the only language that uses the case sensitive check when generating the names.
Clear and createBytesVector are implemented in other languages, and this commit brings these functions into php. Clear method just resets the internal buffer state while createBytesVector is a variation on createString that allows non-UTF8 data. Also, performance was improved by using substr_replace instead of a for-loop.
Applies the TODO about when expanding the byte buffer and it is needed to copy the content into the new buffer. substr_replace seems to be much faster, especially when considering longer strings. Below are some benchmarks: Before: ``` txt BenchmarkCreateString/1 0.0019645690917969 ms BenchmarkCreateString/10 0.0022602081298828 ms BenchmarkCreateString/100 0.0092792510986328 ms BenchmarkCreateString/1000 0.068621635437012 ms BenchmarkCreateString/10000 0.77774047851562 ms BenchmarkCreateString/100000 7.1578598022461 ms BenchmarkCreateString/1000000 66.291980743408 ms BenchmarkCreateString/10000000 789.96389865875 ms ``` After: ``` txt BenchmarkCreateString/1 0.0019216537475586 ms BenchmarkCreateString/10 0.0020408630371094 ms BenchmarkCreateString/100 0.0075387954711914 ms BenchmarkCreateString/1000 0.053601264953613 ms BenchmarkCreateString/10000 0.52011966705322 ms BenchmarkCreateString/100000 4.9904012680054 ms BenchmarkCreateString/1000000 50.608458518982 ms BenchmarkCreateString/10000000 506.61317825317 ms ```
On Windows, the compiler generates a warning which is treated as error: D:\a\flatbuffers\flatbuffers\src\namer.h(216,28): error C2220: the following warning is treated as an error [D:\a\flatbuffers\flatbuffers\flatc.vcxproj] D:\a\flatbuffers\flatbuffers\src\namer.h(216,28): warning C4244: '=': conversion from 'int' to 'char', possible loss of data [D:\a\flatbuffers\flatbuffers\flatc.vcxproj] Enforcing char should silence the warning.
This fixes the name generation for methods for start, create, finish, end, and identifier, to use the old naming conventions, instead of lower camel. Also, it considers the escaped keywords. For example, `string` would yield to `String_::startString_()`, `String_::getRootAsString_()`, or `String_::finishString_Buffer()`.
Contributor
|
This pull request is stale because it has been open 6 months with no activity. Please comment or label |
razvanalex
pushed a commit
to razvanalex/flatbuffers
that referenced
this pull request
Oct 7, 2024
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
Contributor
Author
|
Will close, as I will split this PR in smaller ones. |
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Oct 8, 2024
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Jul 6, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Jul 6, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Nov 27, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Nov 27, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Dec 10, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Dec 10, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
razvanalex
added a commit
to razvanalex/flatbuffers
that referenced
this pull request
Dec 21, 2025
This refactors the code from google#8202, specifically the part related to using the NamerPhp.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds some missing functionality and improved performance to PHP flatbuffers:
During internal testing, I noticed that tables using PHP keywords as names (e.g., bytes, string) would yield errors in the generated code because keywords cannot be used as class names despite their casing (also mentioned in PHP docs). Therefore, I added a check (see commit a58f40c) when escaping keywords, which generally is disabled, except for IDL PHP generator.
Also, when I replaced for-loops with substr_replace in createString and growByteBuffer, the performance improved, especially for longer strings. Below is a benchmark (here is the code).
Before the change:
After the change: