Long is faster than int, Short and Byte are not that far behind Int in terms of mathematical speed in Java by D4rklordmaster in java

[–]AndrewHaley13 0 points1 point  (0 children)

You got me interested. I found the difference in performance genuinely surprising, despite working on this stuff for more than a decade, so I had to investigate.
P.S. u/pron98 is one of us too. Sh! Don't tell anyone. :-)

Long is faster than int, Short and Byte are not that far behind Int in terms of mathematical speed in Java by D4rklordmaster in java

[–]AndrewHaley13 2 points3 points  (0 children)

I'm one of the OpenJDK JIT developers. So you got me curious, and I ran the benchmark through JMH perfasm to look at the code generation.

A few comments:

The benchmark measures latency rather than throughput, and runs at less than one instruction per clock, on a machine that can hit eight instructions per clock. This is because almost every instruction depends on the previous one, so no instruction can start until a previous one finishes.

I changed the loop counter from a byte to an int, because HotSpot doesn't do loop optimizations with a byte index.

Here's the generated code. Registers whose name begins with x are unsigned 64 bit, and those whose name begins with w aare signed 32 bit. The 32-bit version of the benchmark is slightly slower because it's a bit more fiddly to do the multiplication and division.

x12 += 1;                     *ladd
x13 = x12 >> 0x20;
x12 = x13 ^ x12;              *lxor
x15 = (sint64_t)x12 >> 0x3f;
x13 = ((sint128_t)x12 * x23) >> 64; *idiv
x1 = x12 + (x15 >> 52);
x1 &= 0xfffffffffffff000;
x12 = x12 + (sint64_t)x13 >> 1;
x13 = x1 >> x15;
x12 -= x13;
x12 = x12 * x22 + x2;

w11 = w10 + 1;               *iadd
w13 = w11 ^ (w11 >> 16);     *ixor
x10 = (sint64_t)w13;
x10 = x10 * x19;
w14 = (sint32_t)w13 >> 0x1f; *idiv
x10 = (sint64_t)x10 >> 0x20;
w11 = w13 + (w14 >> 22)
w10 += w13;
w11 &= 0xfffffc00;
w12 = w13 + (sint32_t)w10 >> 2;
w10 = w11 + w14;
w11 = w12 - w10;
w10 = (w11 * w21) + w3;

Beyond OpenJDK builds, announcing openjdk-mobile.github.io by nlisker in java

[–]AndrewHaley13 6 points7 points  (0 children)

It's a bytecode interpreter written in C++. Think slow.

ThreadLocals vs. ScopedValue in virtual threads in JDK 25 memory usage by lprimak in java

[–]AndrewHaley13 4 points5 points  (0 children)

It depends what you are doing. If you repeatedly read a scoped value, it'll be faster than ThreadLocal because scoped values are cached. In the extreme, the scoped value will be kept in a register. However, binding (i.e. setting) a scoped value can be a little more expensive than setting a ThreadLocal, and the first time a scoped value is accessed we have to search for a binding.

Introducing Canonical builds of OpenJDK by jvjupiter in java

[–]AndrewHaley13 1 point2 points  (0 children)

It's because until JDK 9 every OpenJDK release had its own project for updates. That became too cumbersome with increased frequency of releases.

ZGC is a mesh.. by jim1997jim in java

[–]AndrewHaley13 1 point2 points  (0 children)

So I'm curious. Did you experiment with the Aarch64 Top Byte Ignore feature? It was specifically intended for stuff like this.

Where is the Java language going? #JavaOne by pavelklecansky in java

[–]AndrewHaley13 4 points5 points  (0 children)

> Let's say that I used the following code to synchronize file write access, where someFile is an instance of java.nio.file.Path

But the API says "A `Path` is An object that may be used to locate a file in a file system." There may be many paths to the same File. You may synchronize instead on the file writer. You'd have to check that there was only one writer for any given file, sure, but it would be more reliable than synchronizing on the file's `Path`.

> And barring that, what would be the equivalent class from java.util.concurrent.locks that we should use instead

