perf(parser): eliminate O(n²) clones in postfix chaining (#86)#152
Merged
Conversation
try_parse_postfix agora recebe/devolve Expr por valor em vez de &mut Expr, movendo lhs para dentro do novo no em vez de clonar. Cadeias como a.b.c.d.e.f passam a crescer em O(n) e nao O(n^2). Closes #86
Owner
|
To testando o benchmark para ver se ficou mais eficiente mesmo |
Owner
|
A logica está funcionando e apresentou uma performace significante para o compilador. Muito bem! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contexto
Issue #86: em
postfix.rs, cada operador postfix clonava a expressãolhsinteira para encapsulá-la em um novo nó. Para uma cadeia comoa.b.c.d.e.fo custo de cópia era1 + 2 + 3 + 4 + 5 = O(n²)em número de operadores.Causa raiz
A assinatura
try_parse_postfix(parser, lhs: &mut Expr)recebialhspor referência mutável, e mover de&mut Tdireto paraBoxnão é possível em Rust — daí olhs.clone().Correção
Adotada a segunda opção da issue: mudar a assinatura para receber/devolver
Exprpor valor:```rust
pub fn try_parse_postfix(parser: &mut Parser, lhs: Expr) -> Result<(Expr, bool), CompilerError>
```
Agora
lhsé movido para dentro do novo nó (Box::new(lhs)) em vez de clonado. O único call site (parser.rs) foi atualizado paralet (next_lhs, consumed) = postfix::try_parse_postfix(self, lhs)?;. Cadeias postfix passam a crescer em O(n).Os 4 clones de
lhsemCall,Index,MemberePostfixforam eliminados.Observação
Esta branch (
perf/issue86-postfix-by-value) já continha o commitperf(parser): elimina clones O(n^2)de trabalho anterior que nunca havia sido PRado. Aqui ele foi rebasado sobre odeveloperatual (10 commits de IR/lowering à frente) e complementado com um teste de regressão.Verificação
cargo fmt --checklimpo.cargo clippy: sem issues no código alterado.parses_deep_member_access_chain_left_associative, que faz parse dea.b.c.de verifica o formato encaixadoMember(Member(Member(a, b), c), d)— trava o comportamento left-assoc do encadeamento.Closes #86.