From e777b054d583aaf2ed445a263208c6ef21bc05ac Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 8 Jul 2022 08:34:45 +0100 Subject: [PATCH] Performance optimisation for parameters passed by value that are C++11 movable. The C++ wrappers create a temporary variable for a parameter to be passed to a function. This is initially default constructed and then copy assigned from the instance being passed in from the target language. This is unchanged, however, when the temporary variable is passed to wrapped function, it is now done using std::move. If the type is move constructible, the move constructor will be used instead of the copy constructor. Note that the implementation calls std::move for all user-defined types (non-primitive types passed by value), this excludes anything passed by pointer, reference and arrays. It does also include any type that has not been defined/parsed by SWIG, that is, unknown types. std::move is called via the SWIG_STD_MOVE macro which only calls std::move for C++11 and later code. --- CHANGES.current | 11 ++++++++++- Examples/test-suite/csharp/cpp11_move_only_runme.cs | 4 ++-- Examples/test-suite/java/cpp11_move_only_runme.java | 4 ++-- Examples/test-suite/python/cpp11_move_only_runme.py | 4 ++-- Source/Swig/cwrap.c | 10 +++++++--- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 9adfa8ad0..d00f53bc2 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,8 +7,17 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-07-08: wsfulton + Performance optimisation for parameters passed by value that are C++11 movable. + The C++ wrappers create a temporary variable for a parameter to be passed to a + function. This is initially default constructed and then copy assigned from the + instance being passed in from the target language. This is unchanged, however, + when the temporary variable is passed to wrapped function, it is now done using + std::move. If the type is move constructible, the move constructor will be used + instead of the copy constructor. + 2022-07-03: wsfulton - Performane optimisation for directors for classes passed by value. The directorin + Performance optimisation for directors for classes passed by value. The directorin typemaps in the director methods now use std::move on the input parameter when copying the object from the stack to the heap prior to the callback into the target language, thereby taking advantage of move semantics if available. diff --git a/Examples/test-suite/csharp/cpp11_move_only_runme.cs b/Examples/test-suite/csharp/cpp11_move_only_runme.cs index 4bd291109..f2d1c4cb5 100644 --- a/Examples/test-suite/csharp/cpp11_move_only_runme.cs +++ b/Examples/test-suite/csharp/cpp11_move_only_runme.cs @@ -27,8 +27,8 @@ public class cpp11_move_only_runme { using (MovableCopyable mo = new MovableCopyable(222)) { Counter.check_counts(1, 0, 0, 0, 0, 0); MovableCopyable.take(mo); - Counter.check_counts(2, 1, 1, 0, 0, 2); + Counter.check_counts(2, 0, 1, 1, 0, 2); } - Counter.check_counts(2, 1, 1, 0, 0, 3); + Counter.check_counts(2, 0, 1, 1, 0, 3); } } diff --git a/Examples/test-suite/java/cpp11_move_only_runme.java b/Examples/test-suite/java/cpp11_move_only_runme.java index e1d7cf180..8f0f2acef 100644 --- a/Examples/test-suite/java/cpp11_move_only_runme.java +++ b/Examples/test-suite/java/cpp11_move_only_runme.java @@ -43,9 +43,9 @@ public class cpp11_move_only_runme { MovableCopyable mo = new MovableCopyable(222); Counter.check_counts(1, 0, 0, 0, 0, 0); MovableCopyable.take(mo); - Counter.check_counts(2, 1, 1, 0, 0, 2); + Counter.check_counts(2, 0, 1, 1, 0, 2); mo.delete(); - Counter.check_counts(2, 1, 1, 0, 0, 3); + Counter.check_counts(2, 0, 1, 1, 0, 3); } } } diff --git a/Examples/test-suite/python/cpp11_move_only_runme.py b/Examples/test-suite/python/cpp11_move_only_runme.py index 1e1defcdc..9727f3fcc 100644 --- a/Examples/test-suite/python/cpp11_move_only_runme.py +++ b/Examples/test-suite/python/cpp11_move_only_runme.py @@ -22,6 +22,6 @@ Counter.reset_counts() mo = MovableCopyable(222) Counter.check_counts(1, 0, 0, 0, 0, 0) MovableCopyable.take(mo) -Counter.check_counts(2, 1, 1, 0, 0, 2) +Counter.check_counts(2, 0, 1, 1, 0, 2) del mo -Counter.check_counts(2, 1, 1, 0, 0, 3) +Counter.check_counts(2, 0, 1, 1, 0, 3) diff --git a/Source/Swig/cwrap.c b/Source/Swig/cwrap.c index ebe9fa702..36e69332c 100644 --- a/Source/Swig/cwrap.c +++ b/Source/Swig/cwrap.c @@ -427,10 +427,14 @@ String *Swig_cfunction_call(const_String_or_char_ptr name, ParmList *parms) { String *rcaststr = SwigType_rcaststr(rpt, pname); if (comma) { - Printv(func, ",", rcaststr, NIL); - } else { - Append(func, rcaststr); + Append(func, ","); } + + if (cparse_cplusplus && SwigType_type(rpt) == T_USER) + Printv(func, "SWIG_STD_MOVE(", rcaststr, ")", NIL); + else + Printv(func, rcaststr, NIL); + Delete(rpt); Delete(pname); Delete(rcaststr);