8000 improve Array::makei by illusory0x0 · Pull Request #1753 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

improve Array::makei #1753

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

illusory0x0
Copy link
Contributor

Uses the same implementation as Array::make, which is panic when len < 0.

Uses the same implementation as Array::make, which is panic when len < 0.
@coveralls
Copy link
Collaborator
coveralls commented Mar 9, 2025

Pull Request Test Coverage Report for Build 5598

Details

  • 0 of 1 (0.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 92.53%

Changes Missing Coverage Covered Lines Changed/Added Lines %
builtin/array.mbt 0 1 0.0%
Totals Coverage Status
Change from base Build 5587: -0.02%
Covered Lines: 6008
Relevant Lines: 6493

💛 - Coveralls

Copy link
Collaborator
@peter-jerry-ye peter-jerry-ye left a comment

Choose a reason for hiding this comment

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

Please do not move the APIs.

If you want to use the intrinsics, just copy them to the array package.

@Yu-zh
Copy link
Collaborator
Yu-zh commented Mar 10, 2025

Maybe you can just make unsafe_set public. Since the unsafe_get is already public, I don't see a reason why unsafe_set cannot be.

if length <= 0 {
[]
} else {
let array = Array::make(length, value(0))
Copy link
Contributor Author
@illusory0x0 illusory0x0 Mar 10, 2025

Choose a reason for hiding this comment

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

I want to avoid redundent fill values Array::make(length, value(0))

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see why it's redundent: when you have Array::make_uninit, it also has this fill operation, except it's filled with some other value.

The only thing that may change is the performance difference between set and unsafe_set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Array::make has two fill operations.

Array::makei call Array::make has three fill operations,

pub fn Array::make[T](len : Int, elem : T) -> Array[T] {
  let arr = Array::make_uninit(len) // (1)
  for i in 0..<len {
    arr.unsafe_set(i, elem)  // (2)
  }
  arr
}

pub fn Array::makei[T](length : Int, value : (Int) -> T) -> Array[T] {
  if length <= 0 {
    []
  } else {
    let array = Array::make(length, value(0)) // (2)
    for i in 1..<length {
      array[i] = value(i) // (3)
    }
    array
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Yu-zh Do we have intrinsics for these cases?

@illusory0x0
Copy link
Contributor Author

Maybe you can just make unsafe_set public. Since the unsafe_get is already public, I don't see a reason why unsafe_set cannot be.

I also want to use Array::make_uninit to avoid redundant filling operations

@illusory0x0
Copy link
Contributor Author
illusory0x0 commented Mar 10, 2025

If you want to use the intrinsics, just copy them to the array package.

So how to copy them to the array package, If I want to use Array::unsafe_set which depend on private definitions.

@peter-jerry-ye
Copy link
Collaborator

Copy them as well.

@illusory0x0 illusory0x0 marked this pull request as draft March 10, 2025 07:56
@peter-jerry-ye
Copy link
Collaborator

Array related implementation will be moved to @array, which is WIP by @Guest0x0

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.

5 participants
0