Skip to content

Commit

Permalink
Use VarHandle for shared data owner
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLaskey committed Mar 11, 2024
1 parent bc2075a commit 1036c04
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
package java.lang.runtime;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.function.Supplier;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import jdk.internal.vm.annotation.Stable;

Expand All @@ -37,6 +38,21 @@
* constructed at a specific {@link java.lang.invoke.CallSite CallSite}.
*/
final class StringTemplateSharedData {
/**
* owner field {@link VarHandle}.
*/
private static final VarHandle OWNER_VH;

static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
OWNER_VH = lookup.findVarHandle(StringTemplateSharedData.class, "owner", Object.class);
} catch (ReflectiveOperationException ex) {
throw new InternalError(ex);
}
}


/**
* List of string fragments for the string template. This value of this list is shared by
* all instances created at the {@link java.lang.invoke.CallSite CallSite}.
Expand Down Expand Up @@ -80,7 +96,7 @@ final class StringTemplateSharedData {
* have a fallback if it does not win the cache.
*/
@Stable
private final AtomicReference<Object> owner;
private Object owner;

/**
* Metadata cache.
Expand All @@ -103,7 +119,7 @@ final class StringTemplateSharedData {
this.types = types;
this.valuesMH = valuesMH;
this.joinMH = joinMH;
this.owner = new AtomicReference<>(null);
this.owner = null;
this.metaData = null;

}
Expand Down Expand Up @@ -143,11 +159,10 @@ MethodHandle joinMH() {
return joinMH;
}


/**
* Get processor meta data.
* Get owner meta data.
*
* @param owner owner object, should be unique to the processor
* @param owner owner object, should be unique to the processor
* @param supplier supplier of meta data
* @return meta data
*
Expand All @@ -156,12 +171,10 @@ MethodHandle joinMH() {
*/
@SuppressWarnings("unchecked")
<S, T> T getMetaData(S owner, Supplier<T> supplier) {
boolean isOwner = this.owner.get() == owner;
Object temp = isOwner && metaData != null ? metaData : supplier.get();
if (!isOwner && this.owner.compareAndExchange(null, owner) == null) {
metaData = temp;
if (this.owner == null && (Object)OWNER_VH.compareAndExchange(this, null, owner) == null) {
metaData = supplier.get();
}
return (T)temp;
return this.owner == owner ? (T)metaData : null;
}

}
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/util/FormatterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private MethodHandle formatSpecifier(FormatSpecifier fs, Class<?> ptype) {
* @return concat {@link MethodHandle} for based on format
*/
private MethodHandle buildFilters(List<FormatString> fsa,
List<String> segments,
List<String> fragments,
MethodHandle[] filters) {
MethodHandle mh = null;
int iParam = 0;
Expand All @@ -558,7 +558,7 @@ private MethodHandle buildFilters(List<FormatString> fsa,
}
break;
case 0: // ordinary index
segments.add(segment.toString());
fragments.add(segment.toString());
segment.setLength(0);

if (iParam < ptypes.length) {
Expand All @@ -574,7 +574,7 @@ private MethodHandle buildFilters(List<FormatString> fsa,
}
}

segments.add(segment.toString());
fragments.add(segment.toString());

return mh;
}
Expand Down

0 comments on commit 1036c04

Please sign in to comment.