Unbearably slow for big texts
Running an assert_eq on a large string is extremely slow with this library enabled.
I noticed it when I added a placeholder assertion before starting some coding. A regular assertion takes no time at all. With pretty-assertions enabled, the test does not return in 5 minutes where I got tired of waiting and killed it.
This is the code:
let result = "A very long string....";
assert_eq!("foo", result);
And this is the test output (where you can see the actual string):
running 1 test
test markdown::test_prefix_with_necessary_spaces ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
running 20 tests
test ending_space_is_trimmed ... ok
test blockquote ... ok
test ignoring_scripts ... ok
test image ... ok
test ignoring_head ... ok
test newline ... ok
test bold ... ok
test emphasize ... ok
test link ... ok
test plaintext ... ok
test ignoring_styles ... ok
test newlines_are_ignored ... ok
test lines_with_empty_spaces_are_killed ... ok
test ordered_list ... ok
test paragraph ... ok
test unordered_list ... ok
test ordered_and_unordered_mixed ... ok
test unordered_more_complex_list ... ok
test ordered_more_complex_list ... ok
test pixel_email_is_readable ... FAILED
failures:
---- pixel_email_is_readable stdout ----
thread 'pixel_email_is_readable' panicked at 'assertion failed: `(left == right)`
left: `"foo"`,
right: `"Beställ innan kl 09.00 20/12 så garanterar vi leverans innan jul! Vi hjälper dig med flera julklappstips som passar perfekt till kökshjälten![](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845929&s=1)[Läs online](http://klick.bagarenochkocken.se/mail/OBS/61yf0003019mpSLyaDg782919919)\u{a0} \u{a0} \u{a0} \u{a0} \u{a0} \u{a0} \u{a0}\u{a0}\u{a0}[Avsluta prenumeration](https://bagarenochkocken.se/avregistrering-nyhetsbrev/?originalId=564907&mid=782919919&guid=61yf0003019mpSLyaDg)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845930&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845931&s=1)[NYHETER](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845932&s=1)[PRESENTTIPS](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845933&s=1)[PRESENTKORT](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845934&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845935&s=1)[Julklappar för en magisk jul 🎄](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845936&s=1)Ännu fler julklappstips! Kanske en komplett servis för kommande flytt, en saltsten för matnörden eller köksknivar av hög kvalitet. Beställ innan kl 09.00 20/12 så garanterar vi leverans innan jul![Se alla julklappstips](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845937&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845938&s=1)[Rosenthal Middagsset](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845939&s=1)Perfekta julklappen till någon som flyttar in i nytt!Nu 995.- (Rek. pris 1869.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845940&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845941&s=1)[Mac Chef Knivset](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845942&s=1)Två knivskarpa knivar som ligger perfekt i handen.Nu 899.- (Rek. pris 1375.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845943&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845944&s=1)[Stelton Emma Vattenkokare](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845945&s=1)En modern och snygg vattenkokare till julens alla tekvällar.Nu 899.- (Rek. pris 1499.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845946&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845947&s=1)[Weber Q2200](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845948&s=1)Grillarnas vardagshjälte - snabb uppvärmning och bra värmespridning.Nu 3995.- (Rek. pris 4995.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845949&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845950&s=1)[Georg Jensen Bestickset ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845951&s=1)Tidlös design i 24 delar.Nu 2499.- (Rek. pris 3899.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845952&s=1)\n\nSpiegelau Beer ClassicPerfekt till ölälskaren.Nu 269.- (Rek. pris 399.-)Köp\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845953&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845954&s=1)[Tefal Stekpanna & Kastrull](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845955&s=1)Nu 649.- (Rek. pris 1249.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845956&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845957&s=1)[Le Creuset Gryta med tillbehör](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845958&s=1)Nu 1995.- (Rek. pris 3139.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845959&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845960&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845961&s=1)[Georg Jensen Ljusstake](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845962&s=1)En ljusstake som gör sig fina hela året.Nu 499.- (Rek pris. 849.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845963&s=1)\n\nKitchenIQ KnivslipKlarar Europeiska & Japanska knivar.Nu 495.- (Rek. pris 659.-)Köp\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845964&s=1)[Lyngby Glas Champagneglas ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845965&s=1)Utmärkt för både dryck och dessert.Nu 199.- (Rek. pris 249.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845966&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845967&s=1)[Wüsthof Classic Kockkniv](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845968&s=1)En kockkniv som aldrig går ur tiden.Nu 699.- (Rek. pris 905.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845969&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845970&s=1)[Weber E-410 & Slowjucier](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845971&s=1)Ett paket med limiterad grill & slowjucier från OBH.Nu 10749.- (Rek. pris 15498.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845972&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845973&s=1)[Dualit Vattenkokare](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845974&s=1)Fantastiska vattenkokare med en traditionell form och modern design. Nu 999.- (Rek. pris 1999.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845975&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845976&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845977&s=1)[Mareld Bordskorg med kvarnset](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845978&s=1)Nu 649.- (Rek. pris 985.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845979&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845980&s=1)[OBH Nordica Vakuumförpackare](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845981&s=1)Nu 3990.- (Rek. pris 9990.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845982&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845983&s=1)[Julkalender 🎁](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845984&s=1)Varje dag fyller på med fler erbjudanden i vår julkalender - missa inte dagens lucka.[Till vår julkalender](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845985&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845986&s=1)[Dukat för jul 🥂](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845987&s=1)Ta fram ditt favoritporslin i jul och duka upp till fest! Oavsett om det är en liten eller stor bjudning finns det anledning att skämma bort köksbordet.[Se mer juldukning](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845988&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845989&s=1)[WMF Philadelphia Bestickset](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845990&s=1)Nu 1279.- (Rek. pris 3420.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845991&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845992&s=1)[Kitchen Craft Serveringsbräda](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845993&s=1)Nu 399.- (Rek. pris 449.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845994&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845995&s=1)[Iittala Essence Rödvin](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845996&s=1)Nu 419.- (Rek. pris 699.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845997&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845998&s=1)[Eva Solo Dricksglas](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80845999&s=1)Nu 189.- (Rek. pris 299.-)[Köp](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846000&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846001&s=1)\n\n[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846002&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846003&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846004&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846005&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846006&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846007&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846008&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846009&s=1)[Alla våra bästa erbjudanden just nu - klicka här!](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846010&s=1)[Köksmaskiner](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846011&s=1)[Köksknivar ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846012&s=1)[Baktillbehör ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846013&s=1)[Köksredskap ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846014&s=1)[Dukade bordet ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846015&s=1)[Kokkärl & stekpannor ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846016&s=1)[Vitvaror ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846017&s=1)[Heminredning ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846018&s=1)[Utomhus ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846019&s=1)[Varumärken ](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846020&s=1)[Presentkort](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846021&s=1)[Presenttips](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846022&s=1)\n\nKöksredskap & design på nätetBagaren och Kocken säljer köksredskap, köksmaskiner och designprodukter på nätet och i butik i Göteborg. Vi har över 10 000 produkter på vårt egna lager i Göteborg och kan därför leverera snabbt, oftast från oss samma dag eller dagen efter att vi fått din beställning.Kundtjänst: 031-25 22 22 (mån-fre 10-16)[bagarenochkocken.se](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846023&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846024&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846025&s=1)[](http://klick.bagarenochkocken.se/mail/RLS?mid=782919919&guid=61yf0003019mpSLyaDg&lid=80846026&s=1)[](mailto:[email protected])*Copyright © 2017 Bagaren och Kocken AB, Alla rättigheter reserverade.*\nDetta är ett nyhetsbrev skickat av Bagaren och Kocken AB. Du får detta mail eftersom du är registrerad som kund hos oss eller har valt att prenumerera på vårt nyhetsbrev.\n\n[Ta bort mig från listan](https://bagarenochkocken.se/avregistrering-nyhetsbrev/?originalId=564907&mid=782919919&guid=61yf0003019mpSLyaDg)\n\n[]() "`', markdown.rs:288:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.
failures:
pixel_email_is_readable
test result: FAILED. 19 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
Yes, there is some additional work in calculating the changeset, but it should not be that slow. Most importantly, the work is only done, if the assertion fails.
It would be interesting to determine if this is some kind of infinite loop or not.
Does anyone want to investigate this? :-)
I have the same problem. I use this crate to help with comparing my generated code to the expected one in my configure_me crate. I already hit the 60 second limit Rust imposes.
This is my first time tracing down a performance issue but it looks like the majority of the slowdown is caused by the difference.rs crate and not by rust-pretty-assertions:

I'll have a look at the diff crate now and see if it can be improved in some way.
The example I used, in case someone want's to replicate it:
use difference::{Changeset};
fn main() {
let x = "abc".repeat(2000);
let y = "abd".repeat(2000);
let c = Changeset::new(&x, &y, "");
println!("{}", c);
}
As a workaround, you can also add
[profile.dev]
opt-level = 2
to your Cargo.toml
It's not only extremely slow but also has a memory leak. On two strings containing xml it took up 4GB before I canceled.
As of Rust 1.41, Cargo supports overriding build settings for specific dependencies, so you can compile the difference crate (which is what is slowing down pretty_assertions) with optimizations for the development profile by adding the following to your Cargo.toml:
[profile.dev.package.difference]
opt-level = 3
This way you don't have to compile your whole crate and all of its dependencies with optimizations when testing just to speed up difference.
Note that if your crate is part of a workspace, this needs to be added to the Cargo.toml of the workspace root instead of your specific crate, or it will be ignored otherwise.
After some investigations triggered by #51 into even worse performance with similar I did some deep diving into this particular usecase. Unfortunately it's very common for both this crate and my snapshot testing library that the diff will be entirely distinct. If that becomes large enough this triggers pathological cases in almost all diffing algorithms.
LCS which is what this crate uses needs to create a massive table which is also likely going to exhaust the available memory for large enough distinct inputs. Myers which is what similar performs even worse then LCS for this type of case. So why does git/or the unix diff utility not suffer from this? The answer is that they internally have some heuristics when the cost of the algorithm becomes too large. It also specifically detects cases where too many lines are distinct and explicitly discards them from the diffing algorithm.
So long story short: fixing this would require some more advanced heuristics in Rust diffing libraries but no library implemented those yet to the best of my knowledge (https://github.com/mitsuhiko/similar/issues/15). Since I have the same issue in insta I made the same workaround now that Google's diff-match-patch does: it implements a deadline of maximum wall time to be spent diffing. This works particularly well with Myer's diff where the diff stays somewhat reasonable even if you bail due to deadline hit.
difference has been replaced by diff, so the Cargo.toml hack from this comment should look like this:
[profile.dev.package.diff]
opt-level = 3
@Ch00k @jasongrlicky Thank you for this tweak - would you be interested in performing some benchmarks and adding a note to the documentation and/or readme via pull request?
cc @tommilligan