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

perf: Reduce to_lowercase() calls by caching results #1225

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ shell-words = "1.1.0"
plc_derive = { path = "./compiler/plc_derive" }
lld_rs = "140.0.0"
which = "4.2.5"
rustc-hash = "1.1.0"
log.workspace = true
inkwell.workspace = true
chrono.workspace = true
Expand All @@ -48,7 +49,7 @@ num = "0.4"
insta = "1.31.0"
pretty_assertions = "1.3.0"
driver = { path = "./compiler/plc_driver/", package = "plc_driver" }
project = { path = "./compiler/plc_project/", package = "plc_project", features = ["integration"]}
project = { path = "./compiler/plc_project/", package = "plc_project", features = ["integration"] }
plc_xml = { path = "./compiler/plc_xml" }
serial_test = "*"
tempfile = "3"
Expand Down
7 changes: 4 additions & 3 deletions compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use ast::{
ast::{pre_process, CompilationUnit, LinkageType},
provider::IdProvider,
};
use indexmap::IndexSet;

use plc::index::FxIndexSet;
use plc::{
codegen::{CodegenContext, GeneratedModule},
index::Index,
Expand Down Expand Up @@ -192,7 +193,7 @@ impl<T: SourceContainer + Sync> IndexedProject<T> {
/// A project that has been annotated with information about different types and used units
pub struct AnnotatedProject<T: SourceContainer + Sync> {
pub project: Project<T>,
pub units: Vec<(CompilationUnit, IndexSet<Dependency>, StringLiterals)>,
pub units: Vec<(CompilationUnit, FxIndexSet<Dependency>, StringLiterals)>,
pub index: Index,
pub annotations: AstAnnotations,
}
Expand Down Expand Up @@ -266,7 +267,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
context: &'ctx CodegenContext,
compile_options: &CompileOptions,
unit: &CompilationUnit,
dependencies: &IndexSet<Dependency>,
dependencies: &FxIndexSet<Dependency>,
literals: &StringLiterals,
) -> Result<GeneratedModule<'ctx>, Diagnostic> {
let mut code_generator = plc::codegen::CodeGen::new(
Expand Down
53 changes: 23 additions & 30 deletions compiler/plc_xml/src/model/fbd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use indexmap::{IndexMap, IndexSet};
use indexmap::IndexSet;
use plc::index::FxIndexMap;
use plc_diagnostics::diagnostics::Diagnostic;
use plc_source::source_location::SourceLocationFactory;
use quick_xml::events::{BytesStart, Event};
Expand All @@ -15,7 +16,7 @@ use super::{

/// Represent either a `localId` or `refLocalId`
pub(crate) type NodeId = usize;
pub(crate) type NodeIndex<'xml> = IndexMap<NodeId, Node<'xml>>;
pub(crate) type NodeIndex<'xml> = FxIndexMap<NodeId, Node<'xml>>;

#[derive(Debug, Default)]
pub(crate) struct FunctionBlockDiagram<'xml> {
Expand Down Expand Up @@ -113,7 +114,7 @@ impl<'xml> Node<'xml> {

impl<'xml> Parseable for FunctionBlockDiagram<'xml> {
fn visit(reader: &mut Reader, _tag: Option<BytesStart>) -> Result<Self, Error> {
let mut nodes = IndexMap::new();
let mut nodes = FxIndexMap::default();

loop {
match reader.read_event().map_err(Error::ReadEvent)? {
Expand Down Expand Up @@ -304,6 +305,7 @@ mod tests {
xml_parser::Parseable,
};
use insta::assert_debug_snapshot;
use plc::index::FxIndexMap;
use plc_source::source_location::SourceLocationFactory;

use super::Node;
Expand Down Expand Up @@ -336,7 +338,7 @@ mod tests {
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };

let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand All @@ -359,8 +361,7 @@ mod tests {
ref_local_id: Some(1),
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -375,7 +376,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -418,8 +419,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -434,7 +434,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -498,8 +498,7 @@ mod tests {
ref_local_id: Some(5),
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -514,7 +513,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -577,8 +576,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -593,7 +591,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -626,8 +624,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -642,7 +639,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -675,8 +672,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -693,7 +689,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -756,8 +752,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -773,7 +768,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -816,8 +811,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand All @@ -834,7 +828,7 @@ mod tests {
let mut model = Project::default();
let mut pou = Pou { name: "TestProg".into(), ..Default::default() };
let fbd = FunctionBlockDiagram {
nodes: [
nodes: FxIndexMap::from_iter([
(
1,
Node::FunctionBlockVariable(FunctionBlockVariable {
Expand Down Expand Up @@ -919,8 +913,7 @@ mod tests {
formal_parameter: None,
}),
),
]
.into(),
]),
};
pou.body.function_block_diagram = fbd;
model.pous.push(pou);
Expand Down
7 changes: 4 additions & 3 deletions compiler/plc_xml/src/xml_parser/fbd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ast::ast::{AstFactory, AstNode, AstStatement};
use indexmap::IndexMap;

use plc::index::FxIndexMap;
use plc_source::source_location::SourceLocation;

use crate::model::fbd::{FunctionBlockDiagram, Node, NodeId};
Expand All @@ -10,7 +11,7 @@ impl<'xml> FunctionBlockDiagram<'xml> {
/// Transforms the body of a function block diagram to their AST-equivalent, in order of execution.
/// Only statements that are necessary for execution logic will be selected.
pub(crate) fn transform(&self, session: &mut ParseSession) -> Vec<AstNode> {
let mut ast_association = IndexMap::new();
let mut ast_association = FxIndexMap::default();

// transform each node to an ast-statement. since we might see and transform a node multiple times, we use an
// ast-association map to keep track of the latest statement for each id
Expand All @@ -36,7 +37,7 @@ impl<'xml> FunctionBlockDiagram<'xml> {
&self,
id: NodeId,
session: &mut ParseSession,
ast_association: &IndexMap<usize, AstNode>,
ast_association: &FxIndexMap<usize, AstNode>,
) -> (AstNode, Option<NodeId>) {
let Some(current_node) = self.nodes.get(&id) else { unreachable!() };

Expand Down
7 changes: 3 additions & 4 deletions src/builtins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use inkwell::{
basic_block::BasicBlock,
types::BasicType,
Expand All @@ -17,6 +15,7 @@ use plc_ast::{
use plc_diagnostics::diagnostics::Diagnostic;
use plc_source::source_location::{SourceLocation, SourceLocationFactory};

use crate::index::FxHashMap;
use crate::{
codegen::generators::expression_generator::{self, ExpressionCodeGenerator, ExpressionValue},
index::Index,
Expand All @@ -32,7 +31,7 @@ use crate::{

// Defines a set of functions that are always included in a compiled application
lazy_static! {
static ref BUILTIN: HashMap<&'static str, BuiltIn> = HashMap::from([
static ref BUILTIN: FxHashMap<&'static str, BuiltIn> = FxHashMap::from_iter([
(
"ADR",
BuiltIn {
Expand Down Expand Up @@ -893,7 +892,7 @@ fn generate_variable_length_array_bound_function<'ink>(
}

type AnnotationFunction = fn(&mut TypeAnnotator, &AstNode, &AstNode, Option<&AstNode>, VisitorContext);
type GenericNameResolver = fn(&str, &[GenericBinding], &HashMap<String, GenericType>) -> String;
type GenericNameResolver = fn(&str, &[GenericBinding], &FxHashMap<String, GenericType>) -> String;
type CodegenFunction = for<'ink, 'b> fn(
&'b ExpressionCodeGenerator<'ink, 'b>,
&[&AstNode],
Expand Down
4 changes: 2 additions & 2 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
};

use super::index::*;
use indexmap::IndexSet;

use inkwell::{
context::Context,
execution_engine::{ExecutionEngine, JitFunction},
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<'ink> CodeGen<'ink> {
context: &'ink CodegenContext,
annotations: &AstAnnotations,
literals: &StringLiterals,
dependencies: &IndexSet<Dependency>,
dependencies: &FxIndexSet<Dependency>,
global_index: &Index,
) -> Result<LlvmTypedIndex<'ink>, Diagnostic> {
let llvm = Llvm::new(context, context.create_builder());
Expand Down
9 changes: 5 additions & 4 deletions src/codegen/debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, ops::Range, path::Path};
use std::{ops::Range, path::Path};

use inkwell::{
basic_block::BasicBlock,
Expand All @@ -16,6 +16,7 @@ use plc_ast::ast::LinkageType;
use plc_diagnostics::diagnostics::Diagnostic;
use plc_source::source_location::SourceLocation;

use crate::index::FxHashMap;
use crate::{
datalayout::{Bytes, DataLayout, MemoryLocation},
index::{ImplementationType, Index, PouIndexEntry, VariableIndexEntry},
Expand Down Expand Up @@ -147,10 +148,10 @@ pub struct DebugBuilder<'ink> {
context: &'ink Context,
debug_info: DebugInfoBuilder<'ink>,
compile_unit: DICompileUnit<'ink>,
types: HashMap<String, DebugType<'ink>>,
variables: HashMap<String, DILocalVariable<'ink>>,
types: FxHashMap<String, DebugType<'ink>>,
variables: FxHashMap<String, DILocalVariable<'ink>>,
optimization: OptimizationLevel,
files: HashMap<&'static str, DIFile<'ink>>,
files: FxHashMap<&'static str, DIFile<'ink>>,
}

/// A wrapper that redirects to correct debug builder implementation based on the debug context.
Expand Down
Loading
Loading