Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

proposal: spec: type parametrized array lengths #70955

Closed
soypat opened this issue Dec 21, 2024 · 3 comments
Closed

proposal: spec: type parametrized array lengths #70955

soypat opened this issue Dec 21, 2024 · 3 comments
Labels
Milestone

Comments

@soypat
Copy link

soypat commented Dec 21, 2024

Proposal Details

Context

Today there is no way to allow parametrization of array lengths in types other than using constant declarations like so

const N = 100 // Parametrization defined at package level.

type RingBuffer struct {
   off, end int
   buf      [N]byte
}

Drawbacks

Poor parametrization for module users: Note the length of the array is defined at a package level and when the type is exported it cannot be changed by end user other than using build tags which must be defined beforehand by module provider.

Poor reusability: Even if parametrization is allowed it can only be specialized once per compile. Regardless of build tag customization you need to create a new type for each array length desired. You also need to rewrite each method from scratch.

Proposal

Add possibility of array length type parametrization.

Example (demonstrative purposes only):

func NewRingBuffer[N [N]byte]() RingBuffer[N] {
      return RingBuffer[N]{}
}

const buflen = 500
r1 := NewRingBuffer[500]()
r2 := NewRingBuffer[buflen]()

func getArrayPtr[N [N]byte](buf []byte, off int) *[N]byte {
    return (*[N]byte)(frm.buf[off:N+off])
}

dataptr := getArrayPtr[20](r1.buf, 10) 
Author's note (more context)

I use Go in embedded systems with TinyGo. My concern is memory usage regarding constraint on size and GC overhead and memory fragmentation. I'm currently writing a networking stack which can run on 100kB of RAM. I regularly make use of arrays to avoid heap allocations which are costly and they also fragment memory on these small devices. This feature would help the embedded scene a bunch for writing low level drivers :)

Below are links to OSI layers I have implemented in TinyGo.

@gopherbot gopherbot added this to the Proposal milestone Dec 21, 2024
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@ianlancetaylor
Copy link
Member

Thanks. Closing as dup of #44253.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2024
@soypat
Copy link
Author

soypat commented Dec 21, 2024

@ianlancetaylor Whoops- should I add a comment with my proposal to 44253?

Edit: done!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants