在Java中,我可以决定我的字段存储在哪里:寄存器、缓存还是内存中?

huangapple go评论131阅读模式
英文:

In Java, can I decide where is my field stored: in register, cache or RAM?

问题

我知道这是自动完成的 - 数据被更频繁访问时,它存储得离处理器更近。但我是否可以通过Java语法来影响它们的放置呢?从我理解的"Volatile"来看,它将数据放置在三级缓存或者RAM中,因为它对所有线程都是可见的,这样对吗?

英文:

I know that it is done automatically - the more frequently a piece of data is accessed, the closer to the processor it is stored. But can I somehow influence their placement with Java syntax? Volatile, the way I understand it, puts data in level 3 cache or RAM since it's visible to all the threads, is that right?

答案1

得分: 6

不,Java语法不允许直接访问硬件。Java语言和虚拟机规范是管理Java代码解释的契约,明确指定其针对的是虚拟机而不是实际的硬件。

来自第1.2节

> Java虚拟机是Java平台的基石。它是技术的组成部分,负责实现其与硬件和操作系统的独立性,编译代码的小巧尺寸以及保护用户免受恶意程序的影响。
>
> Java虚拟机是一个抽象的计算机。类似于真实的计算机,它具有一套指令,并在运行时操作各种内存区域。使用虚拟机实现编程语言是相当常见的;最知名的虚拟机可能是UCSD Pascal的P-Code虚拟机。

甚至没有必要让Java虚拟机拥有可访问的寄存器或缓存。从规范的角度来看,图灵机同样可以实现一个符合规范的Java虚拟机。

英文:

No, Java syntax does not allow direct access to the hardware. The Java language and virtual machine specification is the contract governing how Java code is interpreted - and it is explicitly written to target a Virtual Machine instead of an actual one.

From Section 1.2:

> The Java Virtual Machine is the cornerstone of the Java platform. It
> is the component of the technology responsible for its hardware- and
> operating system-independence, the small size of its compiled code,
> and its ability to protect users from malicious programs.
>
> The Java Virtual Machine is an abstract computing machine. Like a real
> computing machine, it has an instruction set and manipulates various
> memory areas at run time. It is reasonably common to implement a
> programming language using a virtual machine; the best-known virtual
> machine may be the P-Code machine of UCSD Pascal.

There is no need for a Java VM to even have accessible registers or caches. From the point of view of the specs, a Turing Machine could very well implement a conformant Java VM.

答案2

得分: 1

Java在很大程度上在优化方面的工作方式不同。开发者在代码中说出做什么。然后,在运行时,即时编译器查看正在发生的情况,然后(如果需要)将“慢速”的Java字节码转化为高度优化的机器码。

换句话说:JIT 决定哪些代码值得优化。这可能包括优化的数据布局

但正如前面所说:作为开发者,您在其中无法干涉

英文:

Java works differently regarding optimisations to a large degree. You the the developer say what to do in your code. Then, at runtime, the just in time compiler looks at what is going on, and then (if necessary) translates "slow" java byte code into highly optimized machine code.

In other words: the JIT decides what code is worth optimizing. That might include optimized "data layouting".

But as said: you as a developer have "no say" in this.

答案3

得分: 0

你无法控制这种行为。

如果CPU读取对象的字段,则该对象会被拉入L1d缓存中。这与字段是否是volatile无关。

字段是被访问一次还是多次都无关紧要;它仍然会最终出现在L1d缓存中。除非使用非临时加载/存储;但是从Java中无法访问此行为。

volatile关键字可以防止在编译器、CPU和内存子系统级别上重新排序指令。在X86的情况下,由于X86的TSO内存模型,volatile读取是免费的(获取语义)。volatile写入是通过停止前端执行加载指令直到存储缓冲区被清空来实现的。这可以防止将较旧的存储与较新的加载重新排序到不同的地址。

欲了解更多信息,请参见:
https://shipilev.net/blog/2014/on-the-fence-with-dependencies/

英文:

You can't control this behavior.

If the CPU reads a field of an object the object is pulled into the L1d. This is independent of the field being volatile or not.

It doesn't matter if a field is accessed only once or many times; it will still end up in the L1d. Unless you have a non temporal load/store; but this behavior is not accessible from Java.

Volatile prevents reordering of instructions on both compiler and CPU/memory-sub-system level. In case of the X86, the volatile read you get for free (acquire semantics) due to the TSO memory model of X86. The volatile write is implemented by stopping the front-end from executing loads till the store buffer has been drained. This prevents the reordering of older stores with newer loads to a different address.

For more information see:
https://shipilev.net/blog/2014/on-the-fence-with-dependencies/

huangapple
  • 本文由 发表于 2020年10月6日 17:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定