X Tutup
The Wayback Machine - https://web.archive.org/web/20220405131116/https://github.com/nodejs/node-addon-api/pull/127
Skip to content
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

Rewrite constexpr with template metaprogram #127

Closed
wants to merge 1 commit into from

Conversation

Copy link

@kkoopa kkoopa commented Sep 4, 2017

Get rid of constexpr

Copy link
Contributor

@kfarnung kfarnung left a comment

LGTM assuming you tested this on VS2013.

@kkoopa
Copy link
Author

@kkoopa kkoopa commented Sep 4, 2017

I have not tested it on VS2013. I do not have it installed, but I do not see why it would not work there.

@kfarnung
Copy link
Contributor

@kfarnung kfarnung commented Sep 4, 2017

Ok, let's not worry about it then. IIRC node dropped support for building extensions using VS2013 anyway.

@kkoopa kkoopa changed the title Rewrite constexpr with template metapgrogram Rewrite constexpr with template metaprogram Sep 4, 2017
@mhdawson
Copy link
Member

@mhdawson mhdawson commented Sep 28, 2018

CI run on master: https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api/

hmm seems to fail I think it needs to be rebased to be able to validate. @kfarnung do you think this is still something we should pull in (it has been a long time).

@kfarnung
Copy link
Contributor

@kfarnung kfarnung commented Sep 28, 2018

I guess my question is what benefit we get from switching away from constexpr. I'm not a compiler expert, but presumably there's some compatibility or other benefit from this change. @kkoopa can you elaborate here?

I'm not opposed to this change assuming we can get the code rebased and passing the CI.

@kkoopa
Copy link
Author

@kkoopa kkoopa commented Sep 28, 2018

@DaAitch
Copy link
Contributor

@DaAitch DaAitch commented Dec 28, 2018

I guess my question is what benefit we get from switching away from constexpr. I'm not a compiler expert, but presumably there's some compatibility or other benefit from this change. @kkoopa can you elaborate here?

I'm not opposed to this change assuming we can get the code rebased and passing the CI.

I think it's a win-win situation:

  1. the biggest benefit is that for a wrong T it simply does not compile
  2. old compilers (before C++11) may not support constexpr, so a check is needed.

Old implementation will always compile and evaluate at runtime:

  • using constexpr: may inline at compile time that TypedArrayTypeForPrimitiveType<T>() is substituted with a e.g. value of napi_uint16_array
  • not using constexpr: may also inline at compile time, since an expression like return false ? napi_int8_array : false ? napi_uint8_array : true ? napi_int16_array ... will be optimized

thumbs up for resolving merge conflicts 😄

@kkoopa
Copy link
Author

@kkoopa kkoopa commented Dec 28, 2018

I looked at the code and can clarify my previous statement: It was for getting rid of the check for NAPI_HAS_CONSTEXPR which could lead to runtime evaluation instead of compile-time evaluation if not set, which was related to some version of Visual Studio that did not support constexpr properly.

@DaAitch
Copy link
Contributor

@DaAitch DaAitch commented Dec 28, 2018

@kkoopa I think you only have to resolve the conflicts so that it's reviewed again 👍

Copy link
Contributor

@DaAitch DaAitch left a comment

Isn't this pretty much overlapping with #128 ? Landing both wouldn't make sense at all? I think this one is easier to understand and has all advantages:

  • compile-time error
  • no additional template arg

static const napi_typedarray_type value = napi_##TYPE##_array; \
}

X(int8);
Copy link
Contributor

@DaAitch DaAitch Jan 17, 2019

Choose a reason for hiding this comment

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

For me X macro is doing to much concat magic. From L1565 you have to impl the types not matching the X macro. Wouldn't it be easier to write more, but without magic?

#define XX(TYPE, NAPI_TYPEDARRAY_TYPE)                                                                \
  template <> struct TypedArray::type_translator<TYPE> {                   \
    static const napi_typedarray_type value = NAPI_TYPEDARRAY_TYPE;             \
  }

XX(int8_t, napi_int8_array);
// ...
XX(float, napi_float32_array);

Copy link
Author

@kkoopa kkoopa Jan 19, 2019

Choose a reason for hiding this comment

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

Yes, that would be better since there are several special cases now.

@kkoopa
Copy link
Author

@kkoopa kkoopa commented Jan 19, 2019

Isn't this pretty much overlapping with #128 ?

I do not remember, but they are probably not overlapping, as I would not have made #128 otherwise.

Base automatically changed from master to main Jan 26, 2021
@github-actions github-actions bot added the stale label Dec 21, 2021
@github-actions github-actions bot closed this Jan 20, 2022
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.

None yet

4 participants
X Tutup