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

Store BigIntStorage.capacity in header #17

Open
LiarPrincess opened this issue May 18, 2022 · 1 comment
Open

Store BigIntStorage.capacity in header #17

LiarPrincess opened this issue May 18, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@LiarPrincess
Copy link
Owner

From developer.apple.com/ManagedBufferPointer/Capacity:

This value may be nontrivial to compute; it is usually a good idea to store this information in the “header” area when an instance is created.

Under the hood

From ManagedBuffer.swift:

extension ManagedBufferPointer {
  @inlinable
  @available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
  public var capacity: Int {
    return (
      _capacityInBytes &- ManagedBufferPointer._elementOffset
    ) / MemoryLayout<Element>.stride
  }
}

Where _capacityInBytes is defined as malloc_size:

static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
  extern __swift_size_t malloc_size(const void *);
  return malloc_size(ptr);
}

Solutions

  • Store additional member capacity: Int in Header. Header size: 2 words.
  • Pack capacity inside existing header. Header size: 1 word. Layout [1bit - isNegative][31 bits - count][32 bits - capacity]
@LiarPrincess LiarPrincess added the enhancement New feature or request label May 18, 2022
@LiarPrincess
Copy link
Owner Author

Additional reference: loftwareArrayOfOptional (Dave Abrahams thingy).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant