Skip to content

Inline functions instead of macros #6030

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

Closed
wants to merge 10 commits into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Aug 21, 2020

This PR converts various macros in inline functions.

Notable omissions of this pass:

  • Zend Smart String (the one using char*)
  • Zend Virtual CWD
  • Zend/zend_string.h
  • Zend/zend_compile.h
  • Zend/zend_types.h

@twose
Copy link
Member

twose commented Aug 25, 2020

Nice job~
Actually I did similar work before, but I don’t have time to finish it...😢

Although we upgraded to C99, can we make the code style consistent? I really hope that the code style has a unified standard.

I personally hope to continue to use the original coding way, zend_bool, zend_always_inline... and always keep curly braces of function definitions on separate lines...

Zend/zend.c Outdated
@@ -391,6 +391,8 @@ ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
}
/* }}} */

extern ZEND_API inline size_t zend_print_variable(zval *var);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No use of extern inline in php-src please.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must say I'm rather confused as to what is the difference between extern inline and static inline is, I used static inlines initially but when I talked to @morrisonlevi in R11 he preferred extern inline as apparently, IIRC, the semantics differ for static inline depending on the compiler. But I'm having a hard time finding relevant information about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @morrisonlevi has opinions on this topic, but in php-src we do not use extern inline. It should be static inline or static zend_always_inline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I will change them accordingly then.

Copy link
Contributor

@morrisonlevi morrisonlevi Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it is worth, it's not my opinion; it's actually the C99 and newer standard. The standards aren't freely available so it's hard to link, but here's GNU's explanation (with weird macros, but hopefully you can see through them): https://www.gnu.org/software/gnulib/manual/html_node/extern-inline.html. Here's a Stack Overflow answer as well, though I don't know about the C++ part: https://stackoverflow.com/a/216546.

Of course, compilers support static inline functions whether they are still standard or not, so I do acknowledge it is somewhat pedantic. The main reason I care is that you cannot use a static inline function inside of a plain inline function, which makes this non-standard use proliferate into my own codebase.

Now, for the opinion part: in my opinion, all usages of static inline should be converted into inline + extern inline and all usages of static zend_always_inline should remain as-is. In my opinion this strikes a nice balance between standard compliance and pragmatism.

For what it is worth, here are some rough stats on usage:

$ grep -Ir "static zend_always_inline" Zend | wc -l
416
$ grep -Ir "static inline" Zend | wc -l
83

@Girgias Girgias force-pushed the inline-functions-instead-of-macros branch from e85289f to 97e7f04 Compare September 8, 2020 12:48
@Girgias Girgias force-pushed the inline-functions-instead-of-macros branch from 97e7f04 to 39770bc Compare September 8, 2020 13:02
@Girgias Girgias force-pushed the inline-functions-instead-of-macros branch from 39770bc to 4b849f3 Compare September 8, 2020 13:10
@@ -269,6 +270,7 @@ ZEND_API void free_estring(char **str_p);
END_EXTERN_C()

/* output support */
// TODO Convert to inline functions?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything spelled UPPERCASE should stay a macro.

@nikic
Copy link
Member

nikic commented Sep 8, 2020

TBH I'm not completely clear on what advantage we get from doing this, for these kind of "trivial" macros, that just forward to some underlying implementation.

Does this have something to do with C++ code and namespacing?

@github-actions
Copy link

github-actions bot commented Feb 9, 2022

There has not been any recent activity in this PR. It will automatically be closed in 7 days if no further action is taken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants