You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "qianye1001 (via GitHub)" <gi...@apache.org> on 2026/06/10 09:35:11 UTC

[I] [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize [rocketmq]

qianye1001 opened a new issue, #10460:
URL: https://github.com/apache/rocketmq/issues/10460

   ### Before Creating the Enhancement Request
   
   - [x] I have confirmed that this should be classified as an enhancement rather than a bug/feature.
   
   ### Summary
   
   `ProxyContext` (in `org.apache.rocketmq.proxy.common`) uses a `HashMap` with the default initial capacity (16) and default load factor (0.75) to hold context entries. The resize threshold is therefore `12`.
   
   In typical request paths, the context is populated with the 10 standard keys defined in `ContextVariable` (`remote-address`, `local-address`, `client-id`, `channel`, `language`, `client-version`, `remaining-ms`, `action`, `protocol-type`, `namespace`) plus additional entries injected via `withVal(...)` from various processors / interceptors. As soon as the entry count exceeds 12, the underlying `HashMap` triggers a rehash/resize on the hot request path, which is unnecessary overhead given that the upper bound of entries is well-known.
   
   ### Motivation
   
   `ProxyContext` is created on every inbound request (gRPC / Remoting), so this resize cost is paid per RPC. Pre-sizing the map eliminates a guaranteed reallocation + rehash on a hot path with effectively zero downside (the extra memory footprint is negligible, ~256 bytes per context).
   
   ### Describe the Solution You'd Like
   
   Initialize the internal map with an explicit initial capacity (suggest `64`) so that under the default load factor the threshold (`48`) comfortably exceeds the realistic upper bound of context entries, and no resize will happen on the request path.
   
   ```java
   // proxy/src/main/java/org/apache/rocketmq/proxy/common/ProxyContext.java
   private static final int DEFAULT_INITIAL_CAPACITY = 64;
   private final Map<String, Object> value = new HashMap<>(DEFAULT_INITIAL_CAPACITY);
   ```
   
   ### Describe Alternatives You've Considered
   
   - Keep using the default capacity — rejected, incurs a deterministic resize on every request once entries cross 12.
   - Use a smaller capacity such as 32 — works for most cases but still risks resize when interceptors/plugins push more values; 64 leaves comfortable headroom while staying small.
   - Switch to a different map implementation (e.g. `IdentityHashMap`, array-backed) — out of scope; preserving `HashMap` semantics with a tuned initial capacity is the minimal change.
   
   ### Additional Context
   
   File: `proxy/src/main/java/org/apache/rocketmq/proxy/common/ProxyContext.java`
   
   Related code:
   
   - `ProxyContext#value` field — the map being resized.
   - `ContextVariable` — defines 10 well-known keys, all of which are usually set during a single request lifecycle.
   
   Happy to send a PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize [rocketmq]

Posted by "tiagoejoaquim110717-hash (via GitHub)" <gi...@apache.org>.
tiagoejoaquim110717-hash commented on issue #10460:
URL: https://github.com/apache/rocketmq/issues/10460#issuecomment-4700972497

   <!-- Failed to upload "IMG-20250325-WA0000.jpg" -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize [rocketmq]

Posted by "oss-sentinel-ai (via GitHub)" <gi...@apache.org>.
oss-sentinel-ai commented on issue #10460:
URL: https://github.com/apache/rocketmq/issues/10460#issuecomment-4668695366

   loongsuite-site-bootstrap: started successfully (OpenTelemetry auto-instrumentation initialized).
   "**Issue Evaluation**\n\nCategory: `type/enhancement` | Status: **Confirmed**\n\nVerified against the current codebase on `develop` branch.\n\n**Analysis:**\n- `ProxyContext#value` is initialized as `new HashMap<>()` (default capacity 16, resize threshold 12 at 0.75 load factor).\n- `ContextVariable` defines 10 well-known keys, all typically populated per request.\n- With interceptors/plugins adding more entries, the map reliably crosses the threshold of 12 on every request, triggering a resize on the hot path.\n\n**Assessment:**\n- **Feasibility:** Straightforward \u2014 single-line change with no behavioral impact.\n- **Scope:** `proxy/src/main/java/org/apache/rocketmq/proxy/common/ProxyContext.java` only.\n- **Compatibility:** No breaking changes. The proposed capacity of 64 (threshold 48) provides comfortable headroom.\n- **Impact:** Eliminates a deterministic rehash on every gRPC/Remoting request. Negligible memory overhead (~256 bytes per context).\n\nThis is a valid and wel
 l-motivated optimization. The suggested approach (initial capacity = 64) is reasonable.\n\n---\n*Automated evaluation by oss-sentinel-ai*\n"


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize [rocketmq]

Posted by "oss-sentinel-ai (via GitHub)" <gi...@apache.org>.
oss-sentinel-ai commented on issue #10460:
URL: https://github.com/apache/rocketmq/issues/10460#issuecomment-4668712910

   **Issue Evaluation**
   
   Category: `type/enhancement` | Status: **Confirmed**
   
   Verified against the current codebase on `develop` branch.
   
   **Analysis:**
   - `ProxyContext#value` is initialized as `new HashMap<>()` (default capacity 16, resize threshold 12 at 0.75 load factor).
   - `ContextVariable` defines 10 well-known keys, all typically populated per request.
   - With interceptors/plugins adding more entries, the map reliably crosses the threshold of 12 on every request, triggering a resize on the hot path.
   
   **Assessment:**
   - **Feasibility:** Straightforward — single-line change with no behavioral impact.
   - **Scope:** `proxy/src/main/java/org/apache/rocketmq/proxy/common/ProxyContext.java` only.
   - **Compatibility:** No breaking changes. The proposed capacity of 64 (threshold 48) provides comfortable headroom.
   - **Impact:** Eliminates a deterministic rehash on every gRPC/Remoting request. Negligible memory overhead (~256 bytes per context).
   
   This is a valid and well-motivated optimization. The suggested approach (initial capacity = 64) is reasonable.
   
   ---
   *Automated evaluation by oss-sentinel-ai*


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize [rocketmq]

Posted by "tiagoejoaquim110717-hash (via GitHub)" <gi...@apache.org>.
tiagoejoaquim110717-hash commented on issue #10460:
URL: https://github.com/apache/rocketmq/issues/10460#issuecomment-4700975256

   <!-- Failed to upload "image (3).png" -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize [rocketmq]

Posted by "RongtongJin (via GitHub)" <gi...@apache.org>.
RongtongJin closed issue #10460: [Enhancement] Set initial capacity for ProxyContext internal HashMap to avoid resize
URL: https://github.com/apache/rocketmq/issues/10460


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org