annotate rust/chg/src/runcommand.rs @ 48178:f12a19d03d2c

fix: reduce number of tool executions By grouping together (path, ctx) pairs according to the inputs they would provide to fixer tools, we can deduplicate executions of fixer tools to significantly reduce the amount of time spent running slow tools. This change does not handle clean files in the working copy, which could still be deduplicated against the files in the checked out commit. It's a little harder to do that because the filerev is not available in the workingfilectx (and it doesn't exist for added files). Anecdotally, this change makes some real uses cases at Google 10x faster. I think we were originally hesitant to do this because the benefits weren't obvious, and implementing it efficiently is kind of tricky. If we simply memoized the formatter execution function, we would be keeping tons of file content in memory. Also included is a regression test for a corner case that I broke with my first attempt at optimizing this code. Differential Revision: https://phab.mercurial-scm.org/D11280
author Danny Hooper <hooper@google.com>
date Thu, 02 Sep 2021 14:08:45 -0700
parents 426294d06ddc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org>
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
2 //
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
3 // This software may be used and distributed according to the terms of the
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
4 // GNU General Public License version 2 or any later version.
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
5
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
6 //! Functions to run Mercurial command in cHg-aware command server.
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
7
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
8 use bytes::Bytes;
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
9 use std::io;
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
10 use std::os::unix::io::AsRawFd;
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
11 use tokio_hglib::codec::ChannelMessage;
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
12 use tokio_hglib::{Connection, Protocol};
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
13
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
14 use crate::attachio;
44689
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
15 use crate::message::{self, CommandType};
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
16 use crate::uihandler::SystemHandler;
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
17
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
18 /// Runs the given Mercurial command in cHg-aware command server, and
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
19 /// fetches the result code.
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
20 ///
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
21 /// This is a subset of tokio-hglib's `run_command()` with the additional
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
22 /// SystemRequest support.
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
23 pub async fn run_command(
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
24 proto: &mut Protocol<impl Connection + AsRawFd>,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
25 handler: &mut impl SystemHandler,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
26 packed_args: impl Into<Bytes>,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
27 ) -> io::Result<i32> {
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
28 proto
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
29 .send_command_with_args("runcommand", packed_args)
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
30 .await?;
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
31 loop {
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
32 match proto.fetch_response().await? {
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
33 ChannelMessage::Data(b'r', data) => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
34 return message::parse_result_code(data);
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
35 }
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
36 ChannelMessage::Data(..) => {
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
37 // just ignores data sent to optional channel
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
38 }
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
39 ChannelMessage::InputRequest(..)
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
40 | ChannelMessage::LineRequest(..) => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
41 return Err(io::Error::new(
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
42 io::ErrorKind::InvalidData,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
43 "unsupported request",
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
44 ));
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
45 }
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
46 ChannelMessage::SystemRequest(data) => {
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
47 let (cmd_type, cmd_spec) = message::parse_command_spec(data)?;
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
48 match cmd_type {
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
49 CommandType::Pager => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
50 // server spins new command loop while pager request is
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
51 // in progress, which can be terminated by "" command.
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
52 let pin = handler.spawn_pager(&cmd_spec).await?;
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
53 attachio::attach_io(proto, &io::stdin(), &pin, &pin)
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
54 .await?;
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
55 proto.send_command("").await?; // terminator
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
56 }
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
57 CommandType::System => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
58 let code = handler.run_system(&cmd_spec).await?;
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
59 let data = message::pack_result_code(code);
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
60 proto.send_data(data).await?;
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
61 }
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
62 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
63 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
64 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
65 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
66 }