> Going forward, converting older JDK code to use the relatively new FFM API 
> requires system calls that can provide `errno` and the likes to explicitly 
> allocate a MemorySegment to capture potential error states. This can lead to 
> negative performance implications if not designed carefully and also 
> introduces unnecessary code complexity.
> 
> Hence, this PR proposes to add a _JDK internal_ method handle adapter that 
> can be used to handle system calls with `errno`, `GetLastError`, and 
> `WSAGetLastError`.
> 
> It currently relies on a thread-local cache of MemorySegments to allide 
> allocations. If, in the future, a more efficient thread-associated allocation 
> scheme becomes available, we could easily migrate to that one.
> 
> Here are some benchmarks:
> 
> 
> Benchmark                                        Mode  Cnt   Score   Error  
> Units
> CaptureStateUtilBench.explicitAllocationFail     avgt   30  43.942 ? 2.425  
> ns/op
> CaptureStateUtilBench.explicitAllocationSuccess  avgt   30  23.606 ? 0.837  
> ns/op
> CaptureStateUtilBench.threadLocalFail            avgt   30  15.660 ? 0.073  
> ns/op
> CaptureStateUtilBench.threadLocalReuseSuccess    avgt   30  12.712 ? 0.407  
> ns/op
> 
> 
> Explicit allocation:
> 
>         try (var arena = Arena.ofConfined()) {
>             return (int) HANDLE.invoke(arena.allocate(4), 0, 0);
>         }
> 
> 
> Thread Local (tl):
> 
>         return (int) ADAPTED_HANDLE.invoke(arena.allocate(4), 0, 0);
> 
> 
> The graph below shows the difference in latency for a successful call:
> 
> ![image](https://github.com/user-attachments/assets/fb0051b7-a16c-4636-bf5c-27c01a45c8fe)
> 
> This is a ~2x improvement on the happy path.
> 
> 
> Tested and passed tiers 1-3.

Per Minborg has updated the pull request with a new target base due to a merge 
or a rebase. The incremental webrev excludes the unrelated changes brought in 
by the merge/rebase. The pull request contains 29 additional commits since the 
last revision:

 - Merge branch 'master' into errno-util
 - Refactor
 - Wip
 - Update benchmark and micro optimize
 - Clean up benchmark
 - Fix allocation problem in benchmark
 - Add benchmark
 - Remove thread test
 - Clean up
 - Add VT pinning
 - ... and 19 more: https://git.openjdk.org/jdk/compare/19ec23ad...3b8723f3

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/22391/files
  - new: https://git.openjdk.org/jdk/pull/22391/files/57b322a1..3b8723f3

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=22391&range=09
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=22391&range=08-09

  Stats: 21152 lines in 1016 files changed: 8980 ins; 7017 del; 5155 mod
  Patch: https://git.openjdk.org/jdk/pull/22391.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/22391/head:pull/22391

PR: https://git.openjdk.org/jdk/pull/22391

Reply via email to