Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: reproducing HashTable benchmark (Tom Doris)
2. While Condition in Haskell (Lorenzo Isella)
3. Re: While Condition in Haskell (Patrick LeBoutillier)
4. Re: Re: how to read file with locking (Joey Hess)
5. Re: Re: how to read file with locking (Jimmy Wylie)
6. Re: Re: how to read file with locking (Jimmy Wylie)
----------------------------------------------------------------------
Message: 1
Date: Sun, 10 Oct 2010 16:10:12 +0100
From: Tom Doris <[email protected]>
Subject: Re: [Haskell-beginners] reproducing HashTable benchmark
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thanks for the clarification. I installed 6.12.3 and can reproduce the
benchmark timings now.
On 10 October 2010 13:10, Daniel Fischer <[email protected]> wrote:
> On Sunday 10 October 2010 13:01:59, Tom Doris wrote:
> > I've been investigating the performance of some word frequency counting
> > code I wrote, and I came across this stackoverflow thread
> > http://stackoverflow.com/questions/3058529/curious-about-the-hashtable-p
> >roblemwhere Dons shows simple benchmarks for HashTable and IntMap, but I
> > can't reproduce the "after the fix" performance of the HashTable code, I
> > am very confused as to what version of ghc actually has the fix.
> >
> > In the post Don writes "With GHC 6.10.2, before the fix, inserting 10M
> > ints:" and then "With GHC 6.13, after the fix:", is this a typo,
> > shouldn't it read 6.10.3? Since there is no 6.13 version of ghc yet?
>
> And there never will be. GHC's versioning scheme for releases is
>
> series.even-branch-number.patchlevel
>
> Odd numbers in second place indicate the development branch (aka HEAD).
>
> So 6.13 was the development branch after 6.12 was released. Originally, a
> 6.14 release was planned, but there have been so significant changes that
> the next release (due in a few weeks, probably end of October) will be the
> first of the 7 series (and HEAD is now 7.1, no longer 6.13).
>
> > If it should be 6.10.3, and I'm running v 6.12.1, why does the HashTable
> > code in Don's post take 22 seconds to run on my machine (compiled ghc
> > --make -O2) and the IntMap version takes only 5.8s? How can one find out
> > what release the fix for ticket #650 was released into? I've tried the
> > release notes but I can't seem to spot it, and google doesn't either.
> > The ticket has "Milestone: 6.12.2" but that is struck-through, though
> > the comments would suggest this made it into r 6.12.2, I can't spot
> > anything in the release notes for that version either.
>
> Not sure what goes in the release notes, nor whether the fix was in 6.12.2.
> But the patch was merged nine months ago, it should be in 6.12.2 (but if
> you upgrade within the 6.12 series, choose 6.12.3, there were some bugs in
> 6.12.2 iirc).
> However, if it's not pressing, consider waiting for the release of GHC 7.
>
> >
> > Can someone help clarify which version the fix will be/is in and how one
> > should figure this out in general for other issues?
>
> In general, ask or test. But if you found a ticket for the issue, if it's
> closed with a merge, any release later than a few eeeks after the merge
> should contain the fix (unless it's explicitly stated that the patch will
> not go to the stable branch).
>
> >
> > Thanks
> > Tom
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101010/f9e0034e/attachment-0001.html
------------------------------
Message: 2
Date: Sun, 10 Oct 2010 22:15:03 +0200
From: Lorenzo Isella <[email protected]>
Subject: [Haskell-beginners] While Condition in Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Dear All,
Definitely I am having some troubles in thinking like an haskeller...
Please consider the snippet at the end of the email. Its aim is rather
simple: given a list and a value i (position of along the list), it
defines the "future" as the chunk of the list from i-th element of he
list to the end of the list, whereas the "past" is anything in the list
that appears before the i-th element.
I wrote a function which checks whether lists of length 1,2...(till I
reach the end of the list) in the future (always with respect to
position i) has already occurred in past.
In order to find the length of the longest list in the future which has
already been seen in the past, I could select the "true" values in the
output of function
iter_find list i
but this is a waste of CPU: I simply would like a while condition to
tell my function to stop checking new sublists as soon as it finds one
which has not occurred in the past and I would like a counter (a kind of
i++) telling me how many times the process has been iterated.
Any suggestion is helpful.
Cheers
Lorenzo
-------------------------------------------------------------------
import Data.Ord
import Data.List
main :: IO ()
main = do
let list = [4,55,66,77,88,99,12,9,77,88,99,12,-99]
let i = 9
let b = iter_find list i
putStrLn "b is, "
print b
is_sublist sublist list = sublist `isInfixOf` list
gen_fut_list list i j = take j $ drop (i-1) list
gen_past_list list i = take (i-1) list
find_in_list list i j = is_sublist (gen_fut_list list i j)
(gen_past_list list i)
iter_find list i = map (find_in_list list i) [1..n]
where n = (length list) - i +1
------------------------------
Message: 3
Date: Sun, 10 Oct 2010 22:09:12 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] While Condition in Haskell
To: Lorenzo Isella <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Lorenzo,
On Sun, Oct 10, 2010 at 4:15 PM, Lorenzo Isella
<[email protected]> wrote:
> ...
> In order to find the length of the longest list in the future which has
> already been seen in the past, I could select the "true" values in the
> output of function
>
> iter_find list i
>
> but this is a waste of CPU: I simply would like a while condition to tell my
> function to stop checking new sublists as soon as it finds one which has not
> occurred in the past and I would like a counter (a kind of i++) telling me
> how many times the process has been iterated.
I'm still a beginner myself, but here's my take on it. Since Haskell
is lazy, the results
will only be generated as they are needed. For example, if you change:
let b = iter_find list i
for
let b = takeWhile id $ iter_find list i
Haskell will stop generating the list as soon as it sees a False result.
Then you can take the length of b as your answer.
Patrick
> Any suggestion is helpful.
> Cheers
>
> Lorenzo
>
> -------------------------------------------------------------------
>
> import Data.Ord
>
>
> import Data.List
>
> main :: IO ()
>
> main = do
>
>
> let list = [4,55,66,77,88,99,12,9,77,88,99,12,-99]
>
> let i = 9
>
> let b = iter_find list i
>
> putStrLn "b is, "
> print b
>
>
> is_sublist sublist list = sublist `isInfixOf` list
>
>
> gen_fut_list list i j = take j $ drop (i-1) list
>
> gen_past_list list i = take (i-1) list
>
> find_in_list list i j = is_sublist (gen_fut_list list i j) (gen_past_list
> list i)
>
> iter_find list i = map (find_in_list list i) [1..n]
> where n = (length list) - i +1
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 4
Date: Mon, 11 Oct 2010 00:29:35 -0400
From: Joey Hess <[email protected]>
Subject: Re: [Haskell-beginners] Re: how to read file with locking
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Ertugrul Soeylemez wrote:
> readFileLocked :: FilePath -> IO B.ByteString
> readFileLocked fn =
> withFile fn ReadMode $ \h -> do
> lockFile h -- for a suitable function lockFile
> B.hGetContents h
>
> It is, BTW, always preferable to use withFile over openFile, if you can.
> This makes your code cleaner and also exception-safe.
Unless there is a better locking primative than waitToSetLock available,
I don't know how to build your lockFile function. It seems that it would
have a side effect of closing the handle. It could return a new handle
like this, but then withFile's automatic close of the file would be
defeated.
lockFile h = do
lockfd <- handleToFd h -- closes h
waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
newh <- fdToHandle lockfd
return newh
Here's what I'm using instead.
withFileLocked file mode action = do
-- TODO: find a way to use bracket here
handle <- openFile file mode
lockfd <- handleToFd handle -- closes handle
waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
handle' <- fdToHandle lockfd
ret <- action handle'
hClose handle'
return ret
where
lockType ReadMode = ReadLock
lockType _ = WriteLock
BTW, thanks for the hint that ByteString has a strict getContents! I was
prototyping my code with String and thought I'd worry about optimisation
later, but that is a good reason to use ByteString up front.
Jimmy Wylie wrote:
> Implementations should enforce as far as possible, at least locally to the
> Haskell process, multiple-reader single-writer locking on files.
According to strace, this does not involve any system-level locking with
flock/fcntl/lockf. It is done internally to the ghc process.
--
see shy jo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 828 bytes
Desc: Digital signature
Url :
http://www.haskell.org/pipermail/beginners/attachments/20101010/300232c5/attachment-0001.bin
------------------------------
Message: 5
Date: Mon, 11 Oct 2010 00:14:54 -0500
From: Jimmy Wylie <[email protected]>
Subject: Re: [Haskell-beginners] Re: how to read file with locking
To: Ertugrul Soeylemez <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
> It is, BTW, always preferable to use withFile over openFile, if you can.
> This makes your code cleaner and also exception-safe.
>
>
>
Hi Ertugrul,
I don't think I quite understand. How is withFile exception-safe?
Under the covers it's using openFile. I was under the impression
withFile was just a nice way to remove boilerplate file operation code.
Here's what I found on hoogle: "withFile
<http://haskell.org/hoogle/?hoogle=withFile> name mode act opens a file
using openFile <http://haskell.org/hoogle/?hoogle=openFile> and passes
the resulting handle to the computation act. The handle will be closed
on exit from withFile <http://haskell.org/hoogle/?hoogle=withFile>,
whether by normal termination or by raising an exception."
Thanks,
Jimmy
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101011/984b7ed4/attachment-0001.html
------------------------------
Message: 6
Date: Mon, 11 Oct 2010 00:30:04 -0500
From: Jimmy Wylie <[email protected]>
Subject: Re: [Haskell-beginners] Re: how to read file with locking
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="ISO-8859-1"; format=flowed
> I don't know how to build your lockFile function. It seems that it would
> have a side effect of closing the handle. It could return a new handle
> like this, but then withFile's automatic close of the file would be
> defeated.
>
> lockFile h = do
> lockfd<- handleToFd h -- closes h
> waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
> newh<- fdToHandle lockfd
> return newh
>
> Here's what I'm using instead.
>
> withFileLocked file mode action = do
> -- TODO: find a way to use bracket here
> handle<- openFile file mode
> lockfd<- handleToFd handle -- closes handle
> waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
> handle'<- fdToHandle lockfd
> ret<- action handle'
> hClose handle'
> return ret
> where
> lockType ReadMode = ReadLock
> lockType _ = WriteLock
>
I was looking here:
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/unix-2.4.0.1/System-Posix-IO.html
Instead of creating the handle, then converting to an fd, only to return
a new handle, why don't you start with the file descriptor and convert
at the end of the process. I don't have time for a full piece of code,
but maybe something like this:
lockFile file = do
fd <- openFd ReadOnly Nothing defaultFileFlags
waitToSetLock fd (lockType mode, AbsoluteSeek, 0, 0)
handle <- fdToHandle fd
return handle
You could also use the same sort of code in your withFileLocked function.
> According to strace, this does not involve any system-level locking with
> flock/fcntl/lockf. It is done internally to the ghc process.
>
Thanks for testing that out. I appreciate the information.
Ciao,
Jimmy
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 18
*****************************************