-
Notifications
You must be signed in to change notification settings - Fork 2.6k
perf: Memory optimization #4362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| @date:2025/6/9 13:42 | ||
| @desc: | ||
| """ | ||
| import json | ||
| from typing import List | ||
|
|
||
| from django.core.cache import cache | ||
|
|
@@ -19,6 +20,7 @@ | |
| from application.serializers.application_chat import ChatCountSerializer | ||
| from common.constants.cache_version import Cache_Version | ||
| from common.database_model_manage.database_model_manage import DatabaseModelManage | ||
| from common.encoder.encoder import SystemEncoder | ||
| from common.exception.app_exception import ChatException | ||
| from knowledge.models import Document | ||
| from models_provider.models import Model | ||
|
|
@@ -234,7 +236,7 @@ def to_dict(self): | |
| 'knowledge_id_list': self.knowledge_id_list, | ||
| 'exclude_document_id_list': self.exclude_document_id_list, | ||
| 'application_id': self.application_id, | ||
| 'chat_record_list': [self.chat_record_to_map(c) for c in self.chat_record_list], | ||
| 'chat_record_list': [self.chat_record_to_map(c) for c in self.chat_record_list][-20:], | ||
| 'debug': self.debug | ||
| } | ||
|
|
||
|
|
@@ -255,19 +257,19 @@ def chat_record_to_map(self, chat_record): | |
|
|
||
| @staticmethod | ||
| def map_to_chat_record(chat_record_dict): | ||
| ChatRecord(id=chat_record_dict.get('id'), | ||
| chat_id=chat_record_dict.get('chat_id'), | ||
| vote_status=chat_record_dict.get('vote_status'), | ||
| problem_text=chat_record_dict.get('problem_text'), | ||
| answer_text=chat_record_dict.get('answer_text'), | ||
| answer_text_list=chat_record_dict.get('answer_text_list'), | ||
| message_tokens=chat_record_dict.get('message_tokens'), | ||
| answer_tokens=chat_record_dict.get('answer_tokens'), | ||
| const=chat_record_dict.get('const'), | ||
| details=chat_record_dict.get('details'), | ||
| improve_paragraph_id_list=chat_record_dict.get('improve_paragraph_id_list'), | ||
| run_time=chat_record_dict.get('run_time'), | ||
| index=chat_record_dict.get('index'), ) | ||
| return ChatRecord(id=chat_record_dict.get('id'), | ||
| chat_id=chat_record_dict.get('chat_id'), | ||
| vote_status=chat_record_dict.get('vote_status'), | ||
| problem_text=chat_record_dict.get('problem_text'), | ||
| answer_text=chat_record_dict.get('answer_text'), | ||
| answer_text_list=chat_record_dict.get('answer_text_list'), | ||
| message_tokens=chat_record_dict.get('message_tokens'), | ||
| answer_tokens=chat_record_dict.get('answer_tokens'), | ||
| const=chat_record_dict.get('const'), | ||
| details=chat_record_dict.get('details'), | ||
| improve_paragraph_id_list=chat_record_dict.get('improve_paragraph_id_list'), | ||
| run_time=chat_record_dict.get('run_time'), | ||
| index=chat_record_dict.get('index'), ) | ||
|
|
||
| def set_cache(self): | ||
| cache.set(Cache_Version.CHAT.get_key(key=self.chat_id), self.to_dict(), | ||
|
|
@@ -276,15 +278,18 @@ def set_cache(self): | |
|
|
||
| @staticmethod | ||
| def map_to_chat_info(chat_info_dict): | ||
| return ChatInfo(chat_info_dict.get('chat_id'), chat_info_dict.get('chat_user_id'), | ||
| chat_info_dict.get('chat_user_type'), chat_info_dict.get('knowledge_id_list'), | ||
| chat_info_dict.get('exclude_document_id_list'), | ||
| chat_info_dict.get('application_id'), | ||
| [ChatInfo.map_to_chat_record(c_r) for c_r in chat_info_dict.get('chat_record_list')]) | ||
| c = ChatInfo(chat_info_dict.get('chat_id'), chat_info_dict.get('chat_user_id'), | ||
| chat_info_dict.get('chat_user_type'), chat_info_dict.get('knowledge_id_list'), | ||
| chat_info_dict.get('exclude_document_id_list'), | ||
| chat_info_dict.get('application_id'), | ||
| debug=chat_info_dict.get('debug')) | ||
| c.chat_record_list = [ChatInfo.map_to_chat_record(c_r) for c_r in chat_info_dict.get('chat_record_list')] | ||
| return c | ||
|
|
||
| @staticmethod | ||
| def get_cache(chat_id): | ||
| chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id), version=Cache_Version.CHAT_INFO.get_version()) | ||
| chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id), | ||
| version=Cache_Version.CHAT_INFO.get_version()) | ||
| if chat_info_dict: | ||
| return ChatInfo.map_to_chat_info(chat_info_dict) | ||
| return None | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no major issues with the provided code. However, a few optimizations can be made:
Here is an optimized version of the relevant part: class YourClassName:
@staticmethod
def map_to_chat_info(chat_info_dict):
# Using fstring for string interpolation
c = ChatInfo(
chat_id=chat_info_dict['chat_id'],
chat_user_id=chat_info_dict['chat_user_id'],
chat_user_type=chat_info_dict['chat_user_type'],
knowledge_id_list=chat_info_dict['knowledge_id_list'],
exclude_document_id_list=chat_info_dict['exclude_document_id_list'],
application_id=chat_info_dict['application_id']
)
# Assign directly using dict unpacking
c.chat_record_list = [
ChatInfo.map_to_chat_record(c_r)
for cr in chat_info_dict['chat_record_list']
]
return c
@staticmethod
def get_cache(chat_id):
chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id), version=Cache_Version.CHAT_INFO.get_version())
if chat_info_dict:
c = ChatInfo(*list(chat_info_dict.values()))
c.chat_record_list = [
ChatInfo(map_to_chat_record(cr))
for cr in chat_info_dict['chat_record_list']
]
return c
return NoneAdditional Suggestions:
These changes should make the code slightly more efficient and easier to maintain. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided Python code snippet seems to be part of a logging system with classes
HandlerandNodeResult. However, there are several potential concerns and areas for improvement:Empty
self.chat_infoAssignment: The last line of thehandlermethod setsself.chat_infotoNone, but this assignment does not serve any purpose if it's followed by other operations that depend on having a value. Consider removing or commenting out the empty assignment before usingself.chat_info.Potential Infinite Loop: Assuming you have logic in
handlerthat might cause an infinite loop due to certain conditions (untested), ensure such loops are appropriately handled.Database Transactions: If database interactions are involved, consider enclosing them within transaction blocks to ensure atomicity, particularly if changes could fail halfway through execution.
Unused Class: If the
class NodeResult:is only defined at the end of the file, remove it since it doesn't appear to contribute anything useful here.Here’s a suggested revision: