The future of Ruby bindings

Subject: The future of Ruby bindings

Date: Fri, 30 Apr 2021 17:14:15 -0500

To: notmuch@notmuchmail.org

Cc: Ali Polatel

From: Felipe Contreras


Hi,

Ruby is by far my favorite programming language, and I'm very familiar
with the way it's meant to be used.

This is very idiomatic of Ruby:

  $db.query('').search_threads.each do |thread|
    puts thread.subject
  end

It works perfectly fine, but it leaks memory.

In order to prevent memory from being leaked, we have to do something
like:

  query = $db.query('')
  threads = query.search_threads
  threads.each do |thread|
    puts thread.subject
    thread.destroy!
  end
  threads.destroy!
  query.destroy!

This is very ugly Ruby.

Ruby is a garbage collected language, this destroy! approach works, but
there's no better way to describe it but "a hack".

I understand why Ali Polatel did commit c7893408 (ruby: Kill garbage
collection related cruft., 2010-05-26); because the order of the object
destruction cannot be ensured in Ruby, however, there's ways to
workaround that.

 1. We can steal the object
 2. We can increase the reference count
 3. We can add a second parent

The notmuch API doesn't have helpers to do either one of those things,
but since we know talloc is used internally, we can simply utilize that
knowledge.

I sent a proof of concept patch [1], that uses method 3, but it was
ignored.

I could proceed and do the actual full patch using this method over all
the Ruby code, but it's tedious work that I would rather not do until I
know such an approach would be accepted.

With my proposed approach we wouldn't have to rely on destroy! (which
still works), and Ruby's garbage collection would work fine and no
memory would be leaked.

Thoughts?

Cheers.

[1] id:20210427085343.2300-1-felipe.contreras@gmail.com

-- 
Felipe Contreras
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: