codeant-ai-for-open-source[bot] commented on code in PR #40931:
URL: https://github.com/apache/superset/pull/40931#discussion_r3387195936


##########
superset-frontend/packages/superset-ui-core/src/currency-format/symbolPosition.ts:
##########
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { getCurrencyLocale } from './currencyLocale';
+
+export type SymbolPosition = 'prefix' | 'suffix';
+
+const NUMERIC_PART_TYPES = new Set<Intl.NumberFormatPartTypes>([
+  'integer',
+  'group',
+  'decimal',
+  'fraction',
+]);
+
+/**
+ * Resolve where the currency symbol should be placed relative to the value.
+ *
+ * An explicit `prefix`/`suffix` is always honored. When the position is unset,
+ * it is derived from the locale's own convention for that currency via
+ * `Intl.NumberFormat` (e.g. `$1` in `en-US` is a prefix, `1 €` in `fr-FR` is a
+ * suffix). Unknown currency codes fall back to `prefix`, the most common
+ * convention worldwide.
+ */
+export function resolveSymbolPosition(
+  currencyCode: string | undefined,
+  symbolPosition?: string,
+  locale: string = getCurrencyLocale(),
+): SymbolPosition {
+  if (symbolPosition === 'prefix' || symbolPosition === 'suffix') {
+    return symbolPosition;
+  }
+
+  if (currencyCode) {
+    try {
+      const parts = new Intl.NumberFormat(locale, {
+        style: 'currency',
+        currency: currencyCode,
+      }).formatToParts(1);

Review Comment:
   **Suggestion:** `resolveSymbolPosition` constructs a new `Intl.NumberFormat` 
and calls `formatToParts` on every invocation, and this function is called 
inside `CurrencyFormatter.format` (a hot per-value path). On large charts this 
adds avoidable CPU overhead and can noticeably slow rendering. Cache resolved 
positions by `(locale, currencyCode)` (or cache `Intl.NumberFormat` instances) 
so repeated values do not redo expensive locale parsing. [performance]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Table chart currency cells incur extra formatting CPU overhead.
   - ⚠️ Large tables with currency render slower due to Intl.
   - ⚠️ Server and browser CPU usage slightly increased for currency charts.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure a Table chart with a numeric metric and a currency format in 
the Superset UI;
   this flows into `processColumns` in
   `plugins/plugin-chart-table/src/transformProps.ts:236-319`, where a 
`DataColumnMeta` for
   that metric is built with `formatter = new CurrencyFormatter({ d3Format: 
numberFormat,
   currency: resolvedCurrency })` (lines 258-318).
   
   2. When the chart renders, each cell is formatted via `formatColumnValue` in
   `plugins/plugin-chart-table/src/utils/formatValue.ts:65-88`, which passes the
   `CurrencyFormatter` instance as the `formatter` argument and calls the inner 
`formatValue`
   helper for every cell.
   
   3. Inside `formatValue` (`formatValue.ts:52-56`), when `formatter` is a
   `CurrencyFormatter`, it calls `formatter(value as number, rowData, 
currencyColumn)`, which
   invokes `CurrencyFormatter.format` in
   `packages/superset-ui-core/src/currency-format/CurrencyFormatter.ts:81-168` 
once per cell
   value.
   
   4. In `CurrencyFormatter.format`, each invocation calls 
`resolveSymbolPosition` for that
   value (lines 132-137 and 157-162), and `resolveSymbolPosition` in
   `packages/superset-ui-core/src/currency-format/symbolPosition.ts:40-55` 
constructs a new
   `Intl.NumberFormat(locale, { style: 'currency', currency: currencyCode })` 
and immediately
   calls `formatToParts(1)` on every call, causing repeated formatter 
construction and locale
   parsing on this hot per-cell path; with large tables and many currency 
cells, this extra
   work accumulates into noticeable CPU overhead that could be avoided by 
caching resolved
   `(locale, currencyCode) -> SymbolPosition` or `Intl.NumberFormat` instances.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=127bc08cd866424fac459b38c20d1484&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=127bc08cd866424fac459b38c20d1484&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset-frontend/packages/superset-ui-core/src/currency-format/symbolPosition.ts
   **Line:** 51:54
   **Comment:**
        *Performance: `resolveSymbolPosition` constructs a new 
`Intl.NumberFormat` and calls `formatToParts` on every invocation, and this 
function is called inside `CurrencyFormatter.format` (a hot per-value path). On 
large charts this adds avoidable CPU overhead and can noticeably slow 
rendering. Cache resolved positions by `(locale, currencyCode)` (or cache 
`Intl.NumberFormat` instances) so repeated values do not redo expensive locale 
parsing.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40931&comment_hash=efb7cd9fc52f36c1de3ce4bc6a18f56731feebcaa0444b8e574411ac1a6dca75&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40931&comment_hash=efb7cd9fc52f36c1de3ce4bc6a18f56731feebcaa0444b8e574411ac1a6dca75&reaction=dislike'>👎</a>



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to