How to reduce compile time of custom RxCpp operator?

Viewed 174

I wrote my own RxCpp operator subscribe_with_latest_from as a function that composes existing operators. However, it takes a very long time to compile. This example takes almost 6 seconds to compile on my system (with Clang):

#include <rxcpp/rx.hpp>

template<typename Fn, typename... Observables>
auto subscribe_with_latest_from(Fn f, Observables... observables) {
    return [=](auto &&source) {
        return source
                .with_latest_from(
                        [=](auto &&...args) {
                            f(args...);
                            return 0; // dummy value
                        },
                        observables...
                )
                .subscribe([](auto _) {});
    };
}

int identity(int value) { return value; }

auto process(rxcpp::observable<int> source) {
    // do some operations
    return source
            .map(identity)
            .map(identity)
            .map(identity)
            .map(identity)
            .map(identity);
};

int main() {
    const rxcpp::rxsub::subject<int> s1;
    const rxcpp::rxsub::subject<int> s2;
    const rxcpp::rxsub::subject<int> s3;

    process(s1.get_observable()) |
        subscribe_with_latest_from(
                [&](int v1, int v2, int v3) {
                    // do something
                },
                process(s2.get_observable()),
                process(s3.get_observable())
        );

    s1.get_subscriber().on_next(1);
    s2.get_subscriber().on_next(2);
    s3.get_subscriber().on_next(3);
    s1.get_subscriber().on_next(11);

}

It compiles in nearly 3 seconds if I replace the usage of my operator with

process(s1.get_observable())
        .with_latest_from(
                [&](int v1, int v2, int v3) {
                    // do something
                    return 0;
                },
                process(s2.get_observable()),
                process(s3.get_observable())
        )
        .subscribe([](int _) {});

How can I reduce/eliminate the compile time overhead of my custom operator subscribe_with_latest_from?


Note: I measure compile time using

clang++ -ftime-report -I"/path/to/RxCpp/include" -std=c++14 main.cpp
0 Answers
Related