当前位置:网站首页>If you need a million objects
If you need a million objects
2020-11-09 23:53:00 【The way of architect's practice】
Design background
Every platform will have the basic data design of users , As the most basic user , Every user has many properties , Like gender , full name , Phone number etc. , Each user can also have an honor system like experience value , According to different experience value to correspond to different grade , Different grades correspond to different honors UI, For example, a primary user may only display one star , Second level users show two stars , And so on , Be similar to QQ Grade stars moon sun , Such an honor system is growing with the platform , There may be many types of derivatives . So here comes the question , Users need to initialize these honor values when logging in , Take the number of stars , Similar to the following code
public class Star
{
// Grade
public int Level{get ;set ;}
// The corresponding number of stars
public int StarNumber{get ;set ;}
// The corresponding star color
public int Color{get ;set ;}
... Other attributes
}
// User information
public class User{
public Star StarInfo{get ;set ;}
//... Other attributes of the user
}
// Initialize user information
User u=new User(){ StarInfo=new Star(){ Level=1, StarNumber=1,Color=1}};
Every login user will initialize a Star Property to represent the current user's Star Information , When there is 100 When ten thousand or more users are online at the same time , The same amount is instantiated in memory Star object , And other similar property objects . Can't so many duplicate objects be optimized ? Of course not. !!
Problem analysis
There's a business problem , First of all, we should analyze the problem . According to the above , The root of the problem is that there are a lot of objects , First of all, each user object has its own unique state , It's basically impossible to decompose optimization , But it's similar Star There is a way to optimize such attributes , One of the biggest commonalities of these honor attributes is immutability , let me put it another way , Grade 1 Of users Star Information will never change , Forever level=1,starnumber=1,color=1 etc. . Based on this invariance , We can take this Star Pulled out , For all levels 1 Used by , Suppose that there was 10 Ten thousand 1 Users of , The original need 10 Ten thousand objects , Now you only need one object , It's a big difference .
solve the problem
Based on the above analysis , What we need to do is reuse the object , As long as the object repeats the question , Basically, an object exit can be used to solve the problem , Similar to the following object initialization factory , But pay attention to thread safety , Because there will be multiple threads requesting and initializing objects at the same time .
public class UserStarFac
{
static object objLock = new object();
static Dictionary<int, Star> UserStarMap = new Dictionary<int, Star>();
public static Star GetUserStar(int level)
{
// Use locks to prevent multiple instantiations , Of course, it can be optimized here
lock (objLock)
{
Star info = null; ;
if(!UserStarMap.TryGetValue(level, out info))
{
info = new Star() { Color = 1, Level = 1, StarNumber = 1 };
UserStarMap.Add(level,info);
}
return info;
}
}
}
Write a simple test program
static void Main(string[] args)
{
int i = 0;
List<User> userList = new List<User>();
while (i < 100000)
{
// userList.Add(new User() { StarInfo=new Star() { Color=1, Level=1, StarNumber=1} });
userList.Add(new User() { StarInfo= UserStarFac .GetUserStar(1)});
i++;
}
Console.WriteLine(" Initialization complete ");
Console.Read();
}
Memory test results :
Do not perform any procedures : Take up memory :2.8 M
No optimization initialization 10 Ten thousand objects : Take up memory :11 M
Initialization after optimization 10 Ten thousand objects : Take up memory :7 M
Actually a small optimization is reduced 4M Memory , Don't look down on this little 4M, What you want to see is proportion , It's almost reduced 50%, In real business , There are countless places where this optimization can be done , I don't know if you care ?
This problem of a large number of duplicate objects, especially in game programming, often exists , For example, Gobang game , Initialization of chess pieces , There are millions of matches in a game hall , If the pieces in each game initialize an object , That memory usage is pretty scary , This kind of need puts the general object property , Invariant object attributes are extracted , It's necessary to share .
It is said that there is a scientific name for this optimization : The flyweight pattern , There is no need to remember the name , But you need to remember the principles and the scenarios , It must be mentioned that : Pay attention to the constant object
More wonderful articles
版权声明
本文为[The way of architect's practice]所创,转载请带上原文链接,感谢
边栏推荐
- 飞鸽传书局域网找不到其他人的问题解决
- Can public IP address and SSL certificate improve SEO?
- Gets the property value of a column in the list collection object
- Exhibition cloud technology interpretation | in the face of emergencies, how does app do a good job in crash analysis and performance monitoring?
- sql 大小写转换,去掉前后空格
- JT Jingtao project
- Assign the corresponding key and value in the map to the object
- 商品后台系统优化
- Common settings of PLSQL developer
- IP地址SSL证书
猜你喜欢
Gets the property value of a column in the list collection object
飞鸽传书局域网找不到其他人的问题解决
sql 筛选查询重复列
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
SQL filter query duplicate columns
DB engines database ranking in November: PostgreSQL holds the top spot in the same period
“wget: 无法解析主机地址”的解决方法
CRM系统能帮助企业做哪些事?
痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
异常:Invalid or unexpected token
随机推荐
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
Guest interview: Wang Jian
Common settings of PLSQL developer
asp.net Using serilog in core and customizing enrich
痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
害怕重构?都怪我太晚和你介绍该如何重构,现在我来了
自己上手写性能测试工具(二)
《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串
Youtube订阅——解决在弹窗内使用Youtube订阅按钮高度显示不全的问题
推动中国制造升级,汽车装配车间生产流水线3D可视化
z-index属性详解
函数计算进阶-IP查询工具开发
The length of the last word in leetcode
leetcode之最后一个单词的长度
利用尾巴作为时间序列进行处理来识别鲸鱼
Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop
DB engines database ranking in November: PostgreSQL holds the top spot in the same period
IP地址SSL证书
SQL case conversion, remove the space before and after