This is an archive of the discontinued LLVM Phabricator instance.

use ref to avoid copy in range for-loop
ClosedPublic

Authored by XinWang10 on May 6 2023, 1:40 AM.

Details

Summary

Use big obj copy in range for-loop will call copy constructor every time,
which can be avoided by use ref instead.

Diff Detail

Event Timeline

XinWang10 created this revision.May 6 2023, 1:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2023, 1:40 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
XinWang10 requested review of this revision.May 6 2023, 1:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2023, 1:40 AM
skan added a comment.May 26 2023, 3:24 AM

Could you help illustrate why "they're big obj copys"?

XinWang10 added inline comments.May 28 2023, 6:45 PM
llvm/include/llvm/MC/MCParser/MCAsmParser.h
239

Every element here is type MCPendingError defined in MCAsmPaser.h, It contains 1 SMLoc(has 1 internal pointer as member), 1 SmallString, 1 SMRange(composed of 2 SMLoc).

struct MCPendingError {
    SMLoc Loc;
    SmallString<64> Msg;
    SMRange Range;
  };
SmallVector<MCPendingError, 0> PendingErrors;

This enumerated element has 4 pointers which is bigger than 64bit, I think it's better here to use ref.

llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
1315

The enumerated type is InstToEntryLocMap, every element is a pair of MachineInstr * and LocIndex(composed of 2 uint32_t.

using InstToEntryLocMap = std::multimap<const MachineInstr *, LocIndex>;
llvm/lib/CodeGen/MIRParser/MIRParser.cpp
405

YamlMF.CallSitesInfo is type CallSiteInfoMap, defined in MachineFunction.h line 452.

  struct ArgRegPair {
    Register Reg;
    uint16_t ArgNo;
    ArgRegPair(Register R, unsigned Arg) : Reg(R), ArgNo(Arg) {
      assert(Arg < (1 << 16) && "Arg out of range");
    }
  };
using CallSiteInfo = SmallVector<ArgRegPair, 1>;
...
using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;

Every element here is of type CallSiteInfo which contains a vector.

llvm/lib/ObjectYAML/MachOEmitter.cpp
429

The Opcode here is type MachOYAML::BindOpcode, defined in MachOYAML.h line 98.

struct BindOpcode {
  MachO::BindOpcode Opcode;
  uint8_t Imm;
  std::vector<yaml::Hex64> ULEBExtraData;
  std::vector<int64_t> SLEBExtraData;
  StringRef Symbol;
};
llvm/utils/TableGen/GlobalISel/GIMatchTree.cpp
736

It enumerated NewLeaves which is type GIMatchTreeBuilder::LeafVec and TraversedEdgesByNewLeaves.
In GIMatchTree.h

using LeafVec = std::vector<GIMatchTreeBuilderLeafInfo>;

and contains to many elements.

class GIMatchTreeBuilderLeafInfo {
protected:
  GIMatchTreeBuilder &Builder;
  GIMatchTreeLeafInfo Info;
  const GIMatchDag &MatchDag;
  /// The association between GIMatchDagInstr* and GIMatchTreeInstrInfo.
  /// The primary reason for this members existence is to allow the use of
  /// InstrIDToInfo.lookup() since that requires that the value is
  /// default-constructible.
  DenseMap<const GIMatchDagInstr *, GIMatchTreeInstrInfo> InstrNodeToInfo;
  /// The instruction information for a given ID in the context of this
  /// particular leaf.
  DenseMap<unsigned, GIMatchTreeInstrInfo *> InstrIDToInfo;
......

here I is a tuple composed of GIMatchTreeBuilderLeafInfo and BitVector, must be a big obj.

skan accepted this revision.May 28 2023, 7:19 PM

LGTM

This revision is now accepted and ready to land.May 28 2023, 7:19 PM
This revision was automatically updated to reflect the committed changes.