<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Oct 31, 2018 at 9:08 PM Anthony Cowley <<a href="mailto:acowley@seas.upenn.edu">acowley@seas.upenn.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Edward Kmett writes:<br>
<br>
> One messy sketch of how this could proceed would be to make additional<br>
> Addr-based analogues of the report specified type-unrelated "Ptr a"<br>
> functions and install them side by side with the existing functions. This<br>
> would behave much like how we have both readsPrec and readPrec for standard<br>
> vs. GHC specific Read internals. With MINIMAL pragmas it should basically<br>
> become transparent if they are mutually defined. Since generally Storable<br>
> dictionaries aren't built out of compositions of other Storable<br>
> dictionaries, growing the class shouldn't do any measurable harm to<br>
> performance.<br>
<br>
This doesn't sound right to me. I'm probably an outlier, but I rely heavily on composite Storable instances in vinyl and Frames and general FFI situations. I don't know if a more common usage like an ad hoc record relying on the Storable instances of its fields would be greatly impacted, but this needs some investigation.</blockquote><div> </div><div>Fair point. It does feel at least worth benchmarking if we do decide to go down this path. <br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">More generally, I'm in agreement with the points Sven has been making in this discussion, and wouldn't like to see such an unresolved debate be resolved by imposing a performance penalty on everyone as a compromise.<br></blockquote><div><br></div><div>That said coercing copies of ~2 definitions into a class that already has 8 members seems unlikely to move the needle on this to me. If, say, 1-3% is a cost that could never be borne then we'd never be able to get around to fixing things like, say, sizeOf or alignment taking actively harmful arguments, rather than just type arguments or proxies, so we might need to consider what is an acceptable trade off in terms of leaning into the current local optima vs. allowing for growth. Fixing those warts some day in an eventually standardizable way would incur exactly the same amount of overhead.<br></div><div><br></div><div>If on the other hand the cost winds up being appreciably higher than that, then by all means its worth considering other approaches like leaving the methods in the class alone and adding the Addr versions as top level methods, if we decide we do want to move Addr into base. I like that option somewhat less as it means that there is no real sane roadmap for how to go from there to a simpler story in the future, even if we don't choose to start down the road to standardizing that behavior prematurely today.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> We haven't been shy about adding new members to<br>
> report-specified classes like Bits. This doesn't strike me as much<br>
> different.<br>
<br>
It is similar, but I think composite Storable instances are much more common than composite Bits instances.<br></blockquote><div><br></div><div>Fair, I wound up using a similar scheme for OpenGL Uniform and SSBO serialization. </div><div><br></div><div>It is kind of a mess though as you need to track not just the max alignment, you need to run through a pass computing current term alignments in order to correctly pack and compute sizeOf. Otherwise if you stuck a pair of Word16s after a Word32 you're stuck with Word32 alignment all the way through, and it doesn't even manage to resemble even the worst struct packing schemes of the C world. </div><div><br></div><div>It strikes me that relative to _that_ setup or evaluation overhead, the overhead of gluing an extra couple of methods for {peek|poke}ByteOffAddr onto a class that already has 8 fields in it is unlikely to be appreciable.</div><div><br></div><div>I could well be wrong.</div><div><br></div><div>My comment was not an attempt to shut down debate but to try to find some way under which the proposal could plausibly proceed.</div><div><br></div><div>-Edward</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Anthony<br>
<br>
><br>
> At some point in the future we can work out if it is worth it to get the<br>
> report fixed up by incorporating the Addr-based API as the default and make<br>
> the _other_ the legacy.<br>
><br>
> -Edward<br>
><br>
><br>
> On Tue, Oct 30, 2018 at 10:11 AM Sven Panne <<a href="mailto:svenpanne@gmail.com" target="_blank">svenpanne@gmail.com</a>> wrote:<br>
><br>
>> I am not sure if everybody fully comprehends what Storable is all about:<br>
>> It is meant as the lowest-level building block in an Addr-free world<br>
>> (remember: Addr is a GHCism and is *not* mentioned anywhere in the report)<br>
>> to put a few well-defined simple Haskell types into memory or read them<br>
>> from there. Its explicit non-goals are:<br>
>><br>
>>    * Achieve 100% type safety. In the presence of raw memory access,<br>
>> castPtr, C calls etc. this would be a total illusion. Forcing API users to<br>
>> sprinkle tons of castPtr at every possible place over their code wouldn't<br>
>> improve safety at all, it would only hurt readability.<br>
>><br>
>>    * Handle more complicated sum/product types. How would you do this?<br>
>> Respect your native ABI (i.e. automatically handle padding/alignment)?<br>
>> Tightly packed? Or even handle a foreign ABI? Your own ABI? Some funny<br>
>> encoding like OpenGL's packed data types? Etc. etc. You can build all of<br>
>> those things in a layer above Storable, probably introducing other type<br>
>> classes or some marshaling DSLs.<br>
>><br>
>>    * Portability of the written values. This is more in the realm of<br>
>> serialization libraries.<br>
>><br>
>> More concretely:<br>
>><br>
>> Am Di., 30. Okt. 2018 um 14:34 Uhr schrieb Daniel Cartwright <<br>
>> <a href="mailto:chessai1996@gmail.com" target="_blank">chessai1996@gmail.com</a>>:<br>
>><br>
>>> [19:26:50] <chessai_> hPutBuf :: Handle -> Ptr a -> Int -> IO () [...]<br>
>>><br>
>><br>
>> The signature for this is actually perfect: hPutBuf doesn't care about<br>
>> what stuff has been written into the given buffer, it just cares about its<br>
>> start and its size. Forcing castPtr Kung Fu here wouldn't buy you anything:<br>
>> The buffer will probably contain a wild mix of Haskell values or even no<br>
>> Haskell values at all, but that doesn't matter. Whatever you pass as "a" or<br>
>> whatever you cast from/to is probably a lie from the typing perspective. At<br>
>> this level this is no problem at all.<br>
>><br>
>><br>
>>> [19:30:02] <chessai_> peekByteOff :: Ptr b -> Int -> IO a<br>
>>> [19:30:09] <chessai_> peekByteOff :: Addr -> Int -> IO a<br>
>>> [19:30:26] <chessai_> what is 'b' doing there? it's not used in any<br>
>>> meaningful way by peekByteOff [...]<br>
>>><br>
>><br>
>> If you have a pointer pointing to something and shift that pointer by some<br>
>> bytes, you are probably pointing to something completely different, so of<br>
>> course "b" and "a" have nothing to do with each other. So peekByteOff<br>
>> intentionally ignores "b".<br>
>><br>
>><br>
>>> [19:32:22] <carter> pokeElemOff :: Ptr a -> Int -> a -> IO () --- way<br>
>>> better than peak  [...]<br>
>>><br>
>><br>
>> Yes, because this is intended to be used for *arrays* of values of the<br>
>> same type. Note "Elem" vs. "Byte".<br>
>><br>
>><br>
>>> [19:33:12] <carter> hvr: lets add safePeekByteOff :: Ptr a -> Int -> IO a<br>
>>> ? [...]<br>
>>><br>
>><br>
>> This signature doesn't make sense, see above: Shifting a pointer by an<br>
>> arbitrary amount of bytes will probably change the type of what you're<br>
>> pointing to. If you shift by units of the underlying type, well, that's<br>
>> peekElemOff.<br>
>><br>
>><br>
>>> [19:35:31] <chessai_> carter: i am glad we agree on the smell<br>
>>><br>
>><br>
>> I don't have the full chat log, but I think I don't even agree on the<br>
>> smell, at least not at the places I've seen... :-)<br>
>><br>
>><br>
>> _______________________________________________<br>
>> Libraries mailing list<br>
>> <a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
>><br>
> _______________________________________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div></div>