Skip to content

Functions written in expression style are slower than imperative style #1528

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

Closed
brson opened this issue Jan 15, 2012 · 0 comments
Closed

Functions written in expression style are slower than imperative style #1528

brson opened this issue Jan 15, 2012 · 0 comments
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@brson
Copy link
Contributor

brson commented Jan 15, 2012

This takes 4.7s:

use std;                                                                                                                                                                      

fn ack(m: int, n: int) -> int {                                                                                                                                               
    if m == 0 {                                                                                                                                                               
        ret n + 1                                                                                                                                                             
    } else {                                                                                                                                                                  
        if n == 0 {                                                                                                                                                           
            ret ack(m - 1, 1)                                                                                                                                                 
        } else {                                                                                                                                                              
            ret ack(m - 1, ack(m, n - 1));                                                                                                                                    
        }                                                                                                                                                                     
    }                                                                                                                                                                         
}                                                                                                                                                                             

fn main(args: [str]) {                                                                                                                                                        
    // FIXME: #1527                                                                                                                                                           
    sys::set_min_stack(1000000u);                                                                                                                                             
    let n = if vec::len(args) == 2u {                                                                                                                                         
        int::from_str(args[1])                                                                                                                                                
    } else {                                                                                                                                                                  
        12                                                                                                                                                                    
    };                                                                                                                                                                        
    std::io::println(#fmt("Ack(3,%d): %d\n", n, ack(3, n)));                                                                                                                  
}      

This takes 5.5s:

use std;                                                                                                                                                                      

fn ack(m: int, n: int) -> int {                                                                                                                                               
    if m == 0 {                                                                                                                                                               
        n + 1                                                                                                                                                                 
    } else {                                                                                                                                                                  
        if n == 0 {                                                                                                                                                           
            ack(m - 1, 1)                                                                                                                                                     
        } else {                                                                                                                                                              
            ack(m - 1, ack(m, n - 1))                                                                                                                                         
        }                                                                                                                                                                     
    }                                                                                                                                                                         
}                                                                                                                                                                             

fn main(args: [str]) {                                                                                                                                                        
    // FIXME: #1527                                                                                                                                                           
    sys::set_min_stack(1000000u);                                                                                                                                             
    let n = if vec::len(args) == 2u {                                                                                                                                         
        int::from_str(args[1])                                                                                                                                                
    } else {                                                                                                                                                                  
        12                                                                                                                                                                    
    };                                                                                                                                                                        
    std::io::println(#fmt("Ack(3,%d): %d\n", n, ack(3, n)));                                                                                                                  
}                            
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
For now I just moved the reachability code to this new module. As a follow up PR, I am planning to move the unsized coercion related code into a separate module so it can be used to refactor and fix codegen casting logic (related issues rust-lang#1531, rust-lang#566, and rust-lang#1528) .
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
We currently have a few issues with how we are generating code for casting (rust-lang#566, and rust-lang#1528). The structure of the code is also hard to understand and maintain (see rust-lang#1531 for more details).

This PR is the first part of the fix I developed. This change moves the coercion specific code to its own module and it introduces an iterator that traverses the coercion path.
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Fix issues with how we are generating code for casting (rust-lang#566, and rust-lang#1528).

Restructure the unsize casting to be done in one pass instead with deep recursion (rust-lang#1531). This also reuses the code from the reachability analysis, so we don't have to keep two ways of traversing the same structure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

No branches or pull requests

2 participants