Skip to content

[cpp20] Modernize binary reader and writer APIs to use std::span#2775

Open
sbc100 wants to merge 1 commit into
mainfrom
std_span
Open

[cpp20] Modernize binary reader and writer APIs to use std::span#2775
sbc100 wants to merge 1 commit into
mainfrom
std_span

Conversation

@sbc100

@sbc100 sbc100 commented Jun 24, 2026

Copy link
Copy Markdown
Member

Replace legacy raw pointer and size pairs with std::span
(aliased to ByteSpan) across WABT's binary reader interfaces, writer
streams, parser callbacks, and public entry points.

This greatly improves type safety, simplifies call sites (allowing vectors
and arrays to be passed directly), and aligns WABT with modern C++20
standards.

Highlights:

  • Fixes a performance bug in Stream::WriteData where passing std::vector
    accidentally triggered a full copy-by-value of the vector's contents.
    The template overload now correctly accepts std::span (zero-copy).
  • Simplifies various writing call sites (e.g. in binary-writer, c-writer,
    and wasm-strip) by passing vectors directly or leveraging subspan.

@sbc100 sbc100 requested a review from zherczeg June 24, 2026 04:58
@sbc100 sbc100 changed the title [cpp20] Modernize ReadBinaryObjdump API to use std::span [cpp20] Modernize binary reader and writer APIs to use std::span Jun 24, 2026
@sbc100 sbc100 force-pushed the std_span branch 4 times, most recently from 3e428c6 to ed6dfca Compare June 24, 2026 06:04

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
This will be harder for me to follow, but I will figure out something.

@sbc100

sbc100 commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

LGTM. This will be harder for me to follow, but I will figure out something.

Why does it make things harder? Maybe its not worth it?

@zherczeg

Copy link
Copy Markdown
Collaborator

Unfortunately we need to support older systems, even where std::string_view is not available :) With some custom headers and sed commands I am able to minimize the manual source code changes after an update. These might work if I can get a custom std::span implementation. Don't worry, I will be able to solve these things in some way.

@sbc100

sbc100 commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

Oh I didn't realize you have those limitations. What version of C++ are you stuck on?

@zherczeg

Copy link
Copy Markdown
Collaborator

C++ 14

@sbc100

sbc100 commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

C++ 14

That is a unfortunate. Do you know when you might be able to update?

@zherczeg

Copy link
Copy Markdown
Collaborator

Don't know, it is not my decision. Anyway, I will figure something out to maintain backwards compatiblity.

Replace legacy raw pointer and size pairs with std::span<const uint8_t>
(aliased to `ByteSpan`) across WABT's binary reader interfaces, writer
streams, parser callbacks, and public entry points.

This greatly improves type safety, simplifies call sites (allowing vectors
and arrays to be passed directly), and aligns WABT with modern C++20
standards.

Highlights:
- Fixes a performance bug in Stream::WriteData where passing std::vector
  accidentally triggered a full copy-by-value of the vector's contents.
  The template overload now correctly accepts std::span (zero-copy).
- Simplifies various writing call sites (e.g. in binary-writer, c-writer,
  and wasm-strip) by passing vectors directly or leveraging subspan.
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.

2 participants