Skip to content

Commit a1b7b00

Browse files
P-E-Pphilberty
authored andcommitted
Improve vector usages and avoid copies.
Several place built an object before copying it in a vector. This commit favorise in place construction and use readily available vector size to reserve memory beforehand. gcc/rust/ChangeLog: * ast/rust-expr.h (struct TupleClobber): Add constructor. * backend/rust-compile-context.h (struct fncontext): Likewise. * typecheck/rust-hir-dot-operator.h: Likewise. * typecheck/rust-tyty-variance-analysis-private.h (struct Constraint): Likewise. * typecheck/rust-unify.h: Likewise. * ast/rust-ast-builder.cc (Builder::new_lifetime_param): Add memory reservation and construct in place. (Builder::new_generic_args): Likewise. * ast/rust-ast-collector.cc (TokenCollector::newline): Likewise. (TokenCollector::indentation): Likewise. (TokenCollector::comment): Likewise. * ast/rust-desugar-apit.cc: Likewise. * ast/rust-path.cc (Path::convert_to_simple_path): Likewise. (TypePath::as_simple_path): Likewise. * ast/rust-path.h: Likewise. * backend/rust-compile-type.cc (TyTyResolveCompile::visit): Likewise. (TyTyResolveCompile::create_dyn_obj_record): Likewise. * checks/errors/rust-hir-pattern-analysis.cc (Matrix::specialize): Likewise. (WitnessMatrix::apply_constructor): Likewise. (check_match_usefulness): Likewise. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Likewise. * expand/rust-macro-builtins-asm.cc (parse_clobber_abi): Likewise. * expand/rust-macro-expand.cc (MacroExpander::parse_proc_macro_output): Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_generic_args): Likewise. (ASTLoweringBase::lower_extern_block): Likewise. * hir/rust-ast-lower-enumitem.h: Likewise. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise. * hir/rust-ast-lower-extern.h: Likewise. * hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit): Likewise. (ASTLowerTraitItem::visit): Likewise. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise. * hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit): Likewise. * hir/rust-ast-lower.cc (ASTLowering::go): Likewise. (ASTLoweringBlock::visit): Likewise. (ASTLoweringIfLetBlock::desugar_iflet): Likewise. (ASTLoweringExprWithBlock::visit): Likewise. (ASTLowerPathInExpression::visit): Likewise. (ASTLowerQualPathInExpression::visit): Likewise. * hir/tree/rust-hir.cc (PathPattern::convert_to_simple_path): Likewise. (TypePath::as_simple_path): Likewise. * metadata/rust-export-metadata.cc (ExportContext::emit_function): Likewise. * parse/rust-parse-impl.h (Parser::parse_decl_macro_def): Likewise. (Parser::parse_lifetime_params): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise. (ResolveItem::visit): Likewise. (flatten_list): Likewise. * resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::visit): Likewise. * typecheck/rust-autoderef.cc (AutoderefCycle::try_autoderefed): Likewise. * typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsized): Likewise. * typecheck/rust-hir-dot-operator.cc: Likewise. * typecheck/rust-hir-path-probe.cc: Likewise. * typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): Likewise. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_fn_trait_call): Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit): Likewise. (TypeCheckImplItem::visit): Likewise. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Likewise. * typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Likewise. * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise. * typecheck/rust-typecheck-context.cc (TypeCheckContext::push_return_type): Likewise. (TypeCheckContext::insert_associated_impl_mapping): Likewise. * typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::scan): Likewise. (TypeBoundsProbe::add_trait_bound): Likewise. (TypeBoundPredicate::operator=): Likewise. (TypeBoundPredicateItem::get_tyty_for_receiver): Likewise. (TypeBoundPredicate::get_associated_type_items): Likewise. * typecheck/rust-tyty-call.cc (TypeCheckMethodCallExpr::go): Likewise. * typecheck/rust-tyty-subst.cc (SubstitutionRef::clone_substs): Likewise. (SubstitutionRef::infer_substitions): Likewise. (SubstitutionRef::are_mappings_bound): Likewise. * typecheck/rust-tyty-variance-analysis.cc (GenericTyPerCrateCtx::query_generic_variance): Likewise. (GenericTyVisitorCtx::add_constraint): Likewise. * typecheck/rust-tyty.cc (FnPtr::clone): Likewise. (DynamicObjectType::get_object_items): Likewise. * typecheck/rust-unify.cc (UnifyRules::Resolve): Likewise. (UnifyRules::go): Likewise. (UnifyRules::expect_tuple): Likewise. * util/rust-canonical-path.h: Likewise. * util/rust-token-converter.cc (convert): Likewise. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 938ac21 commit a1b7b00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+418
-425
lines changed

