From: "Eregon (Benoit Daloze)" Date: 2022-01-06T22:25:33+00:00 Subject: [ruby-core:106991] [Ruby master Feature#18461] closures are capturing unused variables Issue #18461 has been updated by Eregon (Benoit Daloze). bughit (bug hit) wrote in #note-4: > There's no such need. If the capture is optimized then Proc#binding will continue giving access to what has been captured, rather than everything that could be captured. That's of no use then, that's just not the semantics of Proc#binding, which is to capture every variable at the place the block is defined. The block could also use `eval`, or an alias of `eval` and then how could you know if the block would access an outer variable? Hence Proc#binding would need to be removed before we can sensibly optimize any of that, and we'd need constraints on aliasing eval's and binding's probably. Ruby does reflect updates in the original frame, so you'd still have an indirection for every such outer variable, both by the block but also in the original frame (at least from the point the block is defined). > There could be huge object graphs pointlessly inadvertently kept alive by unoptimized capture. I doubt this is a big issue in practice, there seems to be few reports about it and most Proc instances are not kept alive long. Procs also can't be serialized. It's an interesting and well-known optimization, but I think one that would need to significantly change Ruby semantics to be feasible without breaking many things. I think it's valuable to figure out exactly what we'd need to change and if the Ruby team/matz would support that. OTOH, I believe just breaking compatibility blindly to optimize will never be accepted. ---------------------------------------- Feature #18461: closures are capturing unused variables https://bugs.ruby-lang.org/issues/18461#change-95822 * Author: bughit (bug hit) * Status: Open * Priority: Normal ---------------------------------------- ```rb def foo a = 1 ->{} end p foo.binding.local_variables # [:a] ``` Shouldn't `a` be optimized away? Like v8 does (https://bugs.chromium.org/p/v8/issues/detail?id=3491) -- https://bugs.ruby-lang.org/ Unsubscribe: