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

Add reserve for list #2238

Merged
merged 4 commits into from
Aug 1, 2023
Merged

Add reserve for list #2238

merged 4 commits into from
Aug 1, 2023

Conversation

kabra1110
Copy link
Collaborator

@kabra1110 kabra1110 commented Jul 31, 2023

Fixes #2232.

Similar to C++, I have kept this as a function intrinsic to list class.

The test does not work with CPython, as this function is not present there. I'm not entirely sure on how to add a no-op function for this to work.

@kabra1110 kabra1110 requested a review from czgdp1807 July 31, 2023 05:45
l2: list[list[tuple[f64, str, tuple[i32, f64]]]] = []
i: i32

l1.reserve(100)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
l1.reserve(100)
reserve(l1, 100)

l1.append(i)
assert len(l1) == i + 1

l1.reserve(150)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
l1.reserve(150)
reserve(l1, 150)

l1.pop(0)
assert len(l1) == 49 - i

l2.reserve(100)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
l2.reserve(100)
reserve(l2, 100)

l2.append([(f64(i * i), str(i), (i, f64(i + 1))), (f64(i), str(i), (i, f64(i)))])
assert len(l2) == i + 1

l2.reserve(150)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
l2.reserve(150)
reserve(l2, 150)

@@ -0,0 +1,30 @@
from lpython import i32, f64
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from lpython import i32, f64
from lpython import i32, f64, reserve

return nullptr;
}

static inline ASR::asr_t* create_ListReserve(Allocator& al, const Location& loc,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static inline ASR::asr_t* create_ListReserve(Allocator& al, const Location& loc,
static inline ASR::asr_t* create_Reserve(Allocator& al, const Location& loc,

x.base.base.loc, diagnostics);
}

static inline ASR::expr_t *eval_list_reserve(Allocator &/*al*/,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static inline ASR::expr_t *eval_list_reserve(Allocator &/*al*/,
static inline ASR::expr_t *eval_reserve(Allocator &/*al*/,

@@ -3124,6 +3172,8 @@ namespace IntrinsicFunctionRegistry {
{nullptr, &DictValues::verify_args}},
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListPop),
{nullptr, &ListPop::verify_args}},
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Reserve),

@@ -3206,6 +3256,8 @@ namespace IntrinsicFunctionRegistry {
"list.reverse"},
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListPop),
"list.pop"},
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Reserve),

@@ -3290,6 +3342,7 @@ namespace IntrinsicFunctionRegistry {
{"list.index", {&ListIndex::create_ListIndex, &ListIndex::eval_list_index}},
{"list.reverse", {&ListReverse::create_ListReverse, &ListReverse::eval_list_reverse}},
{"list.pop", {&ListPop::create_ListPop, &ListPop::eval_list_pop}},
{"list.reserve", {&ListReserve::create_ListReserve, &ListReserve::eval_list_reserve}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"list.reserve", {&ListReserve::create_ListReserve, &ListReserve::eval_list_reserve}},
{"list.reserve", {&ListReserve::create_Reserve, &ListReserve::eval_reserve}},

@certik
Copy link
Contributor

certik commented Jul 31, 2023

Beautiful. Yes, as Gagandeep said, you just do:

from lpython import reserve
reserve(l, 100)

Let's get this in and benchmark again.

@kabra1110
Copy link
Collaborator Author

The latest commit makes these changes. But the test gives this error

semantic error: Function 'reserve' is not declared

@czgdp1807 Could you please point where exactly is this to be registered in python_ast_to_asr? I tried following functions like exp2, but those changes do not work

@certik
Copy link
Contributor

certik commented Aug 1, 2023

@czgdp1807 if you could please help, that would be great. Let's get this finished.

@czgdp1807
Copy link
Collaborator

I will meet @kabra1110 today after our meeting ends. We will complete it then.

@czgdp1807 czgdp1807 changed the title Add list.reserve Add reserve for list Aug 1, 2023
@czgdp1807 czgdp1807 enabled auto-merge (squash) August 1, 2023 16:35
@czgdp1807 czgdp1807 merged commit 1180f99 into lcompilers:main Aug 1, 2023
9 checks passed
@certik
Copy link
Contributor

certik commented Aug 1, 2023

Thanks @czgdp1807 !

How do the updated benchmarks look like?

kabra1110 added a commit to kabra1110/lpython that referenced this pull request Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support reserve like std::vector<T>::reserve in C++
3 participants