英文:
Write barrier when swapping array elements
问题
在由ocamlopt
生成的AMD64汇编代码中,主路径中出现了以下行两次:
call caml_modify@PLT
让我想知道...为什么在数组元素v_i
和v_j
的存活性保持不变时会有两个写屏障?
(我简单地认为OCaml中的(非浮点数)数组是一些不可变长度的块,在标记阶段由GC遍历。)
或者:是否有一种特殊的OCaml习惯用法可以避免上述写屏障?
英文:
Consider the following code:
let swap_array_elements (arr:string array) (i:int) (j:int) =
let v_i = arr.(i)
and v_j = arr.(j) in
arr.(i) <- v_j;
arr.(j) <- v_i
In the AMD64 assembly code produced by ocamlopt
the line below occurs twice in the main path:
call caml_modify@PLT
Makes me wonder... why are there two write barriers when the liveness of the array elements v_i
and v_j
remains the same?
(I'm under the simplistic impression that (non-float) arrays in OCaml are blocks of some immutable length which are traversed by the GC in the marking phase.)
Or: is there a special OCaml idiom that allows me to avoid above write barriers?
答案1
得分: 4
以下是翻译好的内容:
由于您进行了两次独立的“写入”,据我所知,无法避免两次写入障碍。
特别是,由于交换不是原子性的,不能保证写入的值在“写入”时仍然在数组中。
此外,写入障碍还会跟踪写入主堆内的次堆(也称为幼儿堆)的指针,而交换可能会传播这些指针。
英文:
Since you are doing two separate write
s, there are no way to avoid two write barrier as far as I know.
In particular, since the swap is not atomic, there is no guaranteed that the values written are still in the array at the time of of the write
s.
There is moreover the issue that the write barrier also tracks pointers to the minor heap (aka the nursery heap) written inside the major heap, and the swap might spread those pointers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论