Merge pull request #1236 from zed-industries/reduce-server-memory-footprint

Antonio Scandurra created

Reduce memory footprint on server

Change summary

crates/collab/src/rpc.rs       | 25 +++++++------------------
crates/collab/src/rpc/store.rs | 17 +++++++----------
crates/rpc/src/peer.rs         |  2 +-
3 files changed, 15 insertions(+), 29 deletions(-)

Detailed changes

crates/collab/src/rpc.rs 🔗

@@ -228,8 +228,8 @@ impl Server {
                 );
                 span.in_scope(|| {
                     tracing::info!(
-                        payload = format!("{:?}", envelope.payload).as_str(),
-                        "message payload"
+                        payload_type = envelope.payload_type_name(),
+                        "message received"
                     );
                 });
                 let future = (handler)(server, *envelope);
@@ -1682,21 +1682,6 @@ impl<'a> Drop for StoreWriteGuard<'a> {
     fn drop(&mut self) {
         #[cfg(test)]
         self.check_invariants();
-
-        let metrics = self.metrics();
-
-        METRIC_CONNECTIONS.set(metrics.connections as _);
-        METRIC_REGISTERED_PROJECTS.set(metrics.registered_projects as _);
-        METRIC_ACTIVE_PROJECTS.set(metrics.active_projects as _);
-        METRIC_SHARED_PROJECTS.set(metrics.shared_projects as _);
-
-        tracing::info!(
-            connections = metrics.connections,
-            registered_projects = metrics.registered_projects,
-            active_projects = metrics.active_projects,
-            shared_projects = metrics.shared_projects,
-            "metrics"
-        );
     }
 }
 
@@ -1802,7 +1787,11 @@ pub async fn handle_websocket_request(
 
 pub async fn handle_metrics(Extension(server): Extension<Arc<Server>>) -> axum::response::Response {
     // We call `store_mut` here for its side effects of updating metrics.
-    server.store_mut().await;
+    let metrics = server.store().await.metrics();
+    METRIC_CONNECTIONS.set(metrics.connections as _);
+    METRIC_REGISTERED_PROJECTS.set(metrics.registered_projects as _);
+    METRIC_ACTIVE_PROJECTS.set(metrics.active_projects as _);
+    METRIC_SHARED_PROJECTS.set(metrics.shared_projects as _);
 
     let encoder = prometheus::TextEncoder::new();
     let metric_families = prometheus::gather();

crates/collab/src/rpc/store.rs 🔗

@@ -1,10 +1,6 @@
 use crate::db::{self, ChannelId, ProjectId, UserId};
 use anyhow::{anyhow, Result};
-use collections::{
-    btree_map,
-    hash_map::{self, Entry},
-    BTreeMap, BTreeSet, HashMap, HashSet,
-};
+use collections::{btree_map, hash_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet};
 use rpc::{proto, ConnectionId, Receipt};
 use serde::Serialize;
 use std::{
@@ -18,11 +14,11 @@ use tracing::instrument;
 
 #[derive(Default, Serialize)]
 pub struct Store {
-    connections: HashMap<ConnectionId, ConnectionState>,
-    connections_by_user_id: HashMap<UserId, HashSet<ConnectionId>>,
+    connections: BTreeMap<ConnectionId, ConnectionState>,
+    connections_by_user_id: BTreeMap<UserId, HashSet<ConnectionId>>,
     projects: BTreeMap<ProjectId, Project>,
     #[serde(skip)]
-    channels: HashMap<ChannelId, Channel>,
+    channels: BTreeMap<ChannelId, Channel>,
 }
 
 #[derive(Serialize)]
@@ -60,7 +56,7 @@ pub struct Worktree {
     pub root_name: String,
     pub visible: bool,
     #[serde(skip)]
-    pub entries: HashMap<u64, proto::Entry>,
+    pub entries: BTreeMap<u64, proto::Entry>,
     #[serde(skip)]
     pub extension_counts: HashMap<String, usize>,
     #[serde(skip)]
@@ -210,7 +206,7 @@ impl Store {
     pub fn leave_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) {
         if let Some(connection) = self.connections.get_mut(&connection_id) {
             connection.channels.remove(&channel_id);
-            if let hash_map::Entry::Occupied(mut entry) = self.channels.entry(channel_id) {
+            if let btree_map::Entry::Occupied(mut entry) = self.channels.entry(channel_id) {
                 entry.get_mut().connection_ids.remove(&connection_id);
                 if entry.get_mut().connection_ids.is_empty() {
                     entry.remove();
@@ -596,6 +592,7 @@ impl Store {
             for worktree in project.worktrees.values_mut() {
                 worktree.diagnostic_summaries.clear();
                 worktree.entries.clear();
+                worktree.extension_counts.clear();
             }
         }
 

crates/rpc/src/peer.rs 🔗

@@ -24,7 +24,7 @@ use std::{
 };
 use tracing::instrument;
 
-#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize)]
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize)]
 pub struct ConnectionId(pub u32);
 
 impl fmt::Display for ConnectionId {