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

make putNode correct #1272

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

zhiqiangxu
Copy link

@zhiqiangxu zhiqiangxu commented Mar 24, 2020

s.buf bytes may be not aligned, so the correct implementation should take that into consideration.

Update

Switched to use makeAlignedBuf instead.


This change is Reviewable

skl/arena.go Outdated Show resolved Hide resolved
@jarifibrahim
Copy link
Contributor

Hey @zhiqiangxu, can you add some more information to the PR description? I'm trying to understand why do we need this change and what are the implications of it.

@zhiqiangxu
Copy link
Author

zhiqiangxu commented Mar 24, 2020

node address = address of first element + offset

In order to make sure node address is aligned, should consider address of first element, which is not necessarily aligned.

It's wrong to only consider offset

@zhiqiangxu zhiqiangxu closed this Mar 25, 2020
@zhiqiangxu zhiqiangxu reopened this Mar 25, 2020
@zhiqiangxu
Copy link
Author

zhiqiangxu commented Mar 25, 2020

The spec only guarantees :

the first (64-bit) word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.

So it's not guaranteed that the first element of a []byte is 8 byte aligned, though the first element of []uint64 is always 8 byte aligned.

@stale
Copy link

stale bot commented Apr 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status/stale The issue hasn't had activity for a while and it's marked for closing. label Apr 24, 2020
@jarifibrahim jarifibrahim added status/confirmed The issue has been triaged by still not reproduced. and removed status/stale The issue hasn't had activity for a while and it's marked for closing. labels Apr 24, 2020
@jarifibrahim
Copy link
Contributor

@zhiqiangxu The review for this PR is pending because I don't understand alignment issues properly. I'll read some stuff about it and then review this PR. Apologies for the delay.

@stale
Copy link

stale bot commented May 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status/stale The issue hasn't had activity for a while and it's marked for closing. label May 24, 2020
@jarifibrahim jarifibrahim added skip/stale Skip stalebot and removed status/stale The issue hasn't had activity for a while and it's marked for closing. labels May 26, 2020
@jarifibrahim
Copy link
Contributor

@zhiqiangxu I did a small benchmark to compare the performance.
This is the BenchmarkWrite function in the skl package.

name            old time/op    new time/op    delta
Write-8          522ns ± 8%     563ns ± 3%  +7.87%  (p=0.001 n=10+8)

The new changes seem to be slower. Do you know why it could be slower?

@zhiqiangxu
Copy link
Author

zhiqiangxu commented Jun 1, 2020

Maybe because of the extra bounding check and more bytes involved, but pity that I don't know how to optimize it:(

@damz
Copy link
Contributor

damz commented Jul 2, 2020

The rationale looks correct to me: Golang doesn't guarantee that the underlying memory block of a []byte is aligned. It only guarantees that the underlying memory block of an array follows the alignment guarantees of each its elements:

From https://golang.org/ref/spec#Size_and_alignment_guarantees:

For a variable x of array type: unsafe.Alignof(x) is the same as the alignment of a variable of the array's element type.

So a []uint64 will be aligned to 64 bit on platforms that requires that. But a []byte will not.

Based on this, I can suggest the following:

func makeAligned(n int64) []byte {
	buf := make([]uint64, n>>3)
	head := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
	head.Len = int(n)
	head.Cap = int(n)
	return *(*[]byte)(unsafe.Pointer(head))
}

It doesn't seem to have any measurable performance impact.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@joshua-goldstein joshua-goldstein removed the skip/stale Skip stalebot label Nov 4, 2022
@joshua-goldstein joshua-goldstein added status/needs-attention This issue needs more eyes on it, more investigation might be required before accepting/rejecting it and removed status/needs-attention This issue needs more eyes on it, more investigation might be required before accepting/rejecting it labels Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/confirmed The issue has been triaged by still not reproduced.
Development

Successfully merging this pull request may close these issues.

None yet

6 participants