Dear Statalisters,
I've released a package called ctools. ctools is a suite of drop-in replacements for a bunch of existing Stata commands (both built-in and user-written), but in general these commands are much, much faster than the programs they replace. The syntax for ctools mostly matches the commands they replace, and the name usually just gets a "c" in front: -cimport-, -cmerge-, -csort-, -creghdfe-, etc. In a handful of cases these commands offer some extended functionality, or combine the functionality of multiple commands. For instance, -cbinscatter- can use the covariate adjustment approach from Cattaneo et al.'s -binsreg- instead of residualizing x and y first, and it also has some stuff from my own -binscatter2-. -cinterpolate- does both linear interpolation like -ipolate- and cubic interpolation like Nick Cox's -cipolate- (ok, this one was hard to replace), etc.
I am maintaining ctools on GitHub, and the repo has a variety of documentation (and of course the source code). The GitHub repository is here: https://www.github.com/mdroste/stata-ctools This repository has actually quietly been public for almost a year, but I don't care much for advertising my work and only a few people noticed. So we can call this Statalist post the 'public' release, although technically it's been available for most of this year and in fact has been on ssc for a month or so. I waited until I felt quite confident that the existing commands are fairly well-optimized, full-featured, and behave essentially identically to the commands they are based on.
ctools works with Stata 14.1 or later on Windows (10 or later), Mac (Apple Silicon or Intel, macOS 11 or later), and x86-64 Linux.
The latest version of ctools will always be available on my GitHub (above), and will be intermittently updated on SSC (I don't want to pester Kit too much every time something gets tweaked, this guy already does enough for this community!)
Install from my GitHub (latest version):
Install from SSC (a little behind latest):
At the end of 2026 it should not be surprising, but almost all of the code for this project was written by AI. I mostly ran prompts on my phone at 11PM while watching The Sopranos and such (example prompts: (1) "please claude just make it run faster!!!" (2) "keep going i believe in you!!"). 98% of the total time was spent extensively building out validation, test harnesses, bug searches, etcetera. What makes this "work" is that whatever command you want to speed up can serve as an "oracle", and at this point coding agents/harnesses are quite good at reverse engineering the functionality (even if they don't have access to the source code, like some of the native Stata commands) of a program and porting the key bits to C. At this point I am reasonably confident that all these commands are about as fast as they could theoretically be and also free of bugs in nearly all use cases.
It is a little hard to tell you exactly how much faster ctools will be than the commands they replace, because this depends a lot on your data and your machine (primarily your CPU and memory bandwidth). As a rough rule of thumb, commands which require moving more of your dataset will be relatively less fast, and commands which involve very high observation counts (e.g. tens of millions or more) will be relatively more fast. Regressions (in my experience) often don't involve more than a dozen or so variables, so they hold up well. You can see the rough speedups on the GitHub, but broadly speaking most of these commands will run something like 2 to 50 times faster than the programs they replace (sometimes more).
Here are some of the included ctools commands and what they replace:
The README on GitHub offers some approximate speedups that I see on my machines. Your mileage will vary depending on your processor, memory, and the data you're working with. For instance, csort and cmerge are both unusual commands in that they require your entire dataset (the active frame, in post-Stata 16 terms) to be sent from Stata to the C plugin and then back again, rather than just a couple of variables. In contrast, when you run -creghdfe y x, absorb(f1 f2)-, only the variables y, x, f1, and f2 need to be transported. This data transport overhead turns out to be quite important. -csort- and -cmerge- will both perform best when your dataset is very 'long' and not very 'wide', and can perform arbitrarily poorly when you fix the number of obs and add more variables to your dataset. So it's hard to report even approximate speedups here, because it very much depends on your type of use case. For datasets with, say, tens of millions of obs and only 1-3 dozen variables, -csort- and -cmerge- are much faster on my machine. However, if you're working with some incredibly detailed survey data or something with, say, 1 million observations and 200 variables, -csort- and -cmerge- will likely be slower. (However, in this case you probably don't care about speed at all, because -sort- and -merge- will already run very quickly on such a dataset.)
In the spirit of full disclosure, I am writing Statalist post today specifically because I just saw a new NBER working paper that was written to advertise a very similar, similarly vibecoded Stata command with a compiled plugin backend maintained by Johannes Schmieder (in this case just for -reghdfe- and -ppmlhdfe-). I am quite sure Johannes wasn't aware of my little project, which isn't his fault because I don't tend to promote my Stata projects online or in working papers, but I did want to get it out there in case this is of interest to the broader community and because it's been around for almost a year at this point and it feels ready to share. (And FWIW, since it is directly relevant to Johannes' NBER WP, on my machines -creghdfe- is about 1.5x to 2x faster than -fereg- on a variety of datasets). And although this is vibecoded, I've taken quite a bit of care to ensure it's extensible and the documentation mostly human readable everywhere. So, if you notice, you know, documentation that looks like a robot wrote it or whatever, just let me know (you can open a github issue or email me or whatever) and I'll get it fixed.
If you write your own C plugins, this is all open source and MIT-licensed, so you can do pretty much whatever you like with it. A lot of effort was spent on finding the most efficient possible way to use the Stata plugin interface to move data between Stata's memory and the C plugin (most of that code is in src/ctools_transport.c and src/ctools_data_io.c). I'd encourage any program-writers not to reinvent the wheel and to take some of this code, at least as a jumping-off point.
ctools owes a lot to the authors of the commands it replaces, especially Sergio Correia (-reghdfe-, -ivreghdfe-, -ppmlhdfe-, -ftools-), who also gave me tons of helpful comments and suggestions on this in the spring, and Mauricio Caceres Bravo (-gtools-, needs no introduction). Thanks also to the authors of -ivreg2-, -rangestat-, -rangejoin-, -cipolate-, -psmatch2-, -winsor2-, -binscatter-, and -binsreg-. A full list of kudos is on the GitHub's README, but do let me know if you think I've left anyone out who deserves some credit.
Please report bugs or results that don't match on the GitHub Issues page: https://github.com/mdroste/stata-ctools/issues. While AI is quite good at writing thousands of test cases and the proper harnesses to make sure these commands behave as expected on a variety of datasets, it can't catch everything.
Michael Droste
I've released a package called ctools. ctools is a suite of drop-in replacements for a bunch of existing Stata commands (both built-in and user-written), but in general these commands are much, much faster than the programs they replace. The syntax for ctools mostly matches the commands they replace, and the name usually just gets a "c" in front: -cimport-, -cmerge-, -csort-, -creghdfe-, etc. In a handful of cases these commands offer some extended functionality, or combine the functionality of multiple commands. For instance, -cbinscatter- can use the covariate adjustment approach from Cattaneo et al.'s -binsreg- instead of residualizing x and y first, and it also has some stuff from my own -binscatter2-. -cinterpolate- does both linear interpolation like -ipolate- and cubic interpolation like Nick Cox's -cipolate- (ok, this one was hard to replace), etc.
I am maintaining ctools on GitHub, and the repo has a variety of documentation (and of course the source code). The GitHub repository is here: https://www.github.com/mdroste/stata-ctools This repository has actually quietly been public for almost a year, but I don't care much for advertising my work and only a few people noticed. So we can call this Statalist post the 'public' release, although technically it's been available for most of this year and in fact has been on ssc for a month or so. I waited until I felt quite confident that the existing commands are fairly well-optimized, full-featured, and behave essentially identically to the commands they are based on.
ctools works with Stata 14.1 or later on Windows (10 or later), Mac (Apple Silicon or Intel, macOS 11 or later), and x86-64 Linux.
The latest version of ctools will always be available on my GitHub (above), and will be intermittently updated on SSC (I don't want to pester Kit too much every time something gets tweaked, this guy already does enough for this community!)
Install from my GitHub (latest version):
Code:
net install ctools, from("https://raw.githubusercontent.com/mdroste/stata-ctools/main/build") replace
Code:
ssc install ctools, replace
It is a little hard to tell you exactly how much faster ctools will be than the commands they replace, because this depends a lot on your data and your machine (primarily your CPU and memory bandwidth). As a rough rule of thumb, commands which require moving more of your dataset will be relatively less fast, and commands which involve very high observation counts (e.g. tens of millions or more) will be relatively more fast. Regressions (in my experience) often don't involve more than a dozen or so variables, so they hold up well. You can see the rough speedups on the GitHub, but broadly speaking most of these commands will run something like 2 to 50 times faster than the programs they replace (sometimes more).
Here are some of the included ctools commands and what they replace:
Code:
Command ctools version ----------------------------------------------------- Built-in commands import cimport export cexport sort csort merge cmerge sample csample bsample cbsample encode cencode decode cdecode destring cdestring ipolate cinterpolate split csplit qreg cqreg User-written commands distinct cdistinct rangejoin crangejoin winsor2, gstats winsor cwinsor rangestat crangestat binscatter, binsreg cbinscatter psmatch2 cpsmatch reghdfe creghdfe ivreghdfe civreghdfe ppmlhdfe cppmlhdfe hdfe chdfe cipolate cinterpolate, cubic
In the spirit of full disclosure, I am writing Statalist post today specifically because I just saw a new NBER working paper that was written to advertise a very similar, similarly vibecoded Stata command with a compiled plugin backend maintained by Johannes Schmieder (in this case just for -reghdfe- and -ppmlhdfe-). I am quite sure Johannes wasn't aware of my little project, which isn't his fault because I don't tend to promote my Stata projects online or in working papers, but I did want to get it out there in case this is of interest to the broader community and because it's been around for almost a year at this point and it feels ready to share. (And FWIW, since it is directly relevant to Johannes' NBER WP, on my machines -creghdfe- is about 1.5x to 2x faster than -fereg- on a variety of datasets). And although this is vibecoded, I've taken quite a bit of care to ensure it's extensible and the documentation mostly human readable everywhere. So, if you notice, you know, documentation that looks like a robot wrote it or whatever, just let me know (you can open a github issue or email me or whatever) and I'll get it fixed.
If you write your own C plugins, this is all open source and MIT-licensed, so you can do pretty much whatever you like with it. A lot of effort was spent on finding the most efficient possible way to use the Stata plugin interface to move data between Stata's memory and the C plugin (most of that code is in src/ctools_transport.c and src/ctools_data_io.c). I'd encourage any program-writers not to reinvent the wheel and to take some of this code, at least as a jumping-off point.
ctools owes a lot to the authors of the commands it replaces, especially Sergio Correia (-reghdfe-, -ivreghdfe-, -ppmlhdfe-, -ftools-), who also gave me tons of helpful comments and suggestions on this in the spring, and Mauricio Caceres Bravo (-gtools-, needs no introduction). Thanks also to the authors of -ivreg2-, -rangestat-, -rangejoin-, -cipolate-, -psmatch2-, -winsor2-, -binscatter-, and -binsreg-. A full list of kudos is on the GitHub's README, but do let me know if you think I've left anyone out who deserves some credit.
Please report bugs or results that don't match on the GitHub Issues page: https://github.com/mdroste/stata-ctools/issues. While AI is quite good at writing thousands of test cases and the proper harnesses to make sure these commands behave as expected on a variety of datasets, it can't catch everything.
Michael Droste

Comment