TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Boxed types in OCaml, and other thoughts

58 点作者 mkristiansen将近 2 年前

7 条评论

reikonomusha将近 2 年前
This code is what OCaml needs to do in order to arithmetic in a function call. This isn&#x27;t code for &quot;just&quot; x*x, but rather it&#x27;s code for &quot;multiply two ints passed into a function and return the product as an int&quot;.<p>Implementations like SBCL have a similar overhead <i>for a function call containing arithmetic</i>. However, if you&#x27;re doing more arithmetic within the function, the bit shifts etc. are only done once as a part of the function&#x27;s pre&#x2F;postamble. In fact, fully unboxed 64-bit arithmetic with integers or floats can be done if these values aren&#x27;t crossing function boundaries or into the garbage-collected heap. (The latter, a memory read&#x2F;write, is way more costly than a bit shift anyway.)<p>The term of art is &quot;inlining&quot; or &quot;open-coding&quot; arithmetic. Raw arithmetic can be open-coded, but data formats must be respected across function call boundaries and in the heap. Therefore, if you inline the function containing the arithmetic, you eliminate the pre&#x2F;postamble overhead too. This is the same as in C-like languages as well, it&#x27;s just that their pre&#x2F;postambles are different.<p>&quot;Block&quot; or &quot;whole-program compilation&quot; can also sometimes eliminate these extra instructions, at some modest expenses (compile wall clock time, implementation complexity, increased size of compilation units, multiple function entry points, modularity, etc.).<p>OCaml&#x27;s built-in Flambda [1,2] is a project&#x2F;technology that will automatically leverage some of the above techniques (especially inlining) to achieve higher performance.<p>[1] <a href="https:&#x2F;&#x2F;v2.ocaml.org&#x2F;manual&#x2F;flambda.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;v2.ocaml.org&#x2F;manual&#x2F;flambda.html</a><p>[2] <a href="https:&#x2F;&#x2F;blog.janestreet.com&#x2F;flambda&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;blog.janestreet.com&#x2F;flambda&#x2F;</a>
评论 #36549133 未加载
DonPellegrino将近 2 年前
This top bit is used by the garbage collector for performance. OCaml&#x27;s GC is light enough that it simply does not feel like it exists at all. Integers also do not cause allocations.<p>Jane Street&#x27;s blog explains the reasoning in detail[0]<p>[0] <a href="https:&#x2F;&#x2F;blog.janestreet.com&#x2F;what-is-gained-and-lost-with-63-bit-integers&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;blog.janestreet.com&#x2F;what-is-gained-and-lost-with-63-...</a>
评论 #36543951 未加载
评论 #36545477 未加载
rwmj将近 2 年前
This is a better explanation: <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20180403212547&#x2F;https:&#x2F;&#x2F;caml.inria.fr&#x2F;pub&#x2F;ml-archives&#x2F;caml-list&#x2F;2004&#x2F;07&#x2F;e86a25aa6c6a6a7d08dd7eb50cfd5d52.fr.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20180403212547&#x2F;https:&#x2F;&#x2F;caml.inri...</a><p>You&#x27;d be better off measuring actual performance on real world tasks before inferring anything. Modern CPUs overlap execution of instructions very aggressively.
hayley-patton将近 2 年前
You can use fewer instructions if you tag integers with a zero (as SBCL does); then 2x * 2y = 4xy, which we only need to shift right one bit to get 4xy&#x2F;2 i.e 2xy. But this takes a bit off your fixnum size, so you might shift an operand right, which also is just one instruction (as (2x&#x2F;2) * 2y = x * 2y = 2xy).
Ericson2314将近 2 年前
Yeah it&#x27;s pretty bad, but Jane Street is working on adding unboxed types.
评论 #36543792 未加载
评论 #36553068 未加载
Solvency将近 2 年前
I&#x27;m confused.<p>In OCaml, the type system is strong and statically typed, ergo the type of a value is known at compile-time.<p>So...why would it need to do this to check if the type is an integer?
评论 #36545779 未加载
评论 #36549141 未加载
评论 #36554074 未加载
评论 #36548844 未加载
mkristiansen将近 2 年前
I came across an interesting fact about OCaml and wrote up my thoughts.
评论 #36546040 未加载