Skip to content

Chi check speedup - #177

Open
Nic-Chr wants to merge 15 commits into
masterfrom
chi_check_speedup
Open

Chi check speedup#177
Nic-Chr wants to merge 15 commits into
masterfrom
chi_check_speedup

Conversation

@Nic-Chr

@Nic-Chr Nic-Chr commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Currently a work-in-progress.

This PR introduces a C++ re-write of both chi_check() and dob_from_chi(), using the C++20 API R package cppally.

@Nic-Chr

Nic-Chr commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Related issue: #176

@alan-y alan-y linked an issue Jul 9, 2026 that may be closed by this pull request
@Moohan
Moohan changed the base branch from master to feature/mod10 July 9, 2026 13:40
@Moohan
Moohan changed the base branch from feature/mod10 to master July 9, 2026 13:41

@Moohan Moohan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Without really reading the C++ or knowing what the conventions are, I would say src/chi_check.cpp should be split into a few scripts, especially splitting the cpp_chi_check definition from the cpp_dob_from_chi definition.

Comment thread DESCRIPTION
LazyData: true
LinkingTo:
cppally
SystemRequirements: C++20

@Moohan Moohan Jul 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As far as I can tell C++20 became the default in 4.6.0 https://cran.r-project.org/doc/manuals/r-release/NEWS.html

What am I missing / what version of R would we need to allow as the minimum, ideally this would not be greater than the current PHS minimum of 4.4

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a good point and it doesn't look like there's a clear answer. The closest answer I can see is that C++20 can be specified from R 4.5.0 and (R 4.5.3 on Windows), according to Writing R Extensions, so that's probably the minimum reliable version unfortunately.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

{cpally} needs R 4.5, so that's also a floor for the dependency.

I think when PHS does the next upgrade, we'll still be keeping 4.4, so we really need to support that. Which means we either need a speed-up that works with at least 4.4, or cpally needs to be an optional dep and we have code that gates on the R version (and whether cpally is installed); we could remove the gating and 'upgrade' the dependency in the future when PHS stops supporting / providing the older R versions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Happy to go either way really.

cppally doesn't need to be installed thankfully - LinkingTo (cppally >= 1.1.0) just makes the header files of the package available.

We could gate on whether C++20 is available and if cppally headers are available, like:

#if __cplusplus >= 202002L && defined(CPPALLY_HPP)
#include <cppally.hpp>
// C++ 20 code
#else
# // C++ 17 code as it's the default anyway for R 4.4.0
#endif

Comment thread DESCRIPTION Outdated
Language: en-GB
LazyData: true
LinkingTo:
cppally

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think if we're introducing a C++ dependency I'd prefer it to be something more mainstream like {cpp11} unless there's a really solid reason not to, and in this case since the functions work fine in pure R, I'm not sure it would be worth it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It can be done but I believe it require a lot more code to achieve the same results for the following reasons.

As far as I know, cpp11 doesn't have a native r_date class, which is doing a lot of heavy-lifting of constructing dates from the DDMMYY part of chi numbers, thanks to the chrono library, which itself is a standard feature of C++20, so no extra dependencies to think about on that front.

A smaller but helpful feature is pmap() which helps reduce a lot of manual for-loops while also handling efficient recycling of elements.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In terms of being worth it, I would argue that based on my benchmarks, it seems to offer a 20x improvement in speed, which is a clear benefit to anyone running chi checks on large numbers of chis.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speed up CHI functions

2 participants