lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


David Kastrup wrote::
> Rici Lake <lua@ricilake.net> writes:
>> and a few probes into the intern table all of which will quickly
>> return "unequal". However, copying a million bytes (say) is time
>> consuming.
> 
> I doubt that million byte strings are the norm.  And I doubt that
> you'll _ever_ be in the situation that million-byte strings will be
> used for comparison or indexing, and thus the whole hashing operation
> is a waste of time.   

Maybe Rici won't _ever_ do that, but in fact other people do, at least
did I once. Here is the pseudo-code I used:

local file = io.open("foo.txt", "r"):
local input = file:read("*a")
file:close()

output = foo(input)

-- Only rewrite file if it changed
if input~=output then
  file = io.open("foo.txt", "w")
  file:write(output)
  file:close()
end

In the real code foo was some function doing specific search and
replace, and foo.txt was a kind of multi-megabyte text database. Of
course I could have done that differently, but that was the easiest
solution to the problem and its constraint ("Only rewrit[ing] file if it
changed"). You can't assume that some algorithm or code construct won't
be used just on the fact that out of context (and with your point of
view) it looks silly.