Skip to content
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

Investigate why += is compiled so inefficiently in compiled mode #424

Closed
perlun opened this issue Feb 24, 2024 · 0 comments
Closed

Investigate why += is compiled so inefficiently in compiled mode #424

perlun opened this issue Feb 24, 2024 · 0 comments
Labels
bug Something isn't working as expected
Milestone

Comments

@perlun
Copy link
Collaborator

perlun commented Feb 24, 2024

Moved to GitLab

Please continue to the new version of this issue here: https://gitlab.perlang.org/perlang/perlang/-/issues/424. The GitHub page you are currently reading will not contain the latest information on this issue.


While working on finalizing #415, I saw something interesting. The following Perlang program (https://github.com/perlang-org/perlang/blob/52f7d7d303eba83fa3fe3bdb5e9725aa940dee0f/docs/examples/quickstart/pi.per):

// pi.per
//
// The number of digits we should calculate
var digits = 10000;

var i = 1;
var x = 3 * (10 ** (digits + 20));
var pi = x;

while (x > 0) {
    x = x * i / ((i + 1) * 4);
    pi += (x / (i + 2));
    i += 2;
}

print(pi / (10 ** 20));

...gets compiled into this:

// Automatically generated code by Perlang 0.4.0-dev.20 at 2024-02-24T20:15:53.3599921Z
// Do not modify. Changes to this file might be overwritten the next time the Perlang compiler is executed.

#include <math.h> // fmod()
#include <stdint.h>

#include "bigint.hpp" // BigInt
#include "stdlib.hpp"

//
// Method definitions
//
int main();

//
// Method declarations
//
int main() {
    int32_t digits = 10000;
    int32_t i = 1;
    BigInt x = (BigInt)3 * (perlang::BigInt_pow(10, (digits + 20)));
    BigInt pi = x;
    while (x > 0)     {
        x = x * (BigInt)i / ((i + 1) * 4);
        pi = pi += (x / (i + 2));
        i = i += 2;
    }
;
    perlang::print((pi / (perlang::BigInt_pow(10, 20))));
}

Note the pi = pi += (x / (i + 2)). Why on earth is this? It should be enough with just pi += (x / (i + 2)), I believe.

@perlun perlun added the bug Something isn't working as expected label Feb 24, 2024
@perlun perlun added this to the 0.5.0 milestone Feb 24, 2024
@perlun perlun modified the milestones: 0.5.0, 0.6.0, 0.7.0 May 8, 2024
@perlun perlun closed this as completed Aug 20, 2024
@perlang-org perlang-org locked as resolved and limited conversation to collaborators Aug 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

1 participant