如何避免在我的数据结构中出现嵌套的映射分配?

huangapple go评论81阅读模式
英文:

How to avoid nested map allocations in my data structure?

问题

我有一个结构体,其中有一个嵌套的CustomersIndex映射,它分配了一堆内部映射,导致内存增加。我进行了性能分析,所以我注意到了这一点。我想知道是否有任何方法可以重新设计我的CustomersIndex数据结构,而不使用嵌套映射?

以下是在我的代码中填充它的方式:

const (
	departmentsKey = "departments"
)

type CustomerManifest struct {
	Customers      []definitions.Customer
	CustomersIndex map[int]map[int]definitions.Customer
}

这是在我的下面代码中对其进行填充的方式:

func updateData(mdmCache *mdm.Cache) map[string]interface{} {
	memCache := mdmCache.MemCache()

	var customers []definitions.Customer
	var customersIndex = map[int]map[int]definitions.Customer{}

	for _, r := range memCache.Customer {
		customer := definitions.Customer{
			Id:           int(r.Id),
			SetId:        int(r.DepartmentSetId),
		}

		customers = append(customers, customer)
		_, yes := customersIndex[customer.SetId]
		if !yes {
			customersIndex[customer.SetId] = make(map[int]definitions.Customer)
		}
		customersIndex[customer.SetId][customer.Id] = customer
	}

	return map[string]interface{}{
		departmentsKey: &CustomerManifest{Customers: customers, CustomersIndex: customersIndex},
	}
}

这是我获取CustomersIndex嵌套映射的方式:

func (c *Client) GetCustomerIndex() map[int]map[int]definitions.Customer {
	c.mutex.RLock()
	defer c.mutex.RUnlock()
	customersIndex := c.data[departmentsKey].(*CustomerManifest).CustomersIndex
	return customersIndex
}

有没有办法设计我的CustomersIndex,而不使用嵌套映射?

英文:

I have a below struct where I have a nested map for CustomersIndex which allocates bunch of internal maps causing memory increase. I profiled it so I noticed this. I am trying to see if there is any way to redesign my CustomersIndex data structure which doesn't uses nested map?

const (
	departmentsKey = "departments"
)

type CustomerManifest struct {
	Customers      []definitions.Customer
	CustomersIndex map[int]map[int]definitions.Customer
}

This is the way it is being populated here in my below code:

func updateData(mdmCache *mdm.Cache) map[string]interface{} {
	memCache := mdmCache.MemCache()

	var customers []definitions.Customer
	var customersIndex = map[int]map[int]definitions.Customer{}

	for _, r := range memCache.Customer {
		customer := definitions.Customer{
			Id:           int(r.Id),
			SetId:        int(r.DepartmentSetId),
		}

		customers = append(customers, customer)
		_, yes := customersIndex[customer.SetId]
		if !yes {
			customersIndex[customer.SetId] = make(map[int]definitions.Customer)
		}
		customersIndex[customer.SetId][customer.Id] = customer
	}

	return map[string]interface{}{
		departmentsKey: &CustomerManifest{Customers: customers, CustomersIndex: customersIndex},
	}
}

And this is the way I am getting my CustomersIndex nested map.

func (c *Client) GetCustomerIndex() map[int]map[int]definitions.Customer {
	c.mutex.RLock()
	defer c.mutex.RUnlock()
	customersIndex := c.data[departmentsKey].(*CustomerManifest).CustomersIndex
	return customersIndex
}

Is there any way to design my CustomersIndex in a way where I don't have to use nested map?

答案1

得分: 1

直到你往其中放入值之前,你不需要分配一个地图。

type CustomerManifest struct {
    Customers      []definitions.Customer
    CustomersIndex map[int]map[int]definitions.Customer
}

func (m *CustomerManifest) AddCustomerDefinition(x, y int, customer definitions.Customer) {
    // 获取现有的地图,如果存在。
    innerMap := m.CustomersIndex[x]
    // 如果不存在,分配它。
    if innerMap == nil {
        innerMap = make(map[int]definitions.Customer)
        m.CustomersIndex[x] = innerMap
    }
    // 将值添加到现在存在的内部地图中。
    innerMap[y] = customer
}
英文:

You don't need to allocate a map until you put values in it.

type CustomerManifest struct {
    Customers      []definitions.Customer
    CustomersIndex map[int]map[int]definitions.Customer
}

func (m *CustomerManifest) AddCustomerDefinition(x, y int, customer definitions.Customer) {
    // Get the existing map, if exists.
    innerMap := m.CustomersIndex[x]
    // If it doesn't exist, allocate it.
    if innerMap == nil {
        innerMap = make(map[int]definitions.Customer)
        m.CustomersIndex[x] = innerMap
    }
    // Add the value to the inner map, which now exists.
    innerMap[y] = customer
}

</details>



huangapple
  • 本文由 发表于 2022年6月25日 21:28:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/72754246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定