aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuyuki Ishi <ishitatsuyuki@google.com>2024-04-10 03:35:46 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-04-12 14:55:48 +0000
commit9b49b4f22d1d3fac3f0a142b269e1b95d7716e11 (patch)
treee5f161b694e9e461ae9a3bb8fbb19984464a58be
parentdc3dc47e53e3030966505bb0f49980cf8236643a (diff)
downloadtoolchain-utils-9b49b4f22d1d3fac3f0a142b269e1b95d7716e11.tar.gz
rust-analyzer-chromiumos-wrapper: Use arrays instead of hashmaps to store replacements
For string replacements, hashmap don't really improve performance nor simplify code. Just use arrays which is a little simpler. Also update the tests to take a variable count of replacements, which will be useful for future tests. BUG=b:333979840 TEST=cargo test Change-Id: I59de9cdeeebe262dc20d2ef438d9206574c4a6b6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5446200 Reviewed-by: Allen Webb <allenwebb@google.com> Tested-by: Tatsuyuki Ishi <ishitatsuyuki@google.com> Reviewed-by: George Burgess <gbiv@chromium.org> Commit-Queue: George Burgess <gbiv@chromium.org> Auto-Submit: Tatsuyuki Ishi <ishitatsuyuki@google.com>
-rw-r--r--rust-analyzer-chromiumos-wrapper/src/main.rs44
1 files changed, 14 insertions, 30 deletions
diff --git a/rust-analyzer-chromiumos-wrapper/src/main.rs b/rust-analyzer-chromiumos-wrapper/src/main.rs
index 09657b12..d653530c 100644
--- a/rust-analyzer-chromiumos-wrapper/src/main.rs
+++ b/rust-analyzer-chromiumos-wrapper/src/main.rs
@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{self, BufRead, BufReader, BufWriter, Write};
@@ -122,16 +121,14 @@ fn main() -> Result<()> {
let mut child_stdin = BufWriter::new(child.0.stdin.take().unwrap());
let mut child_stdout = BufReader::new(child.0.stdout.take().unwrap());
- let replacement_map = {
- let mut m = HashMap::new();
- m.insert(outside_prefix, inside_prefix);
- m.insert(outside_sysroot_prefix, "/usr/lib/rustlib");
- m.insert(outside_home, "/home");
- m
- };
+ let replacement_map = [
+ (outside_prefix, inside_prefix),
+ (outside_sysroot_prefix, "/usr/lib/rustlib"),
+ (outside_home, "/home"),
+ ];
let join_handle = {
- let rm = replacement_map.clone();
+ let rm = replacement_map;
thread::spawn(move || {
let mut stdin = io::stdin().lock();
stream_with_replacement(&mut stdin, &mut child_stdin, &rm)
@@ -140,7 +137,7 @@ fn main() -> Result<()> {
};
// For the mapping between inside to outside, we just reverse the map.
- let replacement_map_rev = replacement_map.iter().map(|(k, v)| (*v, *k)).collect();
+ let replacement_map_rev = replacement_map.map(|(k, v)| (v, k));
let mut stdout = BufWriter::new(io::stdout().lock());
stream_with_replacement(&mut child_stdout, &mut stdout, &replacement_map_rev)
.context("Streaming from rust-analyzer into stdout")?;
@@ -209,12 +206,8 @@ fn read_header<R: BufRead>(r: &mut R, header: &mut Header) -> Result<()> {
/// Extend `dest` with `contents`, replacing any occurrence of patterns in a json string in
/// `contents` with a replacement.
-fn replace(
- contents: &[u8],
- replacement_map: &HashMap<&str, &str>,
- dest: &mut Vec<u8>,
-) -> Result<()> {
- fn map_value(val: Value, replacement_map: &HashMap<&str, &str>) -> Value {
+fn replace(contents: &[u8], replacement_map: &[(&str, &str)], dest: &mut Vec<u8>) -> Result<()> {
+ fn map_value(val: Value, replacement_map: &[(&str, &str)]) -> Value {
match val {
Value::String(s) =>
// `s.replace` is very likely doing more work than necessary. Probably we only need
@@ -271,7 +264,7 @@ fn replace(
fn stream_with_replacement<R: BufRead, W: Write>(
r: &mut R,
w: &mut W,
- replacement_map: &HashMap<&str, &str>,
+ replacement_map: &[(&str, &str)],
) -> Result<()> {
let mut head = Header::default();
let mut buf = Vec::with_capacity(1024);
@@ -350,16 +343,10 @@ mod test {
fn test_stream_with_replacement(
read: &str,
- pattern: &str,
- replacement: &str,
+ replacement_map: &[(&str, &str)],
json_expected: &str,
) -> Result<()> {
let mut w = Vec::new();
- let replacement_map = {
- let mut m = HashMap::new();
- m.insert(pattern, replacement);
- m
- };
let input = format!("Content-Length: {}\r\n\r\n{}", read.as_bytes().len(), read);
stream_with_replacement(&mut input.as_bytes(), &mut w, &replacement_map)?;
@@ -394,8 +381,7 @@ mod test {
},
"anotherkey": "XYZXYZdef"
}"#,
- "XYZXYZ",
- "REPLACE",
+ &[("XYZXYZ", "REPLACE")],
r#"{
"somekey": {
"somepath": "REPLACEabc",
@@ -417,8 +403,7 @@ mod test {
},
"key4": 1
}"#,
- "ABCDEF",
- "replacement",
+ &[("ABCDEF", "replacement")],
r#"{
"key0": "sometextreplacement",
"key1": {
@@ -436,8 +421,7 @@ mod test {
r#"{
"path": "/my_folder/rust-analyzer-chromiumos-wrapper"
}"#,
- "",
- "",
+ &[],
r#"{
"path": "/usr/sbin/rust-analyzer"
}"#,