Skip to content

ByteData should support memcpy from other type data objects #32080

Open
@abarth

Description

@abarth

Writing this code makes me sad. The VM can do this much faster an easier than I can from inside Dart.

void _copyInt8(ByteData buffer, int offset, Int8List value) {
  final int count = value.length;
  for (int i = 0; i < count; ++i) {
    buffer.setInt8(offset + i, value[i]);
  }
}

void _copyUint8(ByteData buffer, int offset, Uint8List value) {
  final int count = value.length;
  for (int i = 0; i < count; ++i) {
    buffer.setUint8(offset + i, value[i]);
  }
}

void _copyInt16(ByteData buffer, int offset, Int16List value) {
  final int count = value.length;
  const int stride = 2;
  for (int i = 0; i < count; ++i) {
    buffer.setInt16(offset + i * stride, value[i], Endianness.LITTLE_ENDIAN);
  }
}

void _copyUint16(ByteData buffer, int offset, Uint16List value) {
  final int count = value.length;
  const int stride = 2;
  for (int i = 0; i < count; ++i) {
    buffer.setUint16(offset + i * stride, value[i], Endianness.LITTLE_ENDIAN);
  }
}

void _copyInt32(ByteData buffer, int offset, Int32List value) {
  final int count = value.length;
  const int stride = 4;
  for (int i = 0; i < count; ++i) {
    buffer.setInt32(offset + i * stride, value[i], Endianness.LITTLE_ENDIAN);
  }
}

void _copyUint32(ByteData buffer, int offset, Uint16List value) {
  final int count = value.length;
  const int stride = 4;
  for (int i = 0; i < count; ++i) {
    buffer.setUint32(offset + i * stride, value[i], Endianness.LITTLE_ENDIAN);
  }
}

void _copyInt64(ByteData buffer, int offset, Int32List value) {
  final int count = value.length;
  const int stride = 8;
  for (int i = 0; i < count; ++i) {
    buffer.setInt64(offset + i * stride, value[i], Endianness.LITTLE_ENDIAN);
  }
}

void _copyUint64(ByteData buffer, int offset, Uint16List value) {
  final int count = value.length;
  const int stride = 8;
  for (int i = 0; i < count; ++i) {
    buffer.setUint64(offset + i * stride, value[i], Endianness.LITTLE_ENDIAN);
  }
}

/cc @Hixie @zanderso

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-typed-datatype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions