Skip to content

Commit

Permalink
nullable properties: minor improvement
Browse files Browse the repository at this point in the history
This commit adds a small refinement to revision 11a9b3c, which
introduced a special ``is_getter`` flag causing the ``nb_func``
constructor to ignore function argument annotations when used
to create the "getter" part of a property binding, e.g.:

```
nb::class_<T>(..)
 .def_rw("value", &T::value, nb::arg().none())
```

A bit of context: Since ``.def_rw()`` internally creates a getter and a
setter function and forwards all annotations to them, there used to be a
mismatch in the function argument counts for the getter, causing a
compile-time static assertion failure. This was fixed in 11a9b3c
which simply disables this assertion.

However, ``nb_func_new`` was still invoked with the
``func_flags::has_args`` flag active, causing it to choose the "complex"
function dispatcher that is able to handle keyword arguments. This
causes unnecessary overheads, hence this commit disables that flag.
  • Loading branch information
wjakob committed Nov 1, 2023
1 parent 4a21c38 commit 45d9415
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/nanobind/nb_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ NB_INLINE PyObject *func_create(Func &&func, Return (*)(Args...),
func_data_prelim<nargs_provided> f;
f.flags = (args_pos_1 < nargs ? (uint32_t) func_flags::has_var_args : 0) |
(kwargs_pos_1 < nargs ? (uint32_t) func_flags::has_var_kwargs : 0) |
(nargs_provided ? (uint32_t) func_flags::has_args : 0) |
(ReturnRef ? (uint32_t) func_flags::return_ref : 0);
(ReturnRef ? (uint32_t) func_flags::return_ref : 0) |
(nargs_provided &&
!is_getter_det ? (uint32_t) func_flags::has_args : 0);

/* Store captured function inside 'func_data_prelim' if there is space. Issues
with aliasing are resolved via separate compilation of libnanobind. */
Expand Down

0 comments on commit 45d9415

Please sign in to comment.