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

map bytes to Uint8array #40

Merged
merged 6 commits into from May 30, 2022
Merged

map bytes to Uint8array #40

merged 6 commits into from May 30, 2022

Conversation

mschneider
Copy link
Contributor

Uint8Array is a js native representation for an array of unsigned bytes, the current Array mapping has bad performance characteristics, especially when working with large buffers. I'd rather leave the Array conversion to the user than making it part of the generator.

@mschneider
Copy link
Contributor Author

I ran yarn build and yarn test locally to confirm this doesn't break anything, but noticed that I had a bunch of changed files not sure why. LMK if i should reset some of them, or if you want to merge them.

Copy link
Owner

@kklas kklas left a comment

Choose a reason for hiding this comment

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

Yes you should include files generated by yarn build. These include the examples generated with the new code as well as the test example-program idl and expected codegen.
The docs fields in tests/example-program-gen/idl.json are removed in your commit which probably means you've ran anchor build manually and that's why the generated example-program code doesn't include docs. With yarn build it should use the nightly version of anchor which should include those comments.

Run yarn build and include all files. Otherwise the CI tests will fail. Also please update CHANGELOG.md.

package.json Outdated
@@ -51,6 +50,8 @@
"js-sha256": "^0.9.0",
"prettier": "^2.6.2",
"snake-case": "^3.0.4",
"ts-morph": "^14.0.0"
"ts-morph": "^14.0.0",
"rimraf": "^3.0.2",
Copy link
Owner

Choose a reason for hiding this comment

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

why change these dependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to move rimraf and tsc to dependencies from devDependencies else they can't be executed during postinstall stage. It allows to add anchor-client-gen as npm dependency to a project rather then requiring people to install it globally, possibly causing versioning projects between multiple projects.

Copy link
Owner

Choose a reason for hiding this comment

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

I had no problems adding it as a dependency. The npm package containes the dist directory already built.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah this only is an issue when linking a package via git branch

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm now that I think about it I don't think we need this (the ability to link the package via git branch and the postinstall script). If you want to get the unreleased features there's the beta package, I can update it for you if you want to use the latest commits. And if you want to use your local package you can always yarn / npm link.
In any case I think this is out of scope for this PR. Can you leave the package.json as it was for now and let's have this discussion on another PR / issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well i needed it to continue with what i wanted to work on as I'm not the maintainer of this repo. I don't think yarn link is a workable solution, when working with two other people on the same project, as it doesn't update lock files and hence produces inconsistent git commits. Reverted the commit anyways.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks. Yea I think it's a valid use case and would be happy to accommodate for this but let's discuss it separately.

src/common.ts Outdated
return `${paramPrefix}${ty.name}`
case "bytes":
return `Buffer.from(${paramPrefix}${ty.name})`
Copy link
Owner

@kklas kklas May 29, 2022

Choose a reason for hiding this comment

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

Shouldn't this be Uint8Array.from(<>)? Buffer is not available in browsers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it's definitely available due to some dependency / polyfill that is included, but agreed, can use the less specific type here

tests/test.ts Outdated
@@ -99,7 +99,7 @@ test("init and account fetch", async () => {
expect(
res.i128Field.eq(new BN("-85070591730234615865843651857942052874"))
).toBe(true)
expect(res.bytesField).toStrictEqual([1, 2, 255, 254])
expect(res.bytesField).toEqual(Buffer.from([1, 2, 255, 254]))
Copy link
Owner

Choose a reason for hiding this comment

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

Could we use Uint8Array.from(...) in this filed instead of Buffer? I know Buffer is a subclass of Uint8Array but it's a bit confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The bytes field is actually a buffer, and the comparison fails if the right hand side is not a buffer either.

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, does the borsh layout decode return a Buffer instead of Uint8Array in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah borsh seems to be pretty much Buffer based.

@mschneider
Copy link
Contributor Author

I ran yarn build, but the npm package doesn't work on my platform (darwin/arm64):

Only x86_64 / Linux distributed in NPM package right now.
Trying globally installed anchor.

@kklas
Copy link
Owner

kklas commented May 30, 2022

Ah that makes sense. It ran your globally installed version of anchor which is < anchor 0.25 and that's why the docs aren't generated.
No worries, I'll build it before merging.

@kklas
Copy link
Owner

kklas commented May 30, 2022

Alright, looks good overall! I'll finish it up from here with the build. Thanks for the PR!

@kklas kklas merged commit 6dd8ab8 into kklas:master May 30, 2022
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.

None yet

2 participants