[lld] r243232 - ELF2: Avoid calling std::sort to make output deterministic.

Duncan P. N. Exon Smith dexonsmith at apple.com
Mon Jul 27 17:00:09 PDT 2015


> On 2015-Jul-27, at 16:37, Rui Ueyama <ruiu at google.com> wrote:
> 
> On Mon, Jul 27, 2015 at 4:18 PM, Chandler Carruth <chandlerc at google.com> wrote:
> On Mon, Jul 27, 2015 at 2:02 PM Rui Ueyama <ruiu at google.com> wrote:
> On Mon, Jul 27, 2015 at 12:01 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com> wrote:
> 
> > On 2015-Jul-25, at 17:50, Rui Ueyama <ruiu at google.com> wrote:
> >
> > Author: ruiu
> > Date: Sat Jul 25 19:50:15 2015
> > New Revision: 243232
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=243232&view=rev
> > Log:
> > ELF2: Avoid calling std::sort to make output deterministic.
> 
> Using `std::stable_sort` should also make it deterministic.  Is that
> better than switching to a `std::map`?
> 
> Yeah, I think the new code is slightly more readable than the previous one. At least it's shorter than before.
> 
> 
> "Yeah" doesn't seem to agree with the rest of your email, so I'm left confused.
> 
> Why wouldn't switching to std::stable_sort be better?
> 
> std::map is horribly slow in essentially every way, and so I struggle to believe it is the best tool for the job even if the code using it is very compact.
> 
> std::stable_sort would actually be slower than the new code. What we are doing here is bin chunks into a few sections, so putting everything into one array and stable-sorting them and scanning them again doesn't make much sense. We used std::stable_sort in COFF before but I removed that in r239292 for that reason.

Looking again, you're just using `Map` as a cache here, right?  If
you're not iterating over the map, you can probably skip the
red-black tree by using a (Small?)DenseMap.

> 
> 
> >
> > Modified:
> >    lld/trunk/ELF/Writer.cpp
> >
> > Modified: lld/trunk/ELF/Writer.cpp
> > URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=243232&r1=243231&r2=243232&view=diff
> 
> > ==============================================================================
> > --- lld/trunk/ELF/Writer.cpp (original)
> > +++ lld/trunk/ELF/Writer.cpp Sat Jul 25 19:50:15 2015
> > @@ -10,6 +10,7 @@
> > #include "Writer.h"
> > #include "Chunks.h"
> > #include "Driver.h"
> > +#include <map>
> >
> > using namespace llvm;
> > using namespace llvm::ELF;
> > @@ -58,24 +59,14 @@ void OutputSection::addChunk(Chunk *C) {
> >   Header.sh_size = Off;
> > }
> >
> > -static int compare(const Chunk *A, const Chunk *B) {
> > -  return A->getSectionName() < B->getSectionName();
> > -}
> > -
> > // Create output section objects and add them to OutputSections.
> > template <class ELFT> void Writer<ELFT>::createSections() {
> > -  std::vector<Chunk *> Chunks = Symtab->getChunks();
> > -  if (Chunks.empty())
> > -    return;
> > -  std::sort(Chunks.begin(), Chunks.end(), compare);
> > -
> > -  Chunk *Prev = nullptr;
> > -  OutputSection *Sec = nullptr;
> > -  for (Chunk *C : Chunks) {
> > -    if (Prev == nullptr || Prev->getSectionName() != C->getSectionName()) {
> > +  std::map<StringRef, OutputSection *> Map;
> > +  for (Chunk *C : Symtab->getChunks()) {
> > +    OutputSection *&Sec = Map[C->getSectionName()];
> > +    if (!Sec) {
> >       Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName());
> >       OutputSections.push_back(Sec);
> > -      Prev = C;
> >     }
> >     Sec->addChunk(C);
> >   }
> >
> >
> > _______________________________________________
> > llvm-commits mailing list
> > llvm-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 





More information about the llvm-commits mailing list