Should C functions be called FFI-safely on big inputs?
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- haskell
- 领域
- performance
调研方向
从 src/Data/Text/Encoding.hs 中的 encodeUtf8 和 foreign imports 开始,然后检查 cryptohash-sha256 中类似的基于大小的分支。使用 benchmark suite,同时注意它缺少多线程练习,以调查对大型 Text 值进行编码或解码是否会阻止垃圾回收。完成的标准是确定大型输入是否需要 safe FFI calls,并记录或实现受支持的方法。
由索引模型根据 Issue 内容生成。
描述
Something like
diff --git a/src/Data/Text/Encoding.hs b/src/Data/Text/Encoding.hs
index 239e15e..9b6a01c 100644
--- a/src/Data/Text/Encoding.hs
+++ b/src/Data/Text/Encoding.hs
@@ -436,7 +436,10 @@ encodeUtf8 (Text arr off len)
fp <- mallocByteString (len*3) -- see https://github.com/haskell/text/issues/194 for why len*3 is enough
withForeignPtr fp $ \ptr ->
with ptr $ \destPtr -> do
- c_encode_utf8 destPtr (A.aBA arr) (fromIntegral off) (fromIntegral len)
+ -- When ByteArray# is large enough, it is pinned.
+ -- Then we can use safe FFI calls, which we indeed want for big inputs!
+ (if len > 4096 then c_encode_utf8_safe else c_encode_utf8_unsafe)
+ destPtr (A.aBA arr) (fromIntegral off) (fromIntegral len)
newDest <- peek destPtr
let utf8len = newDest `minusPtr` ptr
if utf8len >= len `shiftR` 1
@@ -535,5 +538,8 @@ foreign import ccall unsafe "_hs_text_decode_utf8_state" c_decode_utf8_with_stat
foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
:: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()
-foreign import ccall unsafe "_hs_text_encode_utf8" c_encode_utf8
+foreign import ccall unsafe "_hs_text_encode_utf8" c_encode_utf8_unsafe
+ :: Ptr (Ptr Word8) -> ByteArray# -> CSize -> CSize -> IO ()
+
+foreign import ccall safe "_hs_text_encode_utf8" c_encode_utf8_safe
:: Ptr (Ptr Word8) -> ByteArray# -> CSize -> CSize -> IO ()
The benchmark suite doesn't have exercises for multithreaded programs.
As far as I understand the code now, encoding or decoding big Text will prevent GC from running. Is that an issue, hard to tell.
E.g. cryptohash-sha256 branches on size: https://hackage.haskell.org/package/cryptohash-sha256-0.11.102.0/docs/src/Crypto.Hash.SHA256.html#c_sha256_update, (my diff above is probably written better in that way too)
- 主要语言
- Haskell
- 星标
- 424
- 派生
- 167
- 平均合并
- 1 天 21 小时
- 30 天内合并 PR
- 6
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
haskell/text 的其他 Issue
-
Relax constraints on decimal / hexadecimal from Data.Text.Read and Data.Text.Lazy.Read可能已有人在做 @Lysxia 于 148 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 64/100
-
难度 3/5 1-2 天 新手友好度 38/100
-
难度 3/5 1-2 天 新手友好度 42/100
-
难度 2/5 1-3 小时 新手友好度 45/100
-
Shrinking is Insanely Slow for `write_read`可能已有人在做 @BebeSparkelSparkel 于 926 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 30/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
haskell/bytestring#730 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
seal-harness/seal-harness#399 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 65/100
fmidue/codeworld-tasks#124 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
reflex-frp/reflex#563 ·