[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: (not) handling new programming idioms with grace
- From: Sean Conner <sean@...>
- Date: Wed, 18 Jul 2018 14:18:11 -0400
It was thus said that the Great Andrew Starks once stated:
>
> There is no simple and robust way to destroy objects that:
> 1: have allocated resource that cannot remain allocated for indeterminate
> amounts of time
> 2: need to be destroyed in a specified order
> 3: to do so even when something goes wrong, every time
>
> Is that the problem to be solved? Who is the target for this feature? A
> person informally acquainted with programming or someone writing Lua
> libraries?
Yes.
A few months ago I wrote some code to recursively dive into directories,
using something like:
fsys = require "org.conman.fsys"
function dive(path)
fsys.chdir(path)
for entry in fsys.dir() do
local info = fsys.stat(entry)
if info.type == 'file' then
do_something_with(entry)
elseif info.type == 'dir' then
dive(entry)
end
end
end
dive(os.getenv "HOME")
I was exhausting file descriptors before my program finished [1]. Upon
investigation, it was because iterating over a directory uses a file
descriptor, and because GC had yet to kick in, I had a bazillion open
directories. I had to change the iterator to close and release the
directory upon finishing instead of relying upon the GC. And this is in
what I consider library code.
-spc
[1] fsys.dir() is an iterator that returns filename in the given
directory (defaults to '.' if not given one). It will ignore the
'.' (current directory) and '..' (parent directory) entries becase
in my use case, those are things I never want to see.