Use mmap for proc macro libs

This commit is contained in:
Laurențiu Nicola 2020-04-17 10:47:15 +03:00
parent 69f0cb6cd7
commit 93fcf1c133
3 changed files with 17 additions and 2 deletions

11
Cargo.lock generated
View file

@ -675,6 +675,16 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "memmap"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b"
dependencies = [
"libc",
"winapi 0.3.8",
]
[[package]] [[package]]
name = "memoffset" name = "memoffset"
version = "0.5.4" version = "0.5.4"
@ -1112,6 +1122,7 @@ dependencies = [
"difference", "difference",
"goblin", "goblin",
"libloading", "libloading",
"memmap",
"ra_mbe", "ra_mbe",
"ra_proc_macro", "ra_proc_macro",
"ra_tt", "ra_tt",

View file

@ -14,6 +14,7 @@ ra_mbe = { path = "../ra_mbe" }
ra_proc_macro = { path = "../ra_proc_macro" } ra_proc_macro = { path = "../ra_proc_macro" }
goblin = "0.2.1" goblin = "0.2.1"
libloading = "0.6.0" libloading = "0.6.0"
memmap = "0.7"
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }
[dev-dependencies] [dev-dependencies]

View file

@ -1,10 +1,12 @@
//! Handles dynamic library loading for proc macro //! Handles dynamic library loading for proc macro
use crate::{proc_macro::bridge, rustc_server::TokenStream}; use crate::{proc_macro::bridge, rustc_server::TokenStream};
use std::fs::File;
use std::path::Path; use std::path::Path;
use goblin::{mach::Mach, Object}; use goblin::{mach::Mach, Object};
use libloading::Library; use libloading::Library;
use memmap::Mmap;
use ra_proc_macro::ProcMacroKind; use ra_proc_macro::ProcMacroKind;
use std::io::Error as IoError; use std::io::Error as IoError;
@ -21,7 +23,8 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool {
} }
fn find_registrar_symbol(file: &Path) -> Result<Option<String>, IoError> { fn find_registrar_symbol(file: &Path) -> Result<Option<String>, IoError> {
let buffer = std::fs::read(file)?; let file = File::open(file)?;
let buffer = unsafe { Mmap::map(&file)? };
let object = Object::parse(&buffer).map_err(invalid_data_err)?; let object = Object::parse(&buffer).map_err(invalid_data_err)?;
match object { match object {
@ -55,7 +58,7 @@ fn find_registrar_symbol(file: &Path) -> Result<Option<String>, IoError> {
&s.name &s.name
} }
}) })
.find(|s| is_derive_registrar_symbol(&s)) .find(|s| is_derive_registrar_symbol(s))
.map(|s| s.to_string()); .map(|s| s.to_string());
Ok(name) Ok(name)
} }