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

chore(stdlib): Optimize List.join #2130

Merged
merged 4 commits into from
Aug 5, 2024

Conversation

spotandjake
Copy link
Member

This pr optimizes List.join similarly to how Array.join was optimized in #1948, this change makes List.join use only a single allocation, and should reduce the number of times we need to copy while reallocating.

Closes: #728

Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

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

Looks pretty good so far, just needs a few tweaks!

stdlib/list.gr Outdated Show resolved Hide resolved
stdlib/list.gr Show resolved Hide resolved
stdlib/list.gr Outdated
}
}
@unsafe
let rec iterBuildString = (
Copy link
Member

Choose a reason for hiding this comment

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

Could we call this something like joinHelp or buildJoinedString?

stdlib/list.gr Outdated
use DataStructures.{ stringSize }
match (lst) {
[] => void,
// Last Element
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Last Element
// Last element

stdlib/list.gr Outdated
let size = stringSize(ptr)
Memory.copy(offset, ptr + 8n, size)
},
// Regular Path
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Regular Path

stdlib/list.gr Show resolved Hide resolved
stdlib/list.gr Outdated
Memory.copy(offset, ptr + 8n, size)
let offset = offset + size
Memory.copy(offset, sepPtr, sepSize)
iterBuildString(strPtr, sepPtr, sepSize, offset + sepSize, tl)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
iterBuildString(strPtr, sepPtr, sepSize, offset + sepSize, tl)
iterBuildString(strPtr, sepPtr, sepSize, offset + sepSize, tl)
ignore(hd)

Copy link
Member Author

Choose a reason for hiding this comment

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

I should put the ignore above the function call right otherwise it wont be a tail call?

stdlib/list.gr Outdated
use DataStructures.{ allocateString, stringSize }
let sepPtr = WasmI32.fromGrain(separator)
let sepSize = stringSize(sepPtr)
let strSize = iterSize(sepSize, 0n - sepSize, list)
Copy link
Member

Choose a reason for hiding this comment

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

With the changes I suggested this would just be 0n.

stdlib/list.gr Outdated
let sepPtr = WasmI32.fromGrain(separator)
let sepSize = stringSize(sepPtr)
let strSize = iterSize(sepSize, 0n - sepSize, list)
if (strSize <= 0n) return ""
Copy link
Member

Choose a reason for hiding this comment

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

And this check wouldn't be needed.

@spotandjake
Copy link
Member Author

Those changes have been commited.

@ospencer ospencer enabled auto-merge August 4, 2024 23:35
@ospencer ospencer added this pull request to the merge queue Aug 5, 2024
Merged via the queue into grain-lang:main with commit 6c8d1b0 Aug 5, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite List.join and Array.join to use low-level instructions to do joins faster
2 participants