[Haskell-cafe] Re: Threads with high CPU usage

John Goerzen jgoerzen at complete.org
Mon Dec 22 09:15:46 EST 2008

Duncan Coutts wrote:
> On Mon, 2008-12-22 at 10:30 +0000, Malcolm Wallace wrote:
>> The terminology seems counter-intuitive, but in other other words, a
>> "safe" call is slower but more flexible, an "unsafe" call is fast and
>> dangerous.  Therefore it is always OK to convert an "unsafe" declaration
>> into a "safe" one, but never OK to convert from "safe" to "unsafe"
>> without looking at what the foreign side actually does.
> And in general we would not even bother with considering using "unsafe"
> for calls that are already expensive. It's only worth considering when
> the length of the call is always very short.
> For example in a database library it might make sense to use 'unsafe' on
> the data-access functions that extract data from a local query result
> but we should always use 'safe' on any DB function that might want to
> talk to the network (eg to get more query results).

It's difficult to anticipate the needs here.  For instance, some people
may be using a few very-long-running queries measured in minutes, such
as the original poster.  Other people, such as web app developers, may
be issuing literally millions of queries, right after another, where the
difference matters.

I had initially used "unsafe" because of the documented performance
benefit, plus I certainly am not expecting Sqlite to call back into the
Haskell runtime.

It seems to me strange that using "unsafe" instead of "safe" would have
negative implications for threading.  After all, as Malcolm said above,
"it is always OK to convert an unsafe declaration into a safe one".  So
could the compiler be made to be smart enough to do so when it is
advantageous for threading purposes?

What's the best way to make this suitable for both people with many
queries and those with long-running queries?

I should also add that I suspect the C calls that are bothering the
original poster here are the standard ones for reading results, and
could be called many, many times on even a single query.

-- John

More information about the Haskell-Cafe mailing list