The port itself is very old and therefore very outdated.
6,940 karma · joined September 5, 2009
Working on https://filerion.com/, a web based file manager (s3, Dropbox and more).
I created https://www.sumatrapdfreader.org
The port itself is very old and therefore very outdated.
It works in msvc but as someone pointed out, it was a typo and was meant to be (void*) cast.
I wrote it to share my implementation and my experience with it.
SumatraPDF compiles fast (relative to other C++ software) and is smaller, faster and uses less resources that other software.
Is it because I wrote Func0 and Func1 to replace std::function? No.
Is it because I made hundreds decisions like that? Yes.
You're not wrong that performance wins are miniscule.
What you don't understand is that eternal vigilance is the price of liberty. And small, fast software.
Smaller size at runtime (uses less memory).
Smaller generated code.
Faster at runtime.
Faster compilation times.
Smaller implementation.
Implementation that you can understand.
How is it worse?
std::function + lambda with variable capture has better ergonomics i.e. less typing.
There are numerous differences between my Func0 and Func1<T> and std::function<>.
Runtime size, runtime performance, compilation speed, understandability of the code, size of the source code, size of the generated code, ergonomics of use.
My solution wins on everything except ergonomics of use.
LLVM has a small vector class.
When asked for comment, pjmlp said: "Another example of NIH, better served by using the standard library".
All others use a pointer to an object that exists anyway. For example, I have a class MyWindow with a button. A click callback would have MyWindow* as an argument because that's the data needed to perform that action. That's the case for all UI widgets and they are majority uses of callbacks.
I could try to get cheeky and implement similar optimization as Func0Fat where I would have inline buffer on N bytes and use it as a backing storage for the struct. But see above for why it's not needed.
As to benchmarking: while I don't disagree that benchmarking is useful, it's not the ace card argument you think it is.
I didn't do any benchmarks and I do no plan to.
Because benchmarking takes time, which I could use writing features.
And because I know things.
I know things because I've been programming, learning, benchmarking for 30 years.
I know that using 16 bytes instead of 64 bytes is faster. And I know that likely it won't be captured by a microbenchmark.
And even if it was, the difference would be miniscule.
So you would say "pfft, I told you it was not worth it for a few nanoseconds".
But I know that if I do many optimizations like that, it'll add up even if each individual optimization seems not worth it.
And that's why SumatraPDF can do PDF, ePub, mobi, cbz/cbr and uses less resources that Windows' start menu.
There are 2 aspects to this: programmer ergonomics and other (size of code, speed of code, compilation speed, understandability).
Lambdas with variable capture converted to std::function have best ergonomics but at the cost of unnamed, compiler-generated functions that make crash reports hard to read.
My Func0 and Func1<T> approach has similar ergonomics to std::bind. Neither has the problem of potentially crashing in unnamed function but Func0/Func1<T> are better at other (smaller code, faster code, faster compilation).
It's about tradeoffs. I loved the ergonomics of callbacks in C# but I working within limitations of C++ I'm trying to find solutions with attributes important to me.
The problem with that is that for every type of callback you need to create a base class and then create a derived function for every unique use.
That's a lot of classes to write.
Consider this (from memory so please ignore syntax errors, if any):
class ThreadBase {
virtual void Run();
// ...
}
class MyThread : ThreadBase {
MyData* myData;
void Run() override;
// ...
}
StartThread(new MyThread());
compared to: HANDLE StartThread(const Func0&, const char* threadName = nullptr);
auto fn = MkFunc0(InstallerThread, &gCliNew);
StartThread(fn, "InstallerThread");
I would have to create a base class for every unique type of the callback and then for every caller possibly a new class deriving.This is replaced by Func0 or Func1<T>. No new classes, much less typing. And less typing is better programming ergonomics.
std::function arguably has slightly better ergonomics but higher cost on 3 dimension (runtime, compilation time, understandability).
In retrospect Func0 and Func1 seem trivial but it took me years of trying other approaches to arrive at insight needed to create them.
- better call stacks in crash reports
- smaller and faster at runtime
- faster compilation because less complicated, less templated code
- I understand it
So there's more to it that just that one point.Did I loose useful attributes? Yes. There's no free lunch.
Am I going too far to achieve small, fast code that compiles quickly? Maybe I do.
My code, my rules, my joy.
But philosophically, if you ever wonder why most software today can't start up instantly and ships 100 MB of stuff to show a window: it's because most programmers don't put any thought or effort into keeping things small and fast.
I can't even read it.
That's the fundamental problem with C++: I've understood pretty much all Go code I ever looked at.
The code like the above is so obtuse that 0.001% of C++ programmers is capable of writing it and 0.01% is capable of understanding it.
Sure, I can treat it as magic but I would rather not.
But I do know the code I write and you're wrong about performance of Func0 and Func1. Those are 2 machine words and all it takes to construct them or copy them is to set those 2 fields.
There's just no way to make it faster than that, both at runtime or at compile time.
The whole point of this implementation was giving up fancy features of std::function in exchange for code that is small, fast (both runtime and at compilation time) and one that I 100% understand in a way I'll never understand std::function.
But you didn't respond: which language should I use?
Go didn't exist when I started SumatraPDF.
And while I write pretty much everything else in Go and love the productivity, it wouldn't be a good fit.
A big reason people like Sumatra is that it's fast and small. 10 MB (of which majority are fonts embedded in the binary) and not 100MB+ of other apps.
Go's "hello world" is 10 MB.
Plus abysmal (on Windows) interop with C / C++ code.
And the reason SumatraPDF is unportable to mac / linux is not the language but the fact that I use all the Windows API I can for the UI.
Any cross-platform UI solution pretty much require using tens of megabytes of someone else's reimplementation of all the UI widgets (Qt, GTK, Flutter) or re-implementing a smaller subset of UI using less code.
Today, if I was starting from scratch, I would try zig or odin or maybe even Go.
But SumatraPDF started 15 years. There was nothing but C++. And a much different C++ that C++ of today.
Plus, while my own code is over 100k lines, external C / C++ libraries are multiple of that so (easy) integration with C / C++ code is a must.
Two, my main objective is extreme simplicity and understandability of the code.
I explicitly gave up features of std::function for smaller code that I actually understand.
fu2 seems to be "std::function but more features".
Somehow my blog server got overwhelmed and requests started taking tens of seconds. Which is strange because typically it's under 100ms (it's just executing a Go template).
It's not a CPU issues so there must be locking issue I don't understand.
Also, it's still America. Good luck to anyone trying to "force" SpaceX, a private company, to do anything they don't want.
Apparently, here's the data:
California is losing its grip on the film industry as Canada, the UK, and Australia outspend the U.S. on production subsidies.
LA’s soundstage occupancy dropped to 90% pre-strikes, while Warner Brothers and Netflix ramp up overseas filming.
Even a $3.75 billion tax credit boost might not be enough to keep Hollywood on top.
Meanwhile, Vancouver is adding 20 new soundstages, and UK productions hit a record $7 billion.
Here's Ben Affleck talking about this: https://nypost.com/2025/04/18/entertainment/ben-affleck-crit..., https://x.com/MarioNawfal/status/1913449981643456612
Here's Adam Scott and Rob Lowe talking about it: https://x.com/medx0/status/1919178518031896903
Basically it seems that movie production and jobs created by it moved outside of US in a massive way.
Hence the leftist tarrifs meant to reverse that and bring movie production back in US
VAT is bad for US because a car made in US is 22% more expensive when sold in UK than a car made in UK sold in US. VAT makes it harder for US car company to sell in UK. It reduces number of US jobs making cars.
UK giving tax breaks to US companies also reduces number of US jobs because a catering business in Los Angeles loses a job to a catering business in London. So does a carpenter, an extra etc.
It's all simple to understand, consistent and frankly very leftist.
It'll help thousands of Americans that used to be employed by said studios but were fired not because they couldn't do the job but because the greed of studio execs made them move production outside of the US, playing financial arbitrage games against the interest of the US.
Trump is trying to reverse this by making movies outside of the US, especially by US companies, more expensive. The goal here is not some statement about Hollywood but to bring movie production jobs back to US. The extras, the people who build sets, people who make food for actors etc. And jobs bring tax revenue and grow GDP, which helps every american.
> Entering the United States illegally is not classified as a civil offense; it is a criminal offense. Under U.S. law, specifically under Title 8 of the U.S. Code, unauthorized entry into the country is considered a misdemeanor. The specific statute is 8 U.S.C. § 1325
That's why there are no tariffs on Russia: there's no trade, there's nothing to tariff.
I don't see how you concluded that US is currently "in service of Russia".
But yeah, knowing that $100 in bitcoin today might be worth $150 in a year does deter buying non-essential stuff. Why is that a bad thing?
Personally, It won't discourage me from buying a hamburger when I'm hungry, but maybe it would make me hold on to my iPhone for a year or 2 longer but you can do as you please and spend every cent you make.
If you have excess cash, be it $100 or $1 million and keep it in dollars, you'll loose ~8% a year, which is the historical yearly rate of US money printing which is the real inflation (not the fake 2%; check the inflation of health care plans, college education or housing).
To preserve that value you can do simple / safe (bank savings account rate, bonds, S&P 500 etc.) and underperform that 8%.
If you want to match / outperform that 8% (i.e. not loose money over time) you have to become a successful (better than average) stock investor or invest in real estate with all the hassles it involves (like managing tenants).
Or you can invest in Bitcoin, which, historically, over 4 years, outperforms dollars, gold, us stocks.
Bitcoin outperforms dollars because US can (and does) print as many dollars as it wants, whenever it wants. Bitcoin has a fixed limit of 21 million bitcoins. You can't print more bitcoin than that therefore you can't devalue bitcoin.
It's even worse in countries like Niger or Argentina because they print even more money than US, causing even greater inflation, making it ever more important to park assets in non-inflationary asset.
If you self custody bitcoin, you remove the risk your money will get confiscated by the government or bank.
In some countries you can already pay directly in Bitcoin.
In US you can use Strike to basically store you net worth in Bitcoin and convert to cash (dollars) to pay for stuff.
Bitcoin has none of those problems.
I object to calling it "debt" because you can't tell. And my greater point was that buying a liquid asset is neutral to, let's call it, "net worth" of country.
The government does "speculate" on gold. That is the closest equivalent. Gold is a hedge against declining value of a dollar, just as Bitcoin would be.
This is Status Quo Bias.
If you accept that Bitcoin, like gold, is a hedge against devaluing currency, then it makes sense for China or US to stock up on Bitcoin just like they stock up on gold.
Unlike gold, Bitcoin has a non-zero chance to supplant (or at least become a viable alternative to) US dollar as a global currency. In which case it's strategic for a country to own as much of such asset as possible.
But I do invest in Bitcoin because you can't.
Bitcoin is scarce, your coinz aren't.
Do you get it now?
To expand on my previous response: econ 101 teaches us that the price of any non-scarce product will converge to the cost of production plus some small profit margin.
Tulip is not a scarce asset because you can make more tulips. If cost to produce a tulip is $1 dollar and the price spikes to $100 due to demand outstripping supply, people will be incentivized to grow more tulips.
At some point they'll grow enough tulips for supply to meet the demand and they will have to sell tulips at merely small profit. In fact, some will likely have to sell tulips at a loss due to overproduction.
Mona Lisa doesn't have this problem. There's only one Mona Lisa so as long as demand goes up, the price goes up.
Bitcoin doesn't have this problem. There's a hard limit of 21 million bitcoins that will ever exist. As long as demand for bitcoin goes up, the price goes up.
> El Salvador is not recommending Bitcoin and found low interest in actual use.
El Salvador is not a person.
The president of El Salvador is hardcore Bitcoin believer. El Salvador, the country, is stacking as much Bitcoin as they can, despite pressure from IMF that tries (I wonder why?) to stop them.
> Madoff also had a fund that could return very well
Except he didn't. He was lying about returns of his investments and it crashed and burned when the truth came out.
Blockchain is fully transparent. Every Bitcoin transaction ever made is recorded in immutable database and you get to read the full ledger, from the day it was created.
There is no way to lie about Bitcoin transaction. The current price is decided by an auction with millions of participants.
Countries and banks (including central banks) are still buying gold as a hedge against dollar and other currencies.
For example, "from November 2022 to April 2024, China reported adding about 314 tons to its reserves, bringing the official total to 2,264 tons by mid-2024".
The logic for holding Bitcoin is the same as the logic for holding gold: a hedge against dollar and other currencies.