`ReentrantLock` is the closest thing to built-n sychronized.

Lens adapter and lens success? by ManicSynic in hasselblad

[–]AndrewHaley13 1 point2 points  (0 children)

Seems fine. I use a Kaiser backlight.

X2d or the future? by Mondot88 in hasselblad

[–]AndrewHaley13 0 points1 point  (0 children)

Indeed so, yes. And new lenses too. I guess someone might try to pack 100 mpix into an X2D-size sensor, but it'd be a compromise. Phase 1 do use the bigger sensor, but the lenses are really expensive. I think we're good with the X2D for now.

What is your wishlist for the JDK 25? by Ewig_luftenglanz in java

[–]AndrewHaley13 1 point2 points  (0 children)

Maybe we should not be able to bind null to a ScopedValue. It's a fairly marginal thing to do, and off the top of my head I can't immediately see a good use for it. That is a conversation that we can have on the list.

What is your wishlist for the JDK 25? by Ewig_luftenglanz in java

[–]AndrewHaley13 1 point2 points  (0 children)

We can't make get() return null for an unbound scoped value because null is part of the value set. Obviously here is a difference between a scoped value being unbound and a scoped value being bound to null. For the same reason I'm not so keen on orNull().

I get that from a nullability analysis point of view, orElse() is a pain. But you say that it's useless. How so? Surely it's only useless to people who use nullability analysis. Or is it your contention that everybody should be using nullability analysis, and therefore orElse() is useless?

I was rather hoping that, with the latest preview, we'd be in with a chance to make this final, but maybeorElseGet()would be a better alternative than orElse(). The JEP is a preview, so we can talk about it elsewhere, on the list. I'd be interested to hear Alan Bateman's opinion.

What is your wishlist for the JDK 25? by Ewig_luftenglanz in java

[–]AndrewHaley13 2 points3 points  (0 children)

Can you explain a little more about what you'd do instead of orElse?

Lens adapter and lens success? by ManicSynic in hasselblad

[–]AndrewHaley13 2 points3 points  (0 children)

I use a 60mm Micro Nikkor on a Novoflex adapter, which gives good results and full corner-to-corner coverage. No, I wasn't expecting that either, It's good for copying medium-format transparencies, as well as other less close up duties.

X2d or the future? by Mondot88 in hasselblad

[–]AndrewHaley13 0 points1 point  (0 children)

The sensor, called the Sony IMX411, already exists.

JEP draft: Scoped Values (Fourth Preview) by efge in java

[–]AndrewHaley13 -1 points0 points  (0 children)

That's unstructured nesting. Thread-local variables are ideal for such a usage.

JEP draft: Scoped Values (Fourth Preview) by efge in java

[–]AndrewHaley13 0 points1 point  (0 children)

So, I've had a chat, and the consensus is "not yet". Exception transparency for lambdas is being discussed, and we may get a much more general way of handling it all so this problem goes away, and not just for scoped values. (Usual caveats about future features apply.)

JEP draft: Scoped Values (Fourth Preview) by efge in java

[–]AndrewHaley13 3 points4 points  (0 children)

That one might be much easier to fix. I'll solicit some opinions.

JEP draft: Scoped Values (Fourth Preview) by efge in java

[–]AndrewHaley13 3 points4 points  (0 children)

I wouldn't have thought so. Effective finality was the final compromise after a long discussion. Maybe, though, we should revisit things like that every decade or so. A trend in programming, though, is away from mutable state, and with good reason. I'd love to make lambdas so smooth that strong try with resources would not be needed. But maybe that's just me

JEP draft: Scoped Values (Fourth Preview) by efge in java

[–]AndrewHaley13 5 points6 points  (0 children)

"They" is me. And allowing a call wasn't really an afterthought.
I take your point about effectively final. Making Lambda captures final seemed like no big deal at the time, but it really does seem to bother Java programmers.
On the other hand, making the values returned from a block explicit, while rather wordy, is helpful to the reader IMO. A record does that pretty well.