gcc/rust/ast/rust-ast-builder.cc

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,12 @@ std::unique_ptr<GenericParam>
554554
Builder::new_lifetime_param (LifetimeParam &param)
555555
{
556556
Lifetime l = new_lifetime (param.get_lifetime ());
557+
557558
std::vector<Lifetime> lifetime_bounds;
559+
lifetime_bounds.reserve (param.get_lifetime_bounds ().size ());
560+
558561
for (auto b : param.get_lifetime_bounds ())
559-
{
560-
Lifetime bl = new_lifetime (b);
561-
lifetime_bounds.push_back (bl);
562-
}
562+
lifetime_bounds.emplace_back (new_lifetime (b));
563563

564564
auto p = new LifetimeParam (l, std::move (lifetime_bounds),
565565
param.get_outer_attrs (), param.get_locus ());
@@ -605,11 +605,11 @@ Builder::new_type_param (
605605
for (const auto &lifetime : tb.get_for_lifetimes ())
606606
{
607607
std::vector<Lifetime> lifetime_bounds;
608+
lifetime_bounds.reserve (
609+
lifetime.get_lifetime_bounds ().size ());
610+
608611
for (const auto &b : lifetime.get_lifetime_bounds ())
609-
{
610-
Lifetime bl = new_lifetime (b);
611-
lifetime_bounds.push_back (std::move (bl));
612-
}
612+
lifetime_bounds.emplace_back (new_lifetime (b));
613613

614614
Lifetime nl = new_lifetime (lifetime.get_lifetime ());
615615
LifetimeParam p (std::move (nl), std::move (lifetime_bounds),
@@ -626,12 +626,11 @@ Builder::new_type_param (
626626
{
627627
const TypePathSegment &segment
628628
= (const TypePathSegment &) (*seg.get ());
629-
TypePathSegment *s = new TypePathSegment (
629+
630+
segments.emplace_back (new TypePathSegment (
630631
segment.get_ident_segment (),
631632
segment.get_separating_scope_resolution (),
632-
segment.get_locus ());
633-
std::unique_ptr<TypePathSegment> sg (s);
634-
segments.push_back (std::move (sg));
633+
segment.get_locus ()));
635634
}
636635
break;
637636

@@ -642,11 +641,10 @@ Builder::new_type_param (
642641

643642
GenericArgs args
644643
= new_generic_args (generic.get_generic_args ());
645-
TypePathSegmentGeneric *s = new TypePathSegmentGeneric (
644+
645+
segments.emplace_back (new TypePathSegmentGeneric (
646646
generic.get_ident_segment (), false, std::move (args),
647-
generic.get_locus ());
648-
std::unique_ptr<TypePathSegment> sg (s);
649-
segments.push_back (std::move (sg));
647+
generic.get_locus ()));
650648
}
651649
break;
652650

@@ -664,23 +662,19 @@ Builder::new_type_param (
664662
TypePath p (std::move (segments), path.get_locus (),
665663
path.has_opening_scope_resolution_op ());
666664

667-
TraitBound *b = new TraitBound (std::move (p), tb.get_locus (),
668-
tb.is_in_parens (),
669-
tb.has_opening_question_mark (),
670-
std::move (for_lifetimes));
671-
std::unique_ptr<TypeParamBound> bound (b);
672-
type_param_bounds.push_back (std::move (bound));
665+
type_param_bounds.emplace_back (new TraitBound (
666+
std::move (p), tb.get_locus (), tb.is_in_parens (),
667+
tb.has_opening_question_mark (), std::move (for_lifetimes)));
673668
}
674669
break;
675670

676671
case TypeParamBound::TypeParamBoundType::LIFETIME:
677672
{
678673
const Lifetime &l = (const Lifetime &) *b.get ();
679674

680-
auto bl = new Lifetime (l.get_lifetime_type (),
681-
l.get_lifetime_name (), l.get_locus ());
682-
std::unique_ptr<TypeParamBound> bound (bl);
683-
type_param_bounds.push_back (std::move (bound));
675+
type_param_bounds.emplace_back (
676+
new Lifetime (l.get_lifetime_type (), l.get_lifetime_name (),
677+
l.get_locus ()));
684678
}
685679
break;
686680
}
@@ -709,18 +703,14 @@ Builder::new_generic_args (GenericArgs &args)
709703
location_t locus = args.get_locus ();
710704

711705
for (const auto &lifetime : args.get_lifetime_args ())
712-
{
713-
Lifetime l = new_lifetime (lifetime);
714-
lifetime_args.push_back (std::move (l));
715-
}
706+
lifetime_args.push_back (new_lifetime (lifetime));
716707

717708
for (auto &binding : args.get_binding_args ())
718709
{
719710
Type &t = *binding.get_type_ptr ().get ();
720711
std::unique_ptr<Type> ty = t.reconstruct ();
721-
GenericArgsBinding b (binding.get_identifier (), std::move (ty),
722-
binding.get_locus ());
723-
binding_args.push_back (std::move (b));
712+
binding_args.emplace_back (binding.get_identifier (), std::move (ty),
713+
binding.get_locus ());
724714
}
725715

726716
for (auto &arg : args.get_generic_args ())

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ TokenCollector::trailing_comma ()
7676
void
7777
TokenCollector::newline ()
7878
{
79-
tokens.push_back ({CollectItem::Kind::Newline});
79+
tokens.emplace_back (CollectItem::Kind::Newline);
8080
}
8181

8282
void
8383
TokenCollector::indentation ()
8484
{
85-
tokens.push_back ({indent_level});
85+
tokens.emplace_back (indent_level);
8686
}
8787

8888
void
@@ -101,7 +101,7 @@ TokenCollector::decrement_indentation ()
101101
void
102102
TokenCollector::comment (std::string comment)
103103
{
104-
tokens.push_back ({comment});
104+
tokens.emplace_back (comment);
105105
}
106106

107107
void

gcc/rust/ast/rust-desugar-apit.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,19 @@ class DesugarApitType : public DefaultASTVisitor
188188

189189
// Convert to TypePath by creating path segments
190190
std::vector<std::unique_ptr<TypePathSegment>> segments;
191-
segments.push_back (std::unique_ptr<TypePathSegment> (new TypePathSegment (
192-
PathIdentSegment (ident.as_string (), type.get_locus ()), false,
193-
type.get_locus ())));
191+
segments.emplace_back (
192+
new TypePathSegment (PathIdentSegment (ident.as_string (),
193+
type.get_locus ()),
194+
false, type.get_locus ()));
194195

195196
// Create TypePath from segments
196197
auto type_path
197198
= new TypePath (std::move (segments), type.get_locus (), false);
198199

199200
// Convert bounds from impl trait to generic parameter bounds
200201
std::vector<std::unique_ptr<TypeParamBound>> bounds;
202+
bounds.reserve (type.get_type_param_bounds ().size ());
203+
201204
for (auto &bound : type.get_type_param_bounds ())
202205
bounds.push_back (bound->clone_type_param_bound ());
203206

@@ -228,9 +231,10 @@ class DesugarApitType : public DefaultASTVisitor
228231

229232
// Convert to TypePath by creating path segments
230233
std::vector<std::unique_ptr<TypePathSegment>> segments;
231-
segments.push_back (std::unique_ptr<TypePathSegment> (new TypePathSegment (
232-
PathIdentSegment (ident.as_string (), type.get_locus ()), false,
233-
type.get_locus ())));
234+
segments.emplace_back (
235+
new TypePathSegment (PathIdentSegment (ident.as_string (),
236+
type.get_locus ()),
237+
false, type.get_locus ()));
234238

235239
// Create TypePath from segments
236240
auto type_path
@@ -407,6 +411,9 @@ class ApitBoundProcessor
407411

408412
std::vector<std::unique_ptr<TypeParamBound>>
409413
type_param_bounds;
414+
type_param_bounds.reserve (
415+
tp.get_type_param_bounds ().size ());
416+
410417
for (auto &b : tp.get_type_param_bounds ())
411418
type_param_bounds.push_back (std::move (b));
412419
tp.get_type_param_bounds ().clear ();
@@ -459,9 +466,10 @@ class ApitBoundProcessor
459466
std::vector<SimplePathSegment> simple_segs = {simple_seg};
460467
auto simple_path = SimplePath (simple_segs, false, ident.get_locus ());
461468
std::vector<std::unique_ptr<TypePathSegment>> segments;
462-
segments.push_back (std::unique_ptr<TypePathSegment> (new TypePathSegment (
463-
PathIdentSegment (ident.as_string (), ident.get_locus ()), false,
464-
ident.get_locus ())));
469+
segments.emplace_back (
470+
new TypePathSegment (PathIdentSegment (ident.as_string (),
471+
ident.get_locus ()),
472+
false, ident.get_locus ()));
465473
auto type_path = new TypePath (std::move (segments), ident.get_locus ());
466474
return std::unique_ptr<Type> (type_path);
467475
}

gcc/rust/ast/rust-expr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5494,6 +5494,8 @@ struct InlineAsmTemplatePiece
54945494

54955495
struct TupleClobber
54965496
{
5497+
TupleClobber (std::string symbol, location_t loc) : symbol (symbol), loc (loc)
5498+
{}
54975499
// as gccrs still doesn't contain a symbol class I have put them as strings
54985500
std::string symbol;
54995501
location_t loc;

gcc/rust/ast/rust-path.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ Path::convert_to_simple_path (bool with_opening_scope_resolution) const
173173

174174
// create segment and add to vector
175175
std::string segment_str = segment.as_string ();
176-
simple_segments.push_back (
177-
SimplePathSegment (std::move (segment_str), segment.get_locus ()));
176+
simple_segments.emplace_back (std::move (segment_str),
177+
segment.get_locus ());
178178
}
179179

180180
// kind of a HACK to get locus depending on opening scope resolution
@@ -258,8 +258,8 @@ TypePath::as_simple_path () const
258258

259259
// create segment and add to vector
260260
std::string segment_str = segment->as_string ();
261-
simple_segments.push_back (
262-
SimplePathSegment (std::move (segment_str), segment->get_locus ()));
261+
simple_segments.emplace_back (std::move (segment_str),
262+
segment->get_locus ());
263263
}
264264

265265
return SimplePath (std::move (simple_segments), has_opening_scope_resolution,

gcc/rust/ast/rust-path.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,7 @@ struct GenericArgs
448448
generic_args.clear ();
449449
generic_args.reserve (other.generic_args.size ());
450450
for (const auto &arg : other.generic_args)
451-
{
452-
generic_args.push_back (GenericArg (arg));
453-
}
451+
generic_args.emplace_back (arg);
454452
}
455453

456454
~GenericArgs () = default;
@@ -465,9 +463,7 @@ struct GenericArgs
465463
generic_args.clear ();
466464
generic_args.reserve (other.generic_args.size ());
467465
for (const auto &arg : other.generic_args)
468-
{
469-
generic_args.push_back (GenericArg (arg));
470-
}
466+
generic_args.emplace_back (arg);
471467

472468
return *this;
473469
}

gcc/rust/backend/rust-compile-context.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ namespace Compile {
3434

3535
struct fncontext
3636
{
37+
fncontext (tree fndecl, ::Bvariable *ret_addr, TyTy::BaseType *retty)
38+
: fndecl (fndecl), ret_addr (ret_addr), retty (retty)
39+
{}
40+
3741
tree fndecl;
3842
::Bvariable *ret_addr;
3943
TyTy::BaseType *retty;
@@ -154,7 +158,7 @@ class Context
154158
if (it == mono_fns.end ())
155159
mono_fns[dId] = {};
156160

157-
mono_fns[dId].push_back ({ref, fn});
161+
mono_fns[dId].emplace_back (ref, fn);
158162
}
159163

160164
void insert_closure_decl (const TyTy::ClosureType *ref, tree fn)
@@ -164,7 +168,7 @@ class Context
164168
if (it == mono_closure_fns.end ())
165169
mono_closure_fns[dId] = {};
166170

167-
mono_closure_fns[dId].push_back ({ref, fn});
171+
mono_closure_fns[dId].emplace_back (ref, fn);
168172
}
169173

170174
tree lookup_closure_decl (const TyTy::ClosureType *ref)
@@ -279,7 +283,7 @@ class Context
279283

280284
void push_fn (tree fn, ::Bvariable *ret_addr, TyTy::BaseType *retty)
281285
{
282-
fn_stack.push_back (fncontext{fn, ret_addr, retty});
286+
fn_stack.emplace_back (fn, ret_addr, retty);
283287
}
284288
void pop_fn () { fn_stack.pop_back (); }
285289

0 commit comments

Comments
 (0)