From 2e83182b67bb5e7420ac9c5720b4015ccee84c01 Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Mon, 30 Sep 2024 15:32:53 -0400 Subject: [PATCH] ctf: improve scope creation Speeds up trace reading by ~5-10%. The concurrent hashmap was used to avoid contention. But the function is stable A will always produce B. So we can use a normal hashmap. [Changed] speed up ctf parsing of lexical scopes Change-Id: I75f4b68de08c43f8ccb11a6fb6cc19c2e7d02f1e Signed-off-by: Matthew Khouzam --- .../ctf/core/event/scope/LexicalScope.java | 20 +++++++++++++++++-- .../ctf/core/event/types/Declaration.java | 12 ++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/scope/LexicalScope.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/scope/LexicalScope.java index 153098a01e..d68256874c 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/scope/LexicalScope.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/scope/LexicalScope.java @@ -13,8 +13,8 @@ package org.eclipse.tracecompass.ctf.core.event.scope; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -28,7 +28,7 @@ public class LexicalScope implements ILexicalScope { private int hash = 0; private final @NonNull String fName; private final @NonNull String fPath; - private final Map fChildren = new ConcurrentHashMap<>(); + private final Map fChildren = new HashMap<>(); /** * Hidden constructor for the root node only @@ -40,6 +40,22 @@ protected LexicalScope() { fName = ""; //$NON-NLS-1$ } + /** + * Create a scope + * @param parent + * The parent node, can be null, but shouldn't + * @param name + * the name of the field + * @return the scope + */ + public static @NonNull ILexicalScope create(ILexicalScope parent, @NonNull String name) { + ILexicalScope child = parent.getChild(name); + if( child == null) { + child = new LexicalScope(parent, name); + } + return child; + } + /** * The scope constructor * diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/Declaration.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/Declaration.java index 76b5b275b5..96ede37145 100644 --- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/Declaration.java +++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/Declaration.java @@ -43,18 +43,10 @@ public ILexicalScope getPath(IDefinitionScope definitionScope, @NonNull String f if (definitionScope != null) { final ILexicalScope parentPath = definitionScope.getScopePath(); if (parentPath != null) { - ILexicalScope myScope = parentPath.getChild(fieldName); - if (myScope == null) { - myScope = new LexicalScope(parentPath, fieldName); - } - return myScope; + return LexicalScope.create(parentPath, fieldName); } } - ILexicalScope child = ILexicalScope.ROOT.getChild(fieldName); - if (child != null) { - return child; - } - return new LexicalScope(ILexicalScope.ROOT, fieldName); + return LexicalScope.create(ILexicalScope.ROOT, fieldName); } /**