Copilot commented on code in PR #62086:
URL: https://github.com/apache/doris/pull/62086#discussion_r3031350072


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/CompactTabletMetaStore.java:
##########
@@ -0,0 +1,229 @@
+// 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.
+
+package org.apache.doris.catalog;
+
+import org.apache.doris.thrift.TStorageMedium;
+
+import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Memory-compact storage for TabletMeta using Structure-of-Arrays layout.
+ *
+ * Instead of storing one TabletMeta object per tablet (each with a 16-byte 
Java object header),
+ * this class stores each field in a parallel primitive array indexed by an 
internal slot.
+ * A Long2IntOpenHashMap maps tabletId to the slot index.
+ *
+ * Deleted slots are reused via a free list embedded in the dbIds array.
+ *
+ * Thread safety: callers must hold appropriate locks (provided by 
TabletInvertedIndex).
+ */
+public class CompactTabletMetaStore {
+
+    private static final int INITIAL_CAPACITY = 1024;
+    private static final int ABSENT = -1;
+
+    // tabletId -> slot index
+    private Long2IntOpenHashMap tabletIdToSlot;
+
+    // parallel arrays indexed by slot
+    private long[] dbIds;
+    private long[] tableIds;
+    private long[] partitionIds;
+    private long[] indexIds;
+    private int[] oldSchemaHashes;
+    private int[] newSchemaHashes;
+    private byte[] storageMediumOrdinals;
+

Review Comment:
   `newSchemaHashes` is allocated and grown alongside the other arrays, but it 
is never read or exposed, and `TabletMeta` currently has no getter for 
`newSchemaHash`. This adds per-tablet memory overhead and undermines the 
compaction goal.
   
   Either remove `newSchemaHashes` entirely for now, or fully wire it through 
(capture the value on add and expose a getter / include it when constructing 
`TabletMeta`).



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/TabletInvertedIndex.java:
##########
@@ -126,10 +126,10 @@ public List<TabletMeta> getTabletMetaList(List<Long> 
tabletIdList) {
     public void addTablet(long tabletId, TabletMeta tabletMeta) {
         long stamp = writeLock();
         try {
-            if (tabletMetaMap.containsKey(tabletId)) {
+            if (tabletMetaStore.containsKey(tabletId)) {
                 return;
             }

Review Comment:
   `addTablet()` checks `tabletMetaStore.containsKey(tabletId)` and then calls 
`tabletMetaStore.add(tabletId, tabletMeta)`, but `CompactTabletMetaStore.add()` 
also checks for duplicates. This does two hash lookups for every add.
   
   Consider removing the outer `containsKey()` check and rely on 
`tabletMetaStore.add()` to no-op on duplicates, or change 
`CompactTabletMetaStore.add()` to return a boolean and avoid duplicate lookups.
   ```suggestion
   
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/TabletInvertedIndex.java:
##########
@@ -101,7 +100,7 @@ public void tabletReport(long backendId, Map<Long, TTablet> 
backendTablets,
     public TabletMeta getTabletMeta(long tabletId) {
         long stamp = readLock();
         try {
-            return tabletMetaMap.get(tabletId);
+            return tabletMetaStore.getTabletMeta(tabletId);
         } finally {
             readUnlock(stamp);
         }

Review Comment:
   `getTabletMeta()` now constructs a new `TabletMeta` object on every call via 
`tabletMetaStore.getTabletMeta()`. `TabletInvertedIndex.getTabletMeta()` is 
used broadly across FE (rebalancers, report handler, proc nodes, etc.), so this 
can introduce significant allocation/GC overhead and may offset some of the 
memory win.
   
   Consider adding allocation-free accessors on 
`TabletInvertedIndex`/`CompactTabletMetaStore` (e.g., 
`getDbId/getTableId/getPartitionId/getIndexId/getOldSchemaHash/getStorageMedium`)
 and migrating hot call sites to those, or providing a cached/flyweight 
`TabletMeta` for read-only access.



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/LocalTabletInvertedIndex.java:
##########
@@ -896,7 +896,7 @@ public Map<TStorageMedium, Long> 
getReplicaNumByBeIdAndStorageMedium(long backen
             Map<Long, Replica> replicaMetaWithBackend = 
backingReplicaMetaTable.get(backendId);
             if (replicaMetaWithBackend != null) {
                 for (long tabletId : replicaMetaWithBackend.keySet()) {
-                    if (tabletMetaMap.get(tabletId).getStorageMedium() == 
TStorageMedium.HDD) {
+                    if 
(tabletMetaStore.getTabletMeta(tabletId).getStorageMedium() == 
TStorageMedium.HDD) {

Review Comment:
   In `getReplicaNumByBeIdAndStorageMedium()`, 
`tabletMetaStore.getTabletMeta(tabletId)` is called inside the loop solely to 
read `storageMedium`, which allocates a new `TabletMeta` per tablet. This is a 
hot path (iterating all tablets on a backend) and can add significant GC 
pressure.
   
   Use `tabletMetaStore.getStorageMedium(tabletId)` instead (as is already done 
in `getTabletSizeByBackendIdAndStorageMedium`) to avoid per-iteration object 
allocation.
   ```suggestion
                       if (tabletMetaStore.getStorageMedium(tabletId) == 
TStorageMedium.HDD) {
   ```



-- 
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