The invention provides a code optimization method and
system based on a
knowledge graph, and the method comprises the steps: carrying out the
source code analysis based on a to-be-optimized code
library, constructing an
abstract syntax tree, and determining a to-be-optimized
code snippet based on the code complexity of a corresponding code in the
abstract syntax tree; determining a reconstruction mode corresponding to each to-be-optimized
code snippet based on the to-be-optimized
code snippet in combination with a pre-constructed code optimization
knowledge graph, and constructing a context
dependency graph of each to-be-optimized code
snippet based on the
abstract syntax tree and the code optimization
knowledge graph corresponding to each to-be-optimized code
snippet; performing sliding window
decomposition based on the context
dependency graph to obtain a plurality of mutually associated code blocks, and performing simplification and fusion on the code blocks to obtain a current context; and inputting the reconstruction mode corresponding to each to-be-optimized code
snippet and the current context into the large
language model for structured reconstruction to obtain a target reconstruction scheme output by the large
language model. According to the method and the device, the code optimization effect is improved.