This article was written in collaboration with Neil Mitchell, a Software Engineer in the Developer Infrastructure organization at Facebook.
The Rust library Gazebo contains a collection of well-tested Rust utilities in the form of standalone modules. In this series of blog posts, we will cover some of the modules that make up the Gazebo library. In today’s blog, we will cover the Dupe trait. This blog is a part of our Rust Nibbles series, where we go over the various Rust libraries we have open-sourced to learn more about what motivated their creation and how one can use them.
In Rust, there are two related traits for “duplicating” a value – Copy and Clone. In Gazebo we’ve introduced a third such trait, we call Dupe, which is available from the Gazebo Prelude. Let’s go through these three traits, why we think there is value in a third such trait, and how we use it in practice.
The Copy trait represents values which can be duplicated by simply copying their bit pattern. It’s so cheap that in most cases the Rust compiler will automatically copy the values for you. In contrast, the Clone trait represents values which call a custom clone() method to duplicate themselves. As two examples of things that implement the Clone trait:
- The Rust reference-counted type Rc (and similar Arc, the atomic reference count) both support Clone, and their clone implementation bumps a reference count.
- The type Vec and String both support Clone, which allocate memory and copy the contents across, meaning that a type such as Vec<String> will typically do 1 allocation for the vector, and 1 for each string it contains.
While the Copy trait is quite well constrained, the Clone trait covers a massive variety of performance characteristics. In most cases, if you see yourself cloning a Vec<String> a lot, there’s a good chance you should put it in an Arc and make the cloning vastly cheaper. The problem comes when reading the code. At code review, we might see:
let xs = ys.clone();
That might allocate 2GB of memory. Or it might increment a counter. Figuring out which requires a lot of context. One possible solution is to explicitly use Arc::clone, e.g.:
let xs = Arc::clone(ys);
This pattern has the advantage of making the cost clear, but the disadvantage is that as soon as you wrap a type inside a struct, you can no longer call Arc::clone on it, even though the cost of cloning remains the same. This pattern breaks abstraction.
Our solution is to introduce the Dupe trait. It provides a dupe method that just forwards to clone, but we only define Dupe in cases where the cost of copying is “trivial”. In practice, that means zero allocation, and not too many reference count increments. Alas, the definition is a little imprecise, with no definition of “too many”, but it has served us well. Now, for types that implement Dupe, we see:
let xs = ys.dupe();
The dupe function is a clear sign during code review that we don’t need to worry. Having significantly reduced the number of calls to clone, we can pay much more attention to them. The code review experience has improved, letting us focus on the right things.
Gazebo also provides a Dupe derive macro, so you can write:
use gazebo::prelude::*; #[derive(Clone, Dupe)] struct MyArc(Arc<String>);
We think of the Dupe trait as part of the standard package of things that should always be available, so we included it in the gazebo::prelude module, but it can also be found standalone at gazebo::dupe::Dupe, for those who don’t want to use the rest of the Gazebo Prelude.
We hope that this blog helps you understand the Dupe trait, how to use it and gives you good insight into what it does. Look out for our next blog in this series, where we discuss the Variants module, which provides tools for working with the different variants of an enum.
Be sure to check out our previous blog in the Gazebo series to learn more about the various features the Gazebo library has to offer –
Gazebo – Prelude
About the Rust Nibbles series
We at Facebook believe that Rust is an outstanding language that shines in critical issues such as memory safety, performance and reliability. We joined the Rust Foundation to help contribute towards the growth, advancement and adoption of Rust, and towards sustainable development of open source technologies and developer communities across the world.
This blog is a part of our Rust Nibbles series, where we go over the various Rust libraries we have open-sourced to learn more about what motivated their creation and how one can use them. We hope that this series helps you create amazing projects by using these libraries and encourages you to try them out.
Facebook-Meta Earns the ‘Worst Company of 2021’ Title in This Survey
Facebook parent Meta has been named the Worst Company of the Year (2021) by Yahoo Finance respondents. According to the publication, an “open-ended” survey was published on Yahoo Finance on December 4 and 5, where 1,541 respondents participated. Facebook received 8 percent of the write-in vote, but respondents were seemingly mad about the Robinhood trading app as well. Electric truck startup Nikola, which was named last year’s worst company by the same publication also faced respondents ire.
Yahoo Finance even highlights, “At the same time, some critics, including conservatives, say Facebook over-policed the platform’s speech and stifled their voices.” Critics also blame Facebook and other social media platforms for not curbing hate speech that led to Capitol Building riots.
However, around 30 percent of Yahoo Finance readers said that Facebook or Meta could redeem itself. One respondent suggested that the company could issue a formal apology for negligence and donate a sizable amount of its profits to a foundation to help reverse its harm.
On the other hand, respondents chose Microsoft as the Company of the Year (2021). The Satya Nadella-led company touched the trillion-mark this year and introduced notable upgrades. The most notable is the Windows 11 OS update that succeeds Windows 10.
Facebook pays 1.7 Cr fine to Russia after failing to delete content Moscow deems illegal
In the latest legal tussle with Russia over controversial social media regulation laws, Facebook paid 17 million roubles (Rs 1.7 Crore) for failing to remove content deemed illegal by Moscow. With a threat of potential larger fines looming, Facebook parent company Meta, owned by Mark Zuckerberg, is scheduled to face court next week over repeated violations of Russian legislation on content, Interfax News Agency reported. As per the latest updates, the social media giant could be fined a percentage of its annual revenue.
In October, Moscow sent state bailiffs to enforce the collection of 17 million roubles. Meanwhile, as per Interfax report citing a federal bailiffs’ database, on Sunday, there were more enforcement proceedings against the company. Apart from the popular social media app, Telegram has also paid 15 million roubles in fines for failing to comply with the Russian social media legislations that came into force in 2016.
Facebook pays $53k to Russia for refusing controversial social media laws
It is pertinent to mention that Facebook has locked horns with Moscow earlier in November, resulting in it paying 4 million roubles ($53,000) over its refusal to adhere to Russian data localisation laws, the Moscow Times reported. The Moscow court on November 25 had said that Facebook paid the fine levied in February, following which all proceedings against the US-based social media giant. The payment comes against the litigation filed against the company in 2018, alongside Twitter. The tech companies were also forced to pay an additional 3000 rubles ($40) for failing to comply with user data sharing rules as per the law. The Russian authorities have also previously blocked LinkedIn, owned by Microsoft, for failing to abide by the laws.
Russian social media laws
As per Moscow Times, under the Russian social media regulation laws, all foreign technology companies are required to store data related to Russian customers and users on servers located in Russia. Additionally, the Russian tech companies will also have to share encryption data with the federal authorities as well as record user calls, messages and civil society group conversation records. The apparatus is said to be a severe breach of privacy rights and unfettered back-door access to personal data that could be used to harass Kremlin critics.
Facebook Messenger Is Launching a Split Payments Feature for Users to Quickly Share Expenses
Meta has announced the arrival of a new Split Payments feature in Facebook Messenger. This feature, as the name suggests, will let you calculate and split expenses with others right from Facebook Messenger. This feature essentially looks to bring an easier method to share the cost of bills and expenses — for example, splitting a dinner bill with friends. Using this new Split Payment feature, Facebook Messenger users will be able to split bills evenly or modify the contribution for each individual, including their own.
The company took to its blog post to announce the new Split Payment feature in Facebook Messenger. 9to5Mac reports that this new bill splitting feature is still in beta and will be exclusive to US users at first. The rollout will begin early next week. As mentioned, it will help users share the cost of bills, expenses, and payments. This feature is especially useful for those who share an apartment and need to split the monthly rent and other expenses with their mates. It could also come handy at a group dinner with many people.
With Split Payments, users can add the number of people the expense needs to be divided with and, by default, the amount entered will be divided in equal parts. A user can also modify each person’s contribution including their own. To use Split Payments, click the Get Started button in a group chat or the Payments Hub in Messenger. Users can modify the contribution in the Split Payments option and send a notification to all the users who need to make payments. After entering a personalised message and confirming your Facebook Pay details, the request will be sent and viewable in the group chat thread.
Once someone has made the payment, you can mark their transaction as ‘completed’. The Split Payment feature will automatically take into account your share as well and calculate the amount owed accordingly.
Tasneem Akolawala is a Senior Reporter for Gadgets 360. Her reporting expertise encompasses smartphones, wearables, apps, social media, and the overall tech industry. She reports out of Mumbai, and also writes about the ups and downs in the Indian telecom sector. Tasneem can be reached on Twitter at @MuteRiot, and leads, tips, and releases can be sent to email@example.com.