This is an archive of the discontinued Mercurial Phabricator instance.

rhg: Add support for automatic fallback to Python
ClosedPublic

Authored by SimonSapin on Mar 2 2021, 2:58 PM.

Details

Summary

rhg is a command-line application that can do a small subset of what
hg can. It is written entirely in Rust, which avoids the cost of starting
a Python interpreter and importing many Python modules.
In a script that runs many hg commands, this cost can add up.

However making users decide when to use rhg instead of hg is
not practical as we want the subset of supported functionality
to grow over time.

Instead we introduce "fallback" behavior where, when rhg encounters
something (a sub-command, a repository format, …) that is not implemented
in Rust-only, it does nothing but silently start a subprocess of
Python-based hg running the same command.
That way rhg becomes a drop-in replacement for hg that sometimes
goes faster. Whether Python is used should be an implementation detail
not apparent to users (other than through speed).

A new fallback value is added to the previously introduced
rhg.on-unsupported configuration key. When in this mode, the new
rhg.fallback-executable config is determine what command to use
to run a Python-based hg.

The previous rhg.on-unsupported = abort-silent configuration was designed
to let a wrapper script call rhg and then fall back to hg based on the
exit code. This is still available, but having fallback behavior built-in
in rhg might be easier for users instead of leaving that script "as an
exercise for the reader".

Using a subprocess like this is not idea, especially when rhg is to be
installed in $PATH as hg, since the other hg.py executable needs
to still be available… somewhere. Eventually this could be replaced
by using PyOxidizer to a have a single executable that embeds a Python
interpreter, but only starts it when needed.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

SimonSapin created this revision.Mar 2 2021, 2:58 PM
baymax updated this revision to Diff 26028.Mar 2 2021, 4:21 PM

✅ refresh by Heptapod after a successful CI run (🐙 💚)

Alphare added a subscriber: Alphare.Mar 5 2021, 3:35 AM

Please add a small description to your changeset that explains the new config value and why we would want rhg to host a fallback system itself when it already has a specific error code for this.

rust/rhg/src/main.rs
161

Not sure what warning you're planning to add

191–192

You've added this in the previous change, and are removing it now. Was this a hg split mistake?

Alphare requested changes to this revision.Mar 5 2021, 3:36 AM
This revision now requires changes to proceed.Mar 5 2021, 3:36 AM
pulkit added a subscriber: pulkit.Mar 9 2021, 2:17 AM

Please add a small description to your changeset that explains the new config value and why we would want rhg to host a fallback system itself when it already has a specific error code for this.

+1. Having details in commit description will be nice.

SimonSapin marked 2 inline comments as done.Mar 9 2021, 4:39 AM
SimonSapin edited the summary of this revision. (Show Details)
SimonSapin updated this revision to Diff 26166.

I’ve expanded the commit message.

rust/rhg/src/main.rs
161

I forgot to remove this TODO comment when adding the "Blocking recursive fallback" message written to stderr above.

191–192

What happened is:

This comment has been there for Command::Abort a while. When adding a second occurrence of this let _ = ui.write_strerr(…); pattern in the previous commit I copied the comment, but at the third in this commit this felt too redundant so I only kept one.

I’ve amended the previous commit to only keep one occurrence.

Alphare accepted this revision.Mar 10 2021, 3:30 AM

Thanks for the thorough description.

pulkit accepted this revision.Mar 10 2021, 1:31 PM
This revision is now accepted and ready to land.Mar 10 2021, 1:31 PM
This revision was automatically updated to reflect the committed changes.