Use BTreeMap in Server so we release memory when maps are cleared

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-06-23 18:02:17 +02:00
parent 63df644d8f
commit 555847449b
2 changed files with 8 additions and 11 deletions

View file

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

View file

@ -24,7 +24,7 @@ use std::{
}; };
use tracing::instrument; 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); pub struct ConnectionId(pub u32);
impl fmt::Display for ConnectionId { impl fmt::Display for ConnectionId {