8295863: JFR: Use expected size for Maps and Sets

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2022-10-26 03:04:14 +00:00
parent 3bd3caf897
commit 324bec19aa
14 changed files with 31 additions and 31 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -105,7 +105,7 @@ public final class EventFactory {
List<AnnotationElement> sanitizedAnnotation = Utils.sanitizeNullFreeList(annotationElements, AnnotationElement.class); List<AnnotationElement> sanitizedAnnotation = Utils.sanitizeNullFreeList(annotationElements, AnnotationElement.class);
List<ValueDescriptor> sanitizedFields = Utils.sanitizeNullFreeList(fields, ValueDescriptor.class); List<ValueDescriptor> sanitizedFields = Utils.sanitizeNullFreeList(fields, ValueDescriptor.class);
Set<String> nameSet = new HashSet<>(); Set<String> nameSet = HashSet.newHashSet(sanitizedFields.size());
for (ValueDescriptor v : sanitizedFields) { for (ValueDescriptor v : sanitizedFields) {
String name = v.getName(); String name = v.getName();
if (v.isArray()) { if (v.isArray()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -78,7 +78,7 @@ public final class EventType {
Objects.requireNonNull(name, "name"); Objects.requireNonNull(name, "name");
if (cache == null) { if (cache == null) {
List<ValueDescriptor> fields = getFields(); List<ValueDescriptor> fields = getFields();
Map<String, ValueDescriptor> newCache = new LinkedHashMap<>(fields.size()); Map<String, ValueDescriptor> newCache = LinkedHashMap.newLinkedHashMap(fields.size());
for (ValueDescriptor v :fields) { for (ValueDescriptor v :fields) {
newCache.put(v.getName(), v); newCache.put(v.getName(), v);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -102,7 +102,7 @@ public final class MetadataEvent {
private void calculateDelta() { private void calculateDelta() {
List<EventType> added = new ArrayList<>(); List<EventType> added = new ArrayList<>();
Map<Long, EventType> previousSet = new HashMap<>(previous.size()); Map<Long, EventType> previousSet = HashMap.newHashMap(previous.size());
for (EventType eventType : previous) { for (EventType eventType : previous) {
previousSet.put(eventType.getId(), eventType); previousSet.put(eventType.getId(), eventType);
} }

View File

@ -163,8 +163,8 @@ public final class RecordingFile implements Closeable {
List<Type> readTypes() throws IOException { List<Type> readTypes() throws IOException {
ensureOpen(); ensureOpen();
MetadataDescriptor previous = null; MetadataDescriptor previous = null;
List<Type> types = new ArrayList<>(); List<Type> types = new ArrayList<>(200);
HashSet<Long> foundIds = new HashSet<>(); HashSet<Long> foundIds = HashSet.newHashSet(types.size());
try (RecordingInput ri = new RecordingInput(file, FileAccess.UNPRIVILEGED)) { try (RecordingInput ri = new RecordingInput(file, FileAccess.UNPRIVILEGED)) {
ChunkHeader ch = new ChunkHeader(ri); ChunkHeader ch = new ChunkHeader(ri);
ch.awaitFinished(); ch.awaitFinished();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -140,7 +140,7 @@ public final class MetadataLoader {
} }
private final List<TypeElement> types; private final List<TypeElement> types;
private final Map<String, List<AnnotationElement>> anotationElements = new HashMap<>(20); private final Map<String, List<AnnotationElement>> anotationElements = HashMap.newHashMap(16);
private final Map<String, AnnotationElement> categories = new HashMap<>(); private final Map<String, AnnotationElement> categories = new HashMap<>();
MetadataLoader(DataInputStream dis) throws IOException { MetadataLoader(DataInputStream dis) throws IOException {
@ -204,7 +204,7 @@ public final class MetadataLoader {
} }
private Map<String, AnnotationElement> buildRelationMap(Map<String, Type> typeMap) { private Map<String, AnnotationElement> buildRelationMap(Map<String, Type> typeMap) {
Map<String, AnnotationElement> relationMap = new HashMap<>(20); Map<String, AnnotationElement> relationMap = HashMap.newHashMap(10);
for (TypeElement t : types) { for (TypeElement t : types) {
if (t.isRelation) { if (t.isRelation) {
Type relationType = typeMap.get(t.name); Type relationType = typeMap.get(t.name);
@ -272,8 +272,8 @@ public final class MetadataLoader {
} }
private Map<String, Type> buildTypeMap() { private Map<String, Type> buildTypeMap() {
Map<String, Type> typeMap = new HashMap<>(2 * types.size()); Map<String, Type> typeMap = HashMap.newHashMap(types.size());
Map<String, Type> knownTypeMap = new HashMap<>(20); Map<String, Type> knownTypeMap = HashMap.newHashMap(16);
for (Type kt : Type.getKnownTypes()) { for (Type kt : Type.getKnownTypes()) {
typeMap.put(kt.getName(), kt); typeMap.put(kt.getName(), kt);
knownTypeMap.put(kt.getName(), kt); knownTypeMap.put(kt.getName(), kt);

View File

@ -57,8 +57,8 @@ public final class MetadataRepository {
private static final JVM jvm = JVM.getJVM(); private static final JVM jvm = JVM.getJVM();
private static final MetadataRepository instance = new MetadataRepository(); private static final MetadataRepository instance = new MetadataRepository();
private final List<EventType> nativeEventTypes = new ArrayList<>(100); private final List<EventType> nativeEventTypes = new ArrayList<>(150);
private final List<EventControl> nativeControls = new ArrayList<EventControl>(100); private final List<EventControl> nativeControls = new ArrayList<EventControl>(nativeEventTypes.size());
private final TypeLibrary typeLibrary = TypeLibrary.getInstance(); private final TypeLibrary typeLibrary = TypeLibrary.getInstance();
private final SettingsManager settingsManager = new SettingsManager(); private final SettingsManager settingsManager = new SettingsManager();
private final Map<String, Class<? extends Event>> mirrors = new HashMap<>(); private final Map<String, Class<? extends Event>> mirrors = new HashMap<>();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -78,11 +78,11 @@ final class MetadataWriter {
} }
public void writeBinary(DataOutput output) throws IOException { public void writeBinary(DataOutput output) throws IOException {
Set<String> stringPool = new HashSet<>(1000); Set<String> stringPool = HashSet.newHashSet(2500);
// Possible improvement, sort string by how often they occur. // Possible improvement, sort string by how often they occur.
// and assign low number to the most frequently used. // and assign low number to the most frequently used.
buildStringPool(root, stringPool); buildStringPool(root, stringPool);
HashMap<String, Integer> lookup = new LinkedHashMap<>(stringPool.size()); HashMap<String, Integer> lookup = LinkedHashMap.newLinkedHashMap(stringPool.size());
int index = 0; int index = 0;
int poolSize = stringPool.size(); int poolSize = stringPool.size();
writeInt(output, poolSize); writeInt(output, poolSize);

View File

@ -80,7 +80,7 @@ final class SettingsManager {
private void addToMap(Map<String, Set<String>> map, String attribute, String value) { private void addToMap(Map<String, Set<String>> map, String attribute, String value) {
Set<String> values = map.get(attribute); Set<String> values = map.get(attribute);
if (values == null) { if (values == null) {
values = new HashSet<String>(5); values = HashSet.newHashSet(4);
map.put(attribute, values); map.put(attribute, values);
} }
values.add(value); values.add(value);
@ -173,7 +173,7 @@ final class SettingsManager {
} }
private Map<String, InternalSetting> createSettingsMap(List<Map<String,String>> activeSettings) { private Map<String, InternalSetting> createSettingsMap(List<Map<String,String>> activeSettings) {
Map<String, InternalSetting> map = new LinkedHashMap<>(activeSettings.size()); Map<String, InternalSetting> map = LinkedHashMap.newLinkedHashMap(activeSettings.size());
for (Map<String, String> rec : activeSettings) { for (Map<String, String> rec : activeSettings) {
for (InternalSetting internal : makeInternalSettings(rec)) { for (InternalSetting internal : makeInternalSettings(rec)) {
InternalSetting is = map.get(internal.getSettingsId()); InternalSetting is = map.get(internal.getSettingsId());
@ -188,7 +188,7 @@ final class SettingsManager {
} }
private Collection<InternalSetting> makeInternalSettings(Map<String, String> rec) { private Collection<InternalSetting> makeInternalSettings(Map<String, String> rec) {
Map<String, InternalSetting> internals = new LinkedHashMap<>(); Map<String, InternalSetting> internals = LinkedHashMap.newLinkedHashMap(rec.size());
for (Map.Entry<String, String> entry : rec.entrySet()) { for (Map.Entry<String, String> entry : rec.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();

View File

@ -63,7 +63,7 @@ public final class TypeLibrary {
private static TypeLibrary instance; private static TypeLibrary instance;
private static boolean implicitFieldTypes; private static boolean implicitFieldTypes;
private static final Map<Long, Type> types = new LinkedHashMap<>(100); private static final Map<Long, Type> types = LinkedHashMap.newLinkedHashMap(350);
static final ValueDescriptor DURATION_FIELD = createDurationField(); static final ValueDescriptor DURATION_FIELD = createDurationField();
static final ValueDescriptor THREAD_FIELD = createThreadField(); static final ValueDescriptor THREAD_FIELD = createThreadField();
static final ValueDescriptor STACK_TRACE_FIELD = createStackTraceField(); static final ValueDescriptor STACK_TRACE_FIELD = createStackTraceField();
@ -301,7 +301,7 @@ public final class TypeLibrary {
} }
private static void addUserFields(Class<?> clazz, Type type, List<ValueDescriptor> dynamicFields) { private static void addUserFields(Class<?> clazz, Type type, List<ValueDescriptor> dynamicFields) {
Map<String, ValueDescriptor> dynamicFieldSet = new HashMap<>(); Map<String, ValueDescriptor> dynamicFieldSet = HashMap.newHashMap(dynamicFields.size());
for (ValueDescriptor dynamicField : dynamicFields) { for (ValueDescriptor dynamicField : dynamicFields) {
dynamicFieldSet.put(dynamicField.getName(), dynamicField); dynamicFieldSet.put(dynamicField.getName(), dynamicField);
} }

View File

@ -461,7 +461,7 @@ public final class Utils {
} }
public static Map<String, String> sanitizeNullFreeStringMap(Map<String, String> settings) { public static Map<String, String> sanitizeNullFreeStringMap(Map<String, String> settings) {
HashMap<String, String> map = new HashMap<>(settings.size()); HashMap<String, String> map = HashMap.newHashMap(settings.size());
for (Map.Entry<String, String> e : settings.entrySet()) { for (Map.Entry<String, String> e : settings.entrySet()) {
String key = e.getKey(); String key = e.getKey();
if (key == null) { if (key == null) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -177,7 +177,7 @@ public final class EventLog {
// Therefore, create a lookup by event type ID to see if is a JDK/JVM event. // Therefore, create a lookup by event type ID to see if is a JDK/JVM event.
private static void ensureSystemEventLookup() { private static void ensureSystemEventLookup() {
if (systemEventLookup == null) { if (systemEventLookup == null) {
systemEventLookup = new HashSet<>(); systemEventLookup = HashSet.newHashSet(200);
for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) { for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
PlatformEventType pe = PrivateAccess.getInstance().getPlatformEventType(type); PlatformEventType pe = PrivateAccess.getInstance().getPlatformEventType(type);
if (pe.isSystem()) { if (pe.isSystem()) {

View File

@ -232,7 +232,7 @@ final class DCmdStart extends AbstractDCmd {
} }
private LinkedHashMap<String, String> configureStandard(String[] settings) throws DCmdException { private LinkedHashMap<String, String> configureStandard(String[] settings) throws DCmdException {
LinkedHashMap<String, String> s = new LinkedHashMap<>(); LinkedHashMap<String, String> s = LinkedHashMap.newLinkedHashMap(settings.length);
for (String configName : settings) { for (String configName : settings) {
try { try {
s.putAll(JFC.createKnown(configName).getSettings()); s.putAll(JFC.createKnown(configName).getSettings());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -73,7 +73,7 @@ public final class ConfigurationInfo {
if (o instanceof TabularData) { if (o instanceof TabularData) {
TabularData td = (TabularData) o; TabularData td = (TabularData) o;
Collection<?> values = td.values(); Collection<?> values = td.values();
Map<String, String> map = new HashMap<>(values.size()); Map<String, String> map = HashMap.newHashMap(values.size());
for (Object value : td.values()) { for (Object value : td.values()) {
if (value instanceof CompositeData) { if (value instanceof CompositeData) {
CompositeData cdRow = (CompositeData) value; CompositeData cdRow = (CompositeData) value;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -316,7 +316,7 @@ final class FlightRecorderMXBeanImpl extends StandardEmitterMBean implements Fli
public Map<String, String> getRecordingOptions(long recording) throws IllegalArgumentException { public Map<String, String> getRecordingOptions(long recording) throws IllegalArgumentException {
MBeanUtils.checkMonitor(); MBeanUtils.checkMonitor();
Recording r = getExistingRecording(recording); Recording r = getExistingRecording(recording);
Map<String, String> options = new HashMap<>(10); Map<String, String> options = HashMap.newHashMap(10);
options.put(OPTION_DUMP_ON_EXIT, String.valueOf(r.getDumpOnExit())); options.put(OPTION_DUMP_ON_EXIT, String.valueOf(r.getDumpOnExit()));
options.put(OPTION_DISK, String.valueOf(r.isToDisk())); options.put(OPTION_DISK, String.valueOf(r.isToDisk()));
options.put(OPTION_NAME, String.valueOf(r.getName())); options.put(OPTION_NAME, String.valueOf(r.getName()));