Skip to content

compiler,runtime: allow map values >256 bytes #3377

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

Merged
merged 2 commits into from
Jan 18, 2023

Conversation

dgryski
Copy link
Member

@dgryski dgryski commented Jan 10, 2023

No description provided.

@dgryski
Copy link
Member Author

dgryski commented Jan 11, 2023

Wonder if I should extend the keySize type too..

Edit: And I wonder if the type should be uintptr instead...

@aykevl
Copy link
Member

aykevl commented Jan 11, 2023

Whatever size we pick, we should really add a new check to make sure the key and value are in range. (Technically not part of the fix but it would be great to have, perhaps as a separate commit).

Edit: And I wonder if the type should be uintptr instead...

Yeah uintptr seems more appropriate to me. That's also the size that is used internally for slices and means that AVR will automatically use a 16-bit size.
On the other hand, limiting key/value sizes to 4GB on 64-bit systems doesn't seem entirely unreasonable to me. So maybe uint16 on <32-bit systems and uint32 on 32+ bits systems? (We don't have a type for that yet). But that probably overcomplicates things.

@deadprogram
Copy link
Member

Looks good to me. @aykevl any other feedback before merge?

@dgryski
Copy link
Member Author

dgryski commented Jan 11, 2023

Hmm... the first time the CI tests ran there was a OOM from one of the stdlib tests. I think I got another OOM when running the test corpus. I'm not sure if they'll be solved by the precise-GC. But I'm not happy at the moment with merging this until I know what's going on. (That either it is the uintptr change causing the OOM and that the precise GC solves it).

@dgryski
Copy link
Member Author

dgryski commented Jan 11, 2023

So indeed as-is this PR causes a corpus OOM-failure for gonum.org/v1/gonum/lapack/gonum and merging the precise GC branch on-top fixes it again 🎉 . That's definitely nice proof that the precise GC works.

@aykevl
Copy link
Member

aykevl commented Jan 11, 2023

So indeed as-is this PR causes a corpus OOM-failure for gonum.org/v1/gonum/lapack/gonum

Huh, that's kind of unexpected. Unless lapack/gonum creates a massive number of small maps, I don't see how that could easily create an OOM. It's just a few extra bytes. Unless it was already really close to the limit and this pushed it over the limit.

@dgryski
Copy link
Member Author

dgryski commented Jan 11, 2023

@aykevl More likely it's the more pointer-sized integers pinning allocations, hence why the precise GC solves the issue.

@aykevl
Copy link
Member

aykevl commented Jan 11, 2023

That would be rather surprising. Those pointer-sized integers only store relatively small values, far too small to look like a pointer (unless you have absolutely gargantuan types like [0x12345678]byte).

@dgryski
Copy link
Member Author

dgryski commented Jan 11, 2023

Shrug. No idea then. I'm fine to hold off merging this if you think we should investigate more.

@dgryski
Copy link
Member Author

dgryski commented Jan 12, 2023

Actually the precise GC probably won't affect the map code since the map-internal allocations don't have any associated layout information. Hmm.....

@aykevl
Copy link
Member

aykevl commented Jan 12, 2023

Actually the precise GC probably won't affect the map code since the map-internal allocations don't have any associated layout information. Hmm.....

It does affect this code though, which is a normal allocation:

return &hashmap{
buckets: buckets,
seed: uintptr(fastrand()),
keySize: keySize,
valueSize: valueSize,
bucketBits: bucketBits,
keyEqual: keyEqual,
keyHash: keyHash,
}

But still, map creation shouldn't be that frequent.

@aykevl
Copy link
Member

aykevl commented Jan 17, 2023

@dgryski can you try rebasing on the dev branch now that precise GC is in?

@dgryski dgryski force-pushed the dgryski/hashmap-value-size-fix branch from df2061a to 2b0a7e3 Compare January 17, 2023 19:49
@dgryski
Copy link
Member Author

dgryski commented Jan 17, 2023

Rebased. Running the test corpus now.

@dgryski
Copy link
Member Author

dgryski commented Jan 17, 2023

Boo.

FAIL	gonum.org/v1/gonum/lapack/gonum	53.420s

cmd /Users/dgryski/go/src/github.com/tinygo-org/tinygo/build/tinygo test -v -target= -tags=noasm appengine with err: exit status 1 at dir "/Users/dgryski/go/src/github.com/dgryski/tinygo-test-corpus/_corpus/gonum/gonum/lapack/gonum"

Edit: inconsistent passes if I run it in isolation, even without this particular change (dev at 655075e5:

ok  	gonum.org/v1/gonum/lapack/gonum	96.183s
ok  	gonum.org/v1/gonum/lapack/gonum	99.014s
panic: runtime error: out of memory
FAIL	gonum.org/v1/gonum/lapack/gonum	50.066s
panic: runtime error: out of memory
FAIL	gonum.org/v1/gonum/lapack/gonum	51.998s
ok  	gonum.org/v1/gonum/lapack/gonum	101.897s

@aykevl
Copy link
Member

aykevl commented Jan 17, 2023

That is a better explanation for why the precise GC had any effect: it probably didn't (but the test was flaky).

@dgryski
Copy link
Member Author

dgryski commented Jan 18, 2023

Ok, then this is safe to merge I guess.

@deadprogram
Copy link
Member

Thank you @dgryski for working on this, and to @aykevl for review. Now merging.

@deadprogram deadprogram merged commit 0504e4a into tinygo-org:dev Jan 18, 2023
@dgryski dgryski deleted the dgryski/hashmap-value-size-fix branch January 24, 2023 22:33
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.

3 participants