Skip to content

Commit 5eaa31c

Browse files
dougqhclaude
andcommitted
Lazily allocate HashMap in PathMatchingHttpServletRequestWrapper
The wrapper is created on every request to isolate attribute side effects during handler mapping lookups. Previously, a HashMap was eagerly allocated even when no handler mapping matched (and thus no attributes were set). This defers the HashMap allocation to the first setAttribute call, avoiding the cost of a HashMap with its 16-entry Node array on requests that don't match any handler mapping. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3fb3733 commit 5eaa31c

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

dd-java-agent/instrumentation/spring/spring-webmvc/spring-webmvc-3.1/src/main/java/datadog/trace/instrumentation/springweb/PathMatchingHttpServletRequestWrapper.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,35 @@
66
import javax.servlet.http.HttpServletRequestWrapper;
77

88
class PathMatchingHttpServletRequestWrapper extends HttpServletRequestWrapper {
9-
private final Map<String, Object> localAttributes = new HashMap<>();
9+
private Map<String, Object> localAttributes;
1010

1111
public PathMatchingHttpServletRequestWrapper(HttpServletRequest request) {
1212
super(request);
1313
}
1414

1515
@Override
1616
public Object getAttribute(String name) {
17-
final Object ret = localAttributes.get(name);
18-
if (ret == null) {
19-
return super.getAttribute(name);
17+
if (localAttributes != null) {
18+
final Object ret = localAttributes.get(name);
19+
if (ret != null) {
20+
return ret;
21+
}
2022
}
21-
return ret;
23+
return super.getAttribute(name);
2224
}
2325

2426
@Override
2527
public void setAttribute(String name, Object o) {
28+
if (localAttributes == null) {
29+
localAttributes = new HashMap<>();
30+
}
2631
localAttributes.put(name, o);
2732
}
2833

2934
@Override
3035
public void removeAttribute(String name) {
31-
localAttributes.remove(name);
36+
if (localAttributes != null) {
37+
localAttributes.remove(name);
38+
}
3239
}
3340
}

dd-java-agent/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/src/main/java17/datadog/trace/instrumentation/springweb6/PathMatchingHttpServletRequestWrapper.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,35 @@
66
import java.util.Map;
77

88
class PathMatchingHttpServletRequestWrapper extends HttpServletRequestWrapper {
9-
private final Map<String, Object> localAttributes = new HashMap<>();
9+
private Map<String, Object> localAttributes;
1010

1111
public PathMatchingHttpServletRequestWrapper(HttpServletRequest request) {
1212
super(request);
1313
}
1414

1515
@Override
1616
public Object getAttribute(String name) {
17-
final Object ret = localAttributes.get(name);
18-
if (ret == null) {
19-
return super.getAttribute(name);
17+
if (localAttributes != null) {
18+
final Object ret = localAttributes.get(name);
19+
if (ret != null) {
20+
return ret;
21+
}
2022
}
21-
return ret;
23+
return super.getAttribute(name);
2224
}
2325

2426
@Override
2527
public void setAttribute(String name, Object o) {
28+
if (localAttributes == null) {
29+
localAttributes = new HashMap<>();
30+
}
2631
localAttributes.put(name, o);
2732
}
2833

2934
@Override
3035
public void removeAttribute(String name) {
31-
localAttributes.remove(name);
36+
if (localAttributes != null) {
37+
localAttributes.remove(name);
38+
}
3239
}
3340
}

0 commit comments

Comments
 (0)