Skip to content

Commit

Permalink
Use database of partial paths to speed up bindings resolution (NomicF…
Browse files Browse the repository at this point in the history
…oundation#1204)

Rebasing NomicFoundation#1198 on latest after NomicFoundation#1195 was merged.

---------

Co-authored-by: Gustavo Giráldez <[email protected]>
  • Loading branch information
OmarTawfik and ggiraldez authored Jan 2, 2025
1 parent 35d89d6 commit 3936e23
Show file tree
Hide file tree
Showing 50 changed files with 2,138 additions and 2,082 deletions.
10 changes: 4 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ serde = { version = "1.0.217", features = ["derive", "rc"] }
serde_json = { version = "1.0.134", features = ["preserve_order"] }
similar-asserts = { version = "1.6.0" }
smallvec = { version = "1.7.0", features = ["union"] }
stack-graphs = { version = "0.13.0" }
stack-graphs = { git = "https://github.com/NomicFoundation/stack-graphs", branch = "nomic" }
string-interner = { version = "0.17.0", features = [
"std",
"inline-more",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use semver::Version;

use crate::bindings::BindingGraph;
use crate::bindings::BindingGraphBuilder;
use crate::parser::ParserInitializationError;

#[allow(clippy::needless_pass_by_value)]
pub fn add_built_ins(
_binding_graph: &mut BindingGraph,
_binding_graph_builder: &mut BindingGraphBuilder,
_version: Version,
) -> Result<(), ParserInitializationError> {
unreachable!("Built-ins are Solidity-specific")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use semver::Version;

use crate::cst::KindTypes;

pub type BindingGraphBuilder = metaslang_bindings::BindingGraphBuilder<KindTypes>;
pub type BindingGraph = metaslang_bindings::BindingGraph<KindTypes>;
pub type Definition<'a> = metaslang_bindings::Definition<'a, KindTypes>;
pub type Reference<'a> = metaslang_bindings::Reference<'a, KindTypes>;
pub type Definition = metaslang_bindings::Definition<KindTypes>;
pub type Reference = metaslang_bindings::Reference<KindTypes>;
pub type BindingLocation = metaslang_bindings::BindingLocation<KindTypes>;
pub type UserFileLocation = metaslang_bindings::UserFileLocation<KindTypes>;

Expand All @@ -29,8 +30,8 @@ pub enum BindingGraphInitializationError {
pub fn create_with_resolver(
version: Version,
resolver: Rc<dyn PathResolver<KindTypes>>,
) -> Result<BindingGraph, ParserInitializationError> {
let mut binding_graph = BindingGraph::create(
) -> Result<BindingGraphBuilder, BindingGraphInitializationError> {
let mut binding_graph = BindingGraphBuilder::create(
version.clone(),
binding_rules::BINDING_RULES_SOURCE,
resolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ impl CompilationUnit {
files: self.files.clone(),
};

let mut binding_graph =
let mut builder =
create_with_resolver(self.language_version.clone(), Rc::new(resolver))?;

for (id, file) in &self.files {
binding_graph.add_user_file(id, file.create_tree_cursor());
builder.add_user_file(id, file.create_tree_cursor());
}

Ok(Rc::new(binding_graph))
Ok(builder.build())
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/runtime/cargo/crate/src/runtime/cst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod terminal_kind;
pub use edge_label::EdgeLabel;
pub(crate) use lexical_context::{IsLexicalContext, LexicalContext, LexicalContextType};
pub use metaslang_cst::kinds::{
EdgeLabelExtensions, NonterminalKindExtensions, TerminalKindExtensions,
EdgeLabelExtensions, NodeKind, NonterminalKindExtensions, TerminalKindExtensions,
};
pub use nonterminal_kind::NonterminalKind;
pub use terminal_kind::TerminalKind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,8 @@ mod ffi {

mod rust {
pub use crate::rust_crate::bindings::{
BindingGraph, BindingLocation, BuiltInLocation, UserFileLocation,
BindingGraph, BindingLocation, BuiltInLocation, Definition, Reference, UserFileLocation,
};

/// TODO: This is a work-around for the fact that `metaslang_bindings` internals (handles, locators, etc...) are exposed.
/// We should clean this when we finally publish `__experimental_bindings_api`.
/// That means removing the types below, and using the original types instead.
#[derive(Debug, Clone)]
pub struct Definition {
pub id: usize,
pub name_location: BindingLocation,
pub definiens_location: BindingLocation,
}

impl From<crate::rust_crate::bindings::Definition<'_>> for Definition {
fn from(definition: crate::rust_crate::bindings::Definition<'_>) -> Self {
Self {
id: definition.id(),
name_location: definition.name_location(),
definiens_location: definition.definiens_location(),
}
}
}

/// TODO: This is a work-around for the fact that `metaslang_bindings` internals (handles, locators, etc...) are exposed.
/// We should clean this when we finally publish `__experimental_bindings_api`.
/// That means removing the types below, and using the original types instead.
#[derive(Debug, Clone)]
pub struct Reference {
pub id: usize,
pub location: BindingLocation,
pub definitions: Vec<Definition>,
}

impl From<crate::rust_crate::bindings::Reference<'_>> for Reference {
fn from(reference: crate::rust_crate::bindings::Reference<'_>) -> Self {
Self {
id: reference.id(),
location: reference.location(),
definitions: reference
.definitions()
.into_iter()
.map(Into::into)
.collect(),
}
}
}
}

impl ffi::Guest for crate::wasm_crate::World {
Expand Down Expand Up @@ -100,15 +56,15 @@ define_rc_wrapper! { BindingGraph {

define_wrapper! { Definition {
fn id(&self) -> u32 {
self._borrow_ffi().id.try_into().unwrap()
self._borrow_ffi().id().try_into().unwrap()
}

fn name_location(&self) -> ffi::BindingLocation {
self._borrow_ffi().name_location.clone()._into_ffi()
self._borrow_ffi().name_location()._into_ffi()
}

fn definiens_location(&self) -> ffi::BindingLocation {
self._borrow_ffi().definiens_location.clone()._into_ffi()
self._borrow_ffi().definiens_location()._into_ffi()
}
} }

Expand All @@ -120,15 +76,15 @@ define_wrapper! { Definition {

define_wrapper! { Reference {
fn id(&self) -> u32 {
self._borrow_ffi().id.try_into().unwrap()
self._borrow_ffi().id().try_into().unwrap()
}

fn location(&self) -> ffi::BindingLocation {
self._borrow_ffi().location.clone()._into_ffi()
self._borrow_ffi().location().clone()._into_ffi()
}

fn definitions(&self) -> Vec<ffi::Definition> {
self._borrow_ffi().definitions.iter().cloned().map(IntoFFI::_into_ffi).collect()
self._borrow_ffi().definitions().iter().cloned().map(IntoFFI::_into_ffi).collect()
}
} }

Expand Down
92 changes: 41 additions & 51 deletions crates/metaslang/bindings/generated/public_api.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ mod resolver {
use metaslang_graph_builder::graph::{Graph, Value};
use metaslang_graph_builder::ExecutionError;

use crate::{FileDescriptor, PathResolver};
use crate::builder::FileDescriptor;
use crate::PathResolver;

pub fn add_functions<KT: KindTypes + 'static>(
functions: &mut Functions<KT>,
Expand Down
Loading

0 comments on commit 3936e23

Please sign in to comment.