everyone please warm up your monads it's
time uh hasc is very competitive with c
and on some benchmarks it is faster how
is that possible with all that hcll does
on top of the raw C code how can it
possibly be faster in this episode I
talk about two advantages of hasal that
can make it faster than C so typically
this is what I say when it comes to this
type of stuff which is when someone says
X is faster than C what they're actually
saying is that I didn't write C that
good but I'm really good at writing this
one piece of code and so therefore C is
slower than Hull or X is faster than C
because I can't write good C that's what
it means okay it's like literally it is
it's literally the original skill issue
but I'm willing to hear it out I'm just
saying this is skill issue number one
this is your classic skill issue how is
hasell faster than C my name is Eric
Normand and I help people thrive with
functional programming so usually
languages are compared to C right it's
kind of like the standard Benchmark
language to say like oh this language is
only a factor of two slower than C right
most languages are slower than C by the
way every time you see someone do a
comparison of languages in C you know
what they do almost 100% of the time
they always do the worst version of it
right like you'll see like JavaScript is
only 1.5 times slower than C and it's
always doing a bunch of math equations
and you're just like dude the reality is
that your web server everything you do
is in a bunch of math equations is it
surprising that JavaScript can compete
on a small function with c yeah it's not
that surprising because it eventually
gets jit it gets turned into just like
as as best representation as the engine
can make to run this one small function
over and over again is there anything
impressive about that no it's not right
because you're because that's not what
happens what really happens is that you
have a piece you have an object that you
build you go and do an await you go and
get more information you add stuff to
the object you go and do another await
you come back you push some stuff into
arrays then you take that crazy object
and arrays and you turn it into a string
meanwhile every single response that
comes back not only do you parse its
Json you also send it to Zod to be
validated in which Zod is not fast it
then goes through and compares the
structure and Zod to ensure that it
actually is a correct object and
validates at runtime that its type is
the type expected to be and so of course
that's where all the slowness comes from
it does not come from a couple math
equations it comes from all the other
stuff because now that you've held on to
an object over two separate uh awaits
you've held on to the object long enough
that maybe it got promoted from the
nursery cach to the intergenerational
cash and maybe it goes from the
intergenerational one to the long-term
one you don't even know how long you
held on to that object and the next
thing you know now you have major gc's
all over the place causing large
slowdowns maybe you're running on a
multi-core machine but maybe you're not
running on a multi-core machine the
difference in performance on a non-
multicore machine versus a multi-core
machine is huge in JavaScript because
all the garbage collection optimizations
rely on multiple threads and when you
don't have that it just gets completely
destroyed in performance and so whenever
I hear these things that's what I think
of right is here is an unrealistic case
in which we can perform nearest the same
speed and um when especially when you
get into like the more higher level
languages you have this idea of like
well we lost a lot of speed but you
don't have to deal with all these
problems that c has like um you know you
don't have memory leaks because we have
a garbage collector you don't have to
deal with like when to when to free the
memory garbage collector Supremacy is a
real thing uh the fact that you don't
like there's there seems to be nothing
more exciting than garbage collector
plus Arenas right you get to control a
small amount of memory in a very fixed
way or it's just garbage collection like
to me that is like practically the
perfect level of of memory management
because for the most part you don't care
about most memory but there are some
memories in which you want to care about
and to me that is like that's that's
that's a real thing you want to because
then you can make the parts that you
need fast fast and the parts you don't
care about you don't solve by cloning
which ain't fast and you know just all
these niceties add up but it's at the
cost of speed but then you have
something like haskal that
is often faster than C in hat mentioned
and it's faster than C let's go it's the
ultimate
language the benchmarks it's very close
to see when it's not faster and it's not
like oh it's twice as slow no it's like
right there it's like within a few
percentage of c and very often it's on
the other side it's faster than C I'm
doubting so hard right now because the
only way I can possibly think hasell is
faster than C is if somehow they figured
out how to do things in less machine
instructions right that their
compilation is actually just genuinely
better comp I I first off I don't even
know if is hasell a compiled or is it is
is there a runtime I thought there was a
runtime which means it's not even
compiled which makes it even more conf
confusing but if it is compiled okay so
it's compiled therefore it uses llvm so
is it getting like somehow
better better it's either or well damn
that's
confusing so what's going on okay let's
see it okay so there's actually two
things at least these are the two things
I know of that are going on uh the first
is optimization
hasal has a lot of knowledge so the Haso
compiler is given a lot of knowledge
about the program in the form of types
usually and those types
are algebraic types can with an
expressive type system prevent all sorts
of issues at peer functional programming
this is the function about to happen um
than what the types in C are
not only is it the types but it's also
there's a lot of the semantics of has
about to make a argument here comes give
the compiler a lot of leeway let's call
it a lot of wiggle room for optimization
one of those is Again by the way all of
the optimizations he's about to mention
should be doable in
C okay I'm just going to throw this out
there I'm just going to throw it out
there that anything that you do should
be doable also and see I I have a very I
I'm having a I'm having a big doubt
coming on right now evaluation so in C
it's strict evaluation you put a
function there um you know it kind of
executes one line at a time you know is
how you should think of C executing so
it's going to call this line everything
on this line is going to finish before
we get to the next line right yeah but
in high school that's not the case what
you can have you can call something give
it a name and um it doesn't actually do
anything yet right and so the compiler
can actually analyze it and a lot of
times figure out hey you never use this
in this Branch right so I'm not going to
do it yet until I know that you're in
this branch and then I'll I'll do it or
it might it might never do it like the
analysis gets so complex it might just
remember how to do it and in case you
need it it'll be there but it won't do
it so there's a lot of stuff that uh you
could in theory hand tune if you were a
a very
uh very good at optimizing as a
programmer you could hand tune and say
oh there's a certain case where I won't
need this value so I'm just not going to
calculate until I really really need it
but it in in practice that gets so hard
it gets so
complicated is that a skill issue did
did he literally just claim skill
issue I mean I'm not going to say like
again garbage collectors often can lead
you to more like better code than you
doing it in Rust and it's a skill issue
it's fully a skill issue the thing is in
in Rust you'll like I've SE I've made
rust programs slower than go programs
because I'm not great at rust like I I
wasn't the best person ever on it
because I was trying to I was still in
the learning rust phase right and so I
was not able to write a server as fast
as I could write it and go just simply
because some level of garbage collection
prevents all cloning whereas you find
yourself making clones and doing things
in a different way in Rust because
you're not good at it so there is like
most certainly I do not doubt cloning or
skill issues play a big role in how fast
you write your
code that you're just not going to do it
right and so hll can do that Haskell
just does it in the program doesn't have
to think about it right so you can do
some analysis you can figure out when
something should be is going to be
needed anyway might as well just
calculate it now you can do some
analysis and say oh it's only needed
sometimes so I'm probably won't do it
yet and uh and then sometimes it just
punts and just says well I don't know
how to analyze this but the net effect
is that that is faster another thing is
um it can do a lot of because because
everything is uh pure it can do a lot
more uh optimizations of like moving
code around and inlining and um doing
more stuff at compile time that can't be
done in C um and those is that statement
true it's all llvm but is that I
mean I could understand that if you know
everything as peer there's some things
you could do that would be
different right like I I do get that
that that that there has to be some
level of optimization that you can do
there but also if everything is PE it
also means you're creating a bunch of
new
objects you have to Forever create new
things that unless if it's I know I'd
love to see some examples uh unless
if like that's so that's like where I'm
struggling with with is there has if
you're making something like that there
has to
be coping or some other thing going on
which is just notoriously slower than
you know mutating memory could be the
optim way okay
maybe maybe show us yeah I want to see
the video let's see let's hear here
let's see if he does it in the video are
typically thought of as like inlining
people optimization that kind of stuff
but uh Hull can has a broader range of
Maneuvers that it can do that let the
code get optimized really well and
that's nice uh so what I what what I
hear from him what I'm hearing here is
that if you are an average hcll
developer or an average C developer it's
easier to write better code in hll than
it is to write it in C which I wouldn't
NE necessarily argue that okay that's a
fine argument to make that you can say
that it's easier to write like again
when I first started learning rust it
was easier to write a go program faster
than it was to write a rust program
because I kept finding myself cloning
and doing silly things so fair totally
fair but you can't say that in an
absolute sense meaning that anybody
writing C can or writing hll will ne ne
necessarily be faster than writing C so
it's kind of like you're getting the
benefits of the high level like you can
code how how how it should be read right
I'm coding for another programmer I'm
just making it very readable but then
the compiler can kind of transform it
into something that's better to be
executed on the machine okay that's
number one that's optimization but
number two is potentially even bigger
and that is that oh gosh hasal lets you
use better data and algorithms and I
need to explain because okay yes they're
both through in complete okay so let's
take that argument I agree but let's
take it off the table no one made that
argument ran kril though that does sound
like an argument uh hkers would make
well are they okay they are touring
complete okay just making sure uh
actually made this argument and that's
where I got the idea for this episode
from he was talking about rust but it
applies equally to hll so he has been
doing a lot of system programming in his
career and a lot of it is done in C
because it needs to be lowlevel and his
his argument goes like this well see you
know if you need a data structure um
it's and see it's just kind of hard to
do anything more complex than a link
list or you know maybe you could get a
little bit more complicated but link
list is kind of you know you write link
list all day long because that's like
it's something that you know you're not
going to mess up and it oh man you just
got question marks from low-level
learning when you get this many question
marks from llll you know he's upset when
they when he says this this isn't a good
representation right you're kind of
straw Manning the average C developer or
what the average C development
experience is oh since you can only do
link list and C that's what you do I
mean granted I've written a lot of Link
list and C so fair take in that sense
that you do write link list and see
they're very great to write it but you
can also write a tree and see a linked
list is a tree it's just a one-sided
weighted tree right it's the worst
optimization of a tree you can also
write arrays you can also write Dynamic
arrays in C does the job and it's fine
right hash well can actually because
it's higher level it can actually manage
much more complex data structures and do
so in a correct way right so you can
write it gives you tools to write data
structures that are known to be more
efficient for certain access patterns
right and linked lists are very linear
they so so every you know not every
access if you access the first thing
it's not it's constant but in general
you're accessing things Conant inside
the list uh randomly right let's just
say that's what your algorithm is and so
any access so you're so you're accessing
stuff randomly that's linear and so if
you do that in Loop wow you're quadratic
already right
so in hll you can replace that link list
with something else like let's say a
tree right so now you're act I just
claimed I just made the claim that a
linked list is a tree let me explain why
I say a linked list is a tree all right
so we got a little linked list here
right a goes to b b goes to C okay are
you ready for this are you
ready it's it's a
tree yeah yeah the conspiracy theories
were correct linked lists are an effing
tree yeah this is an unbalanced binary
search tree on accident didn't even I
know I know people people are shook
right now just I'm just saying that we
could totally do this hold on on the
only Point here is that most data
structures are somewhat swappable like
your implementation can be abstracted
over data structures that is still
copium it is still copium I agree with
it but I you know I'm just letting you
know like when you say something like
you can replace a linked list with a
tree you've effectively said nothing at
all right because a linked list is a
tree now if you wish to have a second
link instead of having say a next you
can just name it left instead of next
and then you can have right for the
other side so you're saying a link list
is just a unary tree I mean really it's
just a it's just a poor L formed binary
tree if you think about it that's all it
is is it's just a bad binary tree now
you could say okay well technically you
don't use the memory position for each
one which is also a graph it's also a
graph this one would be an ayylic
directed graph commonly referred to as a
dag okay um I have a meeting at 1300 but
I want to see how this ends okay I'll
keep on going I'm gonna keep on going
I'm sorry I'm just I just need to get
that out there just because it's it's
very important for people to understand
that that point access is logarithmic
you've already saved a bunch of time uh
in terms of complexity time
complexity and so that is another way
that Haskell benefits overse that by the
way I do want to make one quick thing uh
by using a tree or a linked list you
could be inefficient as compared to
using something like a array so if you
had some values that could be stored on
on the stack and you had and you had a
nice like array a contiguous memory spot
even though you have to search in a
linear time it can be faster than o of
one access such as a set because a
you're not jumping around to Crazy
memory spots but B you're doing this
really close locality of memory searches
whereas it's very hard to do that with
something like uh like a tree you can't
I mean you could you could a heap is an
array if you think about it so could you
model something like that yeah you could
you could you could Heap it hard and say
the same thing but again now you're just
like the argument is completely lost
here which is choosing like of course
course doing a quadratic search over a
large set of items is always going to be
slower than a logarithmic search over a
large set of items right when I say
large I mean n has to be sufficiently
sized if the problem is complicated
enough where or big enough
where really you should the difference
between in Access complexity in Big O
notation complexity between a link list
and a and a tree boom it's it's a big
enough difference H's going to
win but just make it a tree and see
that's not an argument just make it a
tree and see and it's simply a matter of
um how I do see what he's saying though
is that it's very simple to have this
tree in hasal so if you program it like
a 1D grug head then yes it's easier to
do
that but this is kind of like it's it's
too much of a straw man for me to accept
much complexity can you handle now of
course if someone wrote a tree in C and
you imported that library and you you
know included it in your C code you
would start competing again with Haskell
right you could do that sure um but uh
do you do that is that a possibility in
these benchmarks it might not be a
possibility whereas in hasell you could
do that you could write it yourself um
and I find that this is the case a lot
of times in um higher level languages
you know it's a okay I got to say it one
more time a link list is a tree if you
can write a linked list you could write
a tree the end right C is very low level
Java's higher level than but then Scala
is higher level than Java closure is
higher level than Java um so it's a
spectrum and what happens is as you
tackle these benchmarks sure C's going
to win because it's very small problems
right it's like you know
calculating uh something with a known
algorithm right so like people have been
optimizing the C algorithm for that
years so they know exactly how to make
it fastest um but when you're dealing
with more real world problems bigger
problems uh very often you do need
garbage collection and um higher a
better concept of of data type um and
data structure than c will give you and
so that's uh what happens so just so if
he's saying you can only rely on what's
built into C then yeah I could
understand that because you pretty much
just have a raise I get that tree issue
yeah it's not a skill issue it's a tree
issue like I mean I understand the
argument that hcll gives you more out of
the box and thus you can solve a lot of
things in a more efficient way just off
the rip agreed I think we can all agree
with that if that's the argument he's
making then I think I get that argument
right like just like with C++ how easy
is it to make a vector it's very very
simple the STL is is very very good
right and so yeah does that mean C++ is
faster than C no is it easier to get off
the ground than C sure right can you do
the same thing in Rust absolutely is it
easier to get off the ground in Rust
than C++ well you have to dye your hair
blue first but you get the idea it can
be easier and so it's like is that is
that like are those Fair comparisons I
wouldn't say they're Fair
comparisons and it's not just about data
structor there's other facilities of the
language um just as a little anecdote I
heard a story once where um there was a
competition to see who could write the
fastest program and it was C versus Java
and it surprised everyone especially the
C programmers but the Java
implementation won and it won by a lot
and uh the C people are like no it's not
possible it's you know how could this
big monstrous VM beat our highly tuned
tiny little C program and uh they
started reading the Java code and you
know you can imagine them like huddled
there with their print outs like ah then
they all like no look they cheated and
what they were pointing at was in Java
they had used
threads they used multiple threads to
solve the problem whereas in C they
didn't okay C11 has the new thread API
the one before it has P threads can you
not use just threads right like what is
this this doesn't sound fair if you have
a if you have a problem that could be
massively parallelized easily then of
course it's faster than not
parallelizing
it and they considered that
cheating Well yeah if you were okay
that's fair if you were told which
program is faster at doing X and you
assume that you can't use threads that
is totally fair take go oh that's
cheating we didn't think you could do
that of course we could make this faster
you want us to use threads as
well P threads came out and who knows
what but it was C11 right C11 has the
new uh thread API right which apparently
is really nice C11 uh thread uh thread
threads
right there's like a it's it's this new
yeah you get you get all these like you
get this new nice operations for very
beautiful nice thread stuff that makes
things a little bit easier to use uh
which seems really really nice
uh I have never I haven't used I I
haven't built a lot of things in in C
the last time I built something in C was
a video driver uh last year and the time
before that was a twitch IRC bot was it
in C that was fun I liked the twitch IRC
bot I want to do that again I want to do
that again but let's keep on going
so see you can see the you can see the
the problem here right like you can see
why they consider it cheating because
they're used to benchmarks where it's
just one thread purely sequential code
and it is really hard to write threads
in C compared to Java and in Java it's
very easy to write threads is that
true I I'd say in some senses probably
true is it you don't think so I mean I
think there's some things that are
easier in Java to write threads for
right like just like in Rust it's really
easy to write a thread in
Rust right and to do stuff and not die
in some weird way uh but beyond that
like
if you're Vaguely Familiar with the p
threads Library I feel like you should
be able to easily do
this Fearless concurrency I know it's
not really a fair comparison of course
things are easier in Java in the sense
that you you spawn a
thread right and you just have way less
things you have to be concerned with
because you have so much help from the
garbage collection and all that but at
the end of the day is it that much
easier the hard part isn't in spawning
of threads the hard part is not effing
your yourself and creating mutexes or
atomics that you have to constantly rely
on to be able to do anything the hardest
part is the paralyzation which is
equally hard in any
language I don't know about this I'm
actually curi I'm most curious on uh
low-level learnings one p thread create
my thread boom let's go right I to me
the paralyzation not the thread yeah
threading seems easy it's the
communication between threads that is
hard do you have a non-locking cue that
I can add stuff to are we using semop
are we using mutases what are we using
to control all the stuff right uh that's
one thing I've been wanting to play more
around with um in Rust is I always use a
semaphore and a series of clients to
send stuff out why not spawn Hardware
threads and use and use non-blocking
cues to divvy up the work and try that
out to me that sounds actually like a
more fun experience to do well I'd like
to try that yeah go weight groups
another exact same version yeah spawning
spawning uh threadlike material in go is
the e easiest I just go like this go
Funk go Funk yourself so does that mean
go is superior to
C well I mean I would argue no and to
start
new and so uh you know yes what you do
it is what do you do who won well I say
the Java people won
because that's the whole point of java
is it makes those kinds of things easy
it makes threads on you know
crossplatform really easy uh it makes
garbage collection crossplatform really
easy and I think that the same thing is
true of hll you know if it makes um
writing a data structure that gives you
an advantage over a link list let's say
easy and so that's what you get that's
what that's why it's faster all right if
you um have some ideas about why hll is
faster about these high level languages
how they could be faster than a
low-level language where you get to
control everything and highly tune the
you know the code down to individual
instructions if that's the kind of thing
you like doing how is that even possible
um do you agree with me uh disagree I'd
love to know you can go to lc.com
podcast you can find ways to contact me
email Twitter LinkedIn um you can also
find all the past
episodes they have audio video and
transcripts text transcripts I I find
his comparison completely unfair the
first part I found actually to be the
most
compelling right
the the first part was the most
compelling meaning that it is very easy
that there's a lot of really great
optimizations that can be done with say
pure functions and stuff that just makes
it really easy to do the right thing to
me that seems like that seems like a
valid answer right okay you're right
maybe it's easier to do the right thing
in the 80%
case true but to claim that it's faster
is just silly I can talk about part two
if you want but it's too hard to do in
chat neovim runs uh into that problem
what do you mean part
two can you talk about part two oh
writing your own data structures in Neo
in
C I mean the hard part yeah is part two
argument writing the right data
structure I mean the hard part about it
is just C right you there's it's very
you can't you don't really have generics
you can't make a a I mean I've seen
someone do it with macros which is
disgusting but you can build a tree
dedicated to a singular data type
you know I I I mean I understand that
it's it's harder in C right I've written
enough C to know that it's harder and C
to do things like being hard and C it's
not just about generics it's probably
about the code and reuse yeah yeah yeah
exactly no I'm on your team I mean
generics is what makes these a lot of
these things more reusable but he's not
saying it right there I know I'm on your
team I'm fully on your team I I know I
know the the general problems of C
someone who I've written enough C to
know that I'm bad at C
right I I've written enough C to know
that I would probably write rust faster
generally just because I could write it
faster and I could probably be more
correct
faster in general C has generics really
you going to toss like a void star at me
next that's the
generic no npm andc literally unusable
fair fair void star D nuts uh anyway
it's not just about a code you write
it's also about getting a code from
other people and being able to replace
parts of your stack yep no all that's
completely Fair C makes it harder for
sure 100% I am 100% the the thing is
is it's not so the phrase how is hasal
faster than C is just wrong what it is
is hasal has better development
experience than C would be the better
thing to do would be the better thing to
say because absolutely anything you can
write in hll you can write in
C
right all
right it's easier idiomatic hascal is
faster than C you can't say that
either you can't say that because I
don't know if that's true right you have
to show me the pro I mean you can't say
that you can't say that idiomatic hascal
is faster than c pig
C that's copium that is copium idiomatic
hascal is faster than C is
copium dude these the the comments on
this video on YouTube are going to
literally just be filled with either
hasal lovers and telling me I'm wrong or
people going what the hell's happened
here yeah it's not just compared to Dick
measuring in benchmarks uh then it would
quite valid take this is fair how is has
fast than C once again yeah I know I
know you're playing the devil advocate
I'm on I I understand what you're
saying I just don't think it's a good I
just generally don't like those kind of
arguments again as I made the argument
earlier
today which we'll pull up here comparing
speed uh not difficulty see wins and
speed yeah that's fair if you're saying
it's easier to write stuff in hll I'm
probably on your team honestly right
look at this I even tweeted this out
this
morning if you do an X is faster than C
article it only means one thing you're
not writing C right right like this is
100% I even pregamed this article with a
tweet cuz I knew for a fact this is what
was going to happen at the end of the
article which was literally skill issue
at the end how did I miss I I know it is
Charo chot chot it's always that it's
always that pre-at
pre-at
yeah plus typically that's not the
question we want to ask okay so what's
the more interesting question I think to
ask which
is how easy is it to write fast code to
me that's the more interesting one and
so something like rust makes a more
compelling argument or something like go
go is the 99% case it's still really
fast but it's really easy to write and
so to me that's like the uh what's it
called that's the better argument to
make which is how do you generally write
fast code
use a language that generally produces
fast code Now is it going to be the
fastest no it will not be the fastest
but will it be fast enough that's what
you have to ask yourself right Odin
might win that Odin could win that Odin
Odin's a very interesting language that
I wish I had more time to play with I
just wish I had more time but o camel is
my next one right JavaScript allows you
to write really fast in the beginning
and then you hit what I call like the C
of complexity where as you start writing
you start hitting these things where
things start falling apart in weird ways
I think this generally is what uh he
wanted to say except that hasal is not
easy to write yes I know that's the
that's that's what I'm laughing on the
inside about which is hascal is
historically not known to be easy to
write like that's not one of the phrases
you would use to describe your hascal
experience which is easy to write that's
just not it right like I mean even to
get started in hll how much of your
wardrobe do you need to replace with
tie-dye t-shirts like that's another
thing I don't even know if it's true
right like how many do I need before I
can even be remotely effective then
someone's going to explain a monad to
you but using a burrito and you're going
to be so confusing so confused all right
hey the name is I'll probably never
learn hasal but I'm definitely going to
learn o Camen