
Reducing Memory Usage in Ruby - craigkerstiens
https://tenderlovemaking.com/2018/01/23/reducing-memory-usage-in-ruby.html
======
sandGorgon
What I'm really keen to see is truffleruby receiving a lot of community
support.

The underlying JVM and the Truffle/Graal/SubstrateVM is the product of
millions of man hours of research - and for the very first time, you have a
compiler framework on top of that.

The Truffle framework also brings in a lot of support that all languages get
for free, like zero overhead profiling
[https://twitter.com/nirvdrum/status/948333404122214401](https://twitter.com/nirvdrum/status/948333404122214401)

Truffleruby is already in the running in benchmarks, and it will be awesome to
see how far it can be pushed . [http://nirvdrum.com/2017/02/15/truffleruby-on-
the-substrate-...](http://nirvdrum.com/2017/02/15/truffleruby-on-the-
substrate-vm.html)

~~~
ksec
The major obstacle right now is C extension, which I think Sulong [1] is
suppose to solve.

It seems right now, TruffleRuby 99% of Ruby code without much problem. C
Extension may be the key to wide spread adoption.

[1] [https://github.com/graalvm/sulong](https://github.com/graalvm/sulong)

------
kawsper
I am so happy that Github have hired Tenderlove to go nuts with these type of
projects. His talks are always extremely fun and interesting.

I run Ruby with jemalloc, and I know that samsaffron have tried to include it
within the Ruby releases (like Redis does), but progress seems to have been
stalled [https://bugs.ruby-lang.org/issues/9113](https://bugs.ruby-
lang.org/issues/9113)

~~~
farnsworthy
I thought he was nuts already. (Smiley thing here.)

------
ad_hominem
This is orthogonal to tenderlove's GC work, but if you're using MRI and want
an easy memory usage/performance win, swap out the standard glibc malloc for
jemalloc:
[https://www.levups.com/en/blog/2017/optimize_ruby_memory_usa...](https://www.levups.com/en/blog/2017/optimize_ruby_memory_usage_jemalloc_heroku_scalingo.html)

~~~
johncolanduoni
Kind of a tangent, but does anyone know why glibc’s allocator loses to
jemalloc on common workloads? I would expect that would be something worth
rectifying unless other requirements tie their hands.

~~~
__s
Quite a few complaints about jemalloc from Rust, where people wonder about
higher than necessary memory usage

jemalloc likely wins more for the kinds of allocations you find in scripting
language runtimes, which also happens to be similar to a browser given DOM &
JS

See also: [https://sourceware.org/glibc/wiki/389-ds-
malloc](https://sourceware.org/glibc/wiki/389-ds-malloc)

------
igravious
What I'd like to be able to do is load ActiveRecord (or any other v. heavy
lib) asynchronously in non web-based apps so that I can paint the UI quicker.
By the time the user will do/need anything db-related a proxy object should be
swapped out for an actual ActiveRecord object. Using Rails' ORM is so nice and
ergonomic but it kills start-up time with all the file requires, Bootsnap
helps a bit but I'd prefer not to have to use it as it is yet one other moving
part to consider.

~~~
AznHisoka
I am actually facing a similar problem. I also find that Rails just creates a
db connection everytime I do Model.new even though I have not yet saved it.

~~~
amichal
This is because active record models learn their attributes by querying the
database scheme. I haven’t tried it but there appears to be a way to cache
this information in the local file system which would likely avoid the early
dB connect if you really needed to do that. Also avoids querying the scheme on
processs boot entirely if you are willing to trust the cached scheme matches
the. database scheme. See
[https://github.com/rails/rails/issues/24133](https://github.com/rails/rails/issues/24133)
for discussion

------
hirundo
Ruby seems to have a footprint on Hacker News disproportionate to its general
popularity and TIOBE index. Why?

~~~
mwaitjmp
Not only due to the popularity of Rails, Ruby has become popular for sysadmins
as config management solutions such as puppet and chef are built on top of it.
The resulting syntax is very ruby like, and in some cases you are even
required to write a few ruby blocks here and there.

It also has an amazing testing framework in the form of rspec, which can be
used for much more than just testing Ruby code.

~~~
ngsayjoe
Yeah, and i use Selenium with Ruby as well.

~~~
skinnymuch
Do you Watir Webdriver, much nicer Ruby wrapper, or straight up Selenium?

~~~
ngsayjoe
Straight up Selenium.

------
crb002
Is there any effort to make WASM run under Ruby? Seems like easy work to get
high performance without bespoke work on Ruby's VM.

------
sam0x17
Or just use Crystal [https://crystal-lang.org](https://crystal-lang.org) XD

~~~
igravious
Crystal is not a replacement for Ruby. In the same way that many Algol-like
languages are not a straight swap for each other. Don't get me wrong, Crystal
is a nice idea but say goodbye to all that yummy Ruby meta-programming which
is one of the key things what makes the language so appealing in the first
place.

~~~
eropple
Exactly. As a Ruby developer, Crystal makes me _itch_. It jettisons exactly
what Ruby is valuable to me for: quickly and dynamically building fluent-
feeling libraries and tools. It's fast, but I don't need fast--if I don't need
Ruby's metaprogramming, I have other, better, established languages to use for
those tasks. (Kotlin, C#, Rust, whatever.)

Elixir is kind-of in the same boat for me. I think it's neat and has its
place, and BEAM is _really_ cool, but it doesn't scratch the Ruby itch for me.
I just don't care about it looking vaguely like Ruby if it's not actually
gonna be Ruby.

~~~
sam0x17
Have you used a recent (2017) version of Crystal? Quickly building fluent-
feeling libraries and tools is exactly my use case for Crystal.

For me, everything that Ruby is valuable for is retained. I'm able to do all
the things I did in Ruby just fine, and it prevents me from doing a lot of
things I shouldn't have been doing.

We recently migrated our app from Rails to Crystal (using the Amber framework)
and our code base is now 2/3rds the size and we eliminated hundreds of bugs in
the process simply from static typing and explicit nil handling. Our response
times are also now all under 30ms where we were at 100ms+ before.

~~~
eropple
Until I can perform runtime define_method calls and dynamically construct
classes, it doesn't matter what Crystal does. It's fundamentally antithetical
to my needs. (JavaScript isn't, but JavaScript is kind of clunky.)

I have Kotlin, C#, C++, and Rust if I need static typing. Ruby is for when I
don't.

~~~
yorwba
How late at runtime do these dynamic definitions need to be possible? Just at
startup or also after the program has been running for a while?

I occasionally wonder whether it might be possible to accommodate more dynamic
language features with a compiler that treats them like a macro system that is
expanded statically, such that no actual dynamicism is necessary at runtime.

~~~
sam0x17
This is exactly what crystal does

