C++ Weekly - Ep 396 - emplace vs emplace_hint! What's the difference?

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024
  • ☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
    Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
    ► ndctechtown.co...
    Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
    ► cppunderthesea...
    github.com/lef...
    T-SHIRTS AVAILABLE!
    ► The best C++ T-Shirts anywhere! my-store-d16a2...
    WANT MORE JASON?
    ► My Training Classes: emptycrate.com/...
    ► Follow me on twitter: / lefticus
    SUPPORT THE CHANNEL
    ► Patreon: / lefticus
    ► Github Sponsors: github.com/spo...
    ► Paypal Donation: www.paypal.com...
    GET INVOLVED
    ► Video Idea List: github.com/lef...
    JASON'S BOOKS
    ► C++23 Best Practices
    Leanpub Ebook: leanpub.com/cp...
    ► C++ Best Practices
    Amazon Paperback: amzn.to/3wpAU3Z
    Leanpub Ebook: leanpub.com/cp...
    JASON'S PUZZLE BOOKS
    ► Object Lifetime Puzzlers Book 1
    Amazon Paperback: amzn.to/3g6Ervj
    Leanpub Ebook: leanpub.com/ob...
    ► Object Lifetime Puzzlers Book 2
    Amazon Paperback: amzn.to/3whdUDU
    Leanpub Ebook: leanpub.com/ob...
    ► Object Lifetime Puzzlers Book 3
    Leanpub Ebook: leanpub.com/ob...
    ► Copy and Reference Puzzlers Book 1
    Amazon Paperback: amzn.to/3g7ZVb9
    Leanpub Ebook: leanpub.com/co...
    ► Copy and Reference Puzzlers Book 2
    Amazon Paperback: amzn.to/3X1LOIx
    Leanpub Ebook: leanpub.com/co...
    ► Copy and Reference Puzzlers Book 3
    Leanpub Ebook: leanpub.com/co...
    ► OpCode Puzzlers Book 1
    Amazon Paperback: amzn.to/3KCNJg6
    Leanpub Ebook: leanpub.com/op...
    RECOMMENDED BOOKS
    ► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
    AWESOME PROJECTS
    ► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp...
    ► C++ Best Practices Forkable Coding Standards - github.com/cpp...
    O'Reilly VIDEOS
    ► Inheritance and Polymorphism in C++ - www.oreilly.co...
    ► Learning C++ Best Practices - www.oreilly.co...

Комментарии • 26

  • @X_Baron
    @X_Baron 11 месяцев назад +6

    As is often the case, the names of emplace and try_emplace are the wrong way around, kind of. Emplace can try to construct the element and then throw away the result if the key already exists, while the newer try_emplace doesn't even try if the key exists. 😄

    • @sqlexp
      @sqlexp 10 месяцев назад

      I think it has to do with move semantics. If you specifically use std::move on an argument, emplace will always turn it into the empty state if the argument's type supports that, whereas try_emplace will not change it if the key already exists. If the argument is already an rvalue without using std::move, you probably will not observe the difference.

  • @ysakhno
    @ysakhno 11 месяцев назад +6

    Would be interesting to see the performance comparison between 'just plain' putting into map vs emplacing into map (without any hints).

  • @MyCodingDiary
    @MyCodingDiary 11 месяцев назад +1

    Your videos are a constant source of inspiration for me. Please keep them coming!

  • @danielrhouck
    @danielrhouck 11 месяцев назад +8

    I’m surprised and disappointed that I can’t get compilers to complain about making a string with `'a'` 42s. Not with `-Wconversiοn` or even Clang’s `-Weverything`

    • @danielrhouck
      @danielrhouck 11 месяцев назад +3

      Looking into this more, it’s because the compiler knows 42 fits in a char; if I use `argc` there it gives a warning. But even given that, using a character literal or explicit type suffix (putting `uz` on 42 in this case) should be enough to trigger it.

  • @GeorgeTsiros
    @GeorgeTsiros 11 месяцев назад

    You're continuously getting better at this. Rock on 🤘

  • @FreeDownloadHere
    @FreeDownloadHere 11 месяцев назад

    Thanks for the video!

  • @lassdasi
    @lassdasi 11 месяцев назад +3

    Compiler Explorer has a dark theme. Is it possible to switch the C++ docs/references also to a dark theme? (Maybe "dark reader" Addon)

    • @anon_y_mousse
      @anon_y_mousse 11 месяцев назад

      Totally agreed, and I use Dark Reader and it does indeed work on cppreference.

  • @kamilsullin9588
    @kamilsullin9588 7 месяцев назад

    I tested in Quick Bench various combinations of pairs to check how much more effective emplace is than insert, to my surprise insert almost always won (probably due to various compiler optimizations), I did it only for simple types (std::pair) as well as for complex types (std::pair). I understand that the compiler is able to optimize the creation (and placement in the container) of such elements, but it is strange that the optimizations outperform direct emplace. This makes me wonder whether to use an insert

    • @cppweekly
      @cppweekly  6 месяцев назад +1

      I still prefer to use emplace when it's what I actually mean - when I have a set of parameters and want to construct the object in-place.
      If I already have an object of the correct type, I use insert or push_back.

  • @az-kalaak6215
    @az-kalaak6215 11 месяцев назад +2

    useful it was! Do this mean we should always use emplace_hint (in the same way emplace replaced insert) or is there any benefit of using raw emplace?

    • @primevst
      @primevst 11 месяцев назад +7

      I suspect it may be more performant on really big tables. lower_bound (used in emplace) is basically just a binary search, and they work really well on big collections. On small collections they may just be causing extra overhead.
      I'm personally sticking with emplace, as I also don't know what the future holds. Maybe they fix this 'performance issue' at some point with a simple if statement at the element count

    • @brunorcabral
      @brunorcabral 11 месяцев назад +1

      The performance comes from the ordered data. The hint with the wrong start position is as bad as emplace, so, unordered data would be hint badly as well.

    • @cppweekly
      @cppweekly  11 месяцев назад +1

      Agreeing with the other replies - I would still default to emplace unless you know that you have a reasonable _hint to give it.

  • @AndreasBeham
    @AndreasBeham 11 месяцев назад

    Don't know if it fits the topic, but e.g. constructing a vector that houses a pair of tuples: Using push_back, one can write vec.push_back({ { /*tuple elements*/ }, { /*tuple elements*/ } }). While with emplace, it's more cumbersome: vec.emplace_back(std::forward_as_tuple(/*tuple elements*/), std::forward_as_tuple(/*tuple elements*/)). The first almost has python-like simplicity, the second is slightly more effcient, but not as simple. Ignoring the decision on whether a vector of a pair of tuples is meaningful, what would you choose?

  • @Omnifarious0
    @Omnifarious0 11 месяцев назад

    8:40 - Given that there was likely a script involved here, did you emplace this discussion of emplace at the end of your discussion?

  • @PaulMetalhero
    @PaulMetalhero 11 месяцев назад +1

    try_emplace() rules
    Question: insert_or_assign does not invoke emplace at all?

    • @carstenm.2897
      @carstenm.2897 11 месяцев назад

      Why is there no emplace_or_assign()?

    • @PaulMetalhero
      @PaulMetalhero 11 месяцев назад

      that would be great@@carstenm.2897

  • @BartTrojanowski
    @BartTrojanowski 11 месяцев назад

    regarding quick-bench ... does it still work? I push "run benchmark" and nothing happens.

    • @benjaminshinar9509
      @benjaminshinar9509 11 месяцев назад

      same thing happened to me.
      I just copied the code to another instance of quick-bench and ran it there.

    • @cppweekly
      @cppweekly  11 месяцев назад

      Strange...

  • @gonzajuarez4918
    @gonzajuarez4918 11 месяцев назад

    damn