Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: less cloning and caching #235

Merged
merged 4 commits into from
Apr 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: fix Hash implementation for Segment
gvozdvmozgu committed Apr 27, 2024
commit e7aebd197f78b7a1262339e2cd1943877a3deee6
24 changes: 21 additions & 3 deletions crates/lib/src/core/parser/segments/base.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ use std::rc::Rc;

use ahash::{AHashMap, AHashSet};
use dyn_clone::DynClone;
use dyn_hash::DynHash;
use dyn_ord::DynEq;
use itertools::{enumerate, Itertools};
use serde::ser::SerializeMap;
@@ -119,12 +118,16 @@ impl ErasedSegment {
}
}

pub trait Segment: Any + DynEq + DynClone + DynHash + Debug + CloneSegment {
pub trait Segment: Any + DynEq + DynClone + Debug + CloneSegment {
#[allow(clippy::new_ret_no_self, clippy::wrong_self_convention)]
fn new(&self, _segments: Vec<ErasedSegment>) -> ErasedSegment {
unimplemented!("{}", std::any::type_name::<Self>())
}

fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}

fn as_object_reference(&self) -> Node<ObjectReferenceSegment> {
if let Some(value) = self.as_any().downcast_ref::<Node<ObjectReferenceSegment>>() {
return value.clone();
@@ -673,7 +676,6 @@ pub trait Segment: Any + DynEq + DynClone + DynHash + Debug + CloneSegment {
}

dyn_clone::clone_trait_object!(Segment);
dyn_hash::hash_trait_object!(Segment);

impl PartialEq for dyn Segment {
fn eq(&self, other: &Self) -> bool {
@@ -697,6 +699,22 @@ impl PartialEq for dyn Segment {

impl Eq for ErasedSegment {}

impl Hash for dyn Segment {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.type_name().hash(state);

if let Some(marker) = &self.get_raw() {
marker.hash(state);
}

if let Some(marker) = &self.get_position_marker() {
marker.source_position().hash(state);
} else {
None::<usize>.hash(state);
}
}
}

pub fn position_segments(
segments: &[ErasedSegment],
parent_pos: Option<&PositionMarker>,