Bernd Prünster
11/18/2024, 9:34 AMephemient
11/18/2024, 7:18 PMI know for the JVM that ADD and CMIV can be made constant-time and the JVM gives you guarantees for a set of low-level operations to be constant-time (if done correctly)that is not the case as far as I know
Bernd Prünster
11/18/2024, 7:51 PMephemient
11/19/2024, 5:11 PMBernd Prünster
11/19/2024, 5:13 PMBernd Prünster
11/19/2024, 5:13 PMephemient
11/19/2024, 5:15 PMBernd Prünster
11/19/2024, 5:16 PMephemient
11/19/2024, 5:17 PMBernd Prünster
11/19/2024, 5:17 PMephemient
11/19/2024, 5:18 PMBernd Prünster
11/19/2024, 5:21 PMBernd Prünster
11/19/2024, 5:24 PMephemient
11/19/2024, 5:34 PMBernd Prünster
11/19/2024, 6:24 PMIvan Kylchik
11/27/2024, 4:40 PMconst
keyword that is used like this
const val a = 1 + 2
Compiler guarantees that 1 + 2
will be evaluated at compile time into 3
. It works the same on all targets. As for the list of supported operations, you can refer to the spec.
Also, take a look at the following case
fun main() {
println(1 + 2) // It will be evaluated into `3`, but no guarantees
}
So, some expressions are evaluated, but it is not specified.
We want to extend this feature in some future. I am really interested to hear about your use case and why you need it.Bernd Prünster
11/28/2024, 7:41 AMephemient
11/28/2024, 7:47 AMephemient
11/28/2024, 7:48 AMephemient
11/28/2024, 7:52 AMephemient
11/28/2024, 7:58 AMephemient
11/28/2024, 8:00 AMBernd Prünster
11/28/2024, 8:11 AMIvan Kylchik
11/28/2024, 10:23 AMephemient
11/28/2024, 10:30 AMephemient
11/28/2024, 10:35 AMTheso whatever you do, you're going to have to re-evaluate on each new CPUinstruction runs in time independent of its arguments in all current x86 architecture processors. [...] Future versions of the architecture may introduce new addressing modes that do not exhibit this property.CMOVcc
Ivan Kylchik
11/28/2024, 10:56 AMC doesn't provide guarantees,Yes of course. I meant that you can write some important logic using
Inline assembly
and logic without time constraints in C.
so whatever you do, you're going to have to re-evaluate on each new CPUYes, that is also that I am talking about. You should take one specific microprocessor and write your program for it with time constraints. I don't know any other way.
ephemient
11/28/2024, 11:09 AMephemient
11/28/2024, 11:19 AMJIT is not only allowed to do such optimizations, but it actually does so sometimes.
Here is a sample bug I've found in JMH, where a short circuit optimization resulted in unstable benchmark scores. JIT has optimized the evaluation ofso yeah. even if you check that the compiler's output looks like it would be constant time, and even if you measure it to be when run on some JVM under some conditions, that doesn't mean that will be under all conditions, despite that(bool == bool1 & bool == bool2)
was used rather than&
, even when&&
andbool1
were declaredbool2
.volatile
Bernd Prünster
11/28/2024, 1:45 PM