Skip to content

[PHP] Add mmap support to proxyFS#3126

Merged
adamziel merged 12 commits intotrunkfrom
proxyfs-mmap
Jan 16, 2026
Merged

[PHP] Add mmap support to proxyFS#3126
adamziel merged 12 commits intotrunkfrom
proxyfs-mmap

Conversation

@adamziel
Copy link
Collaborator

@adamziel adamziel commented Jan 15, 2026

What it does

Adds mmap support to PROXYFS so the Intl extension works when /internal/shared (which contains ICU data files) is accessed through a proxied filesystem. This PR is based on @mho22's excellent work in WordPress/wordpress-playground#3073.

Rationale

PROXYFS proxies filesystem operations between PHP instances, enabling file sharing. The Intl extension depends on ICU, which uses mmap to load its data file (icudt74l.dat). Without mmap support in PROXYFS, ICU initialization fails when accessing the data file through a proxied mount.

This showed up when trying to use Collator or NumberFormatter in a PHP instance where /internal/shared was mounted via PROXYFS—the classes would fail to instantiate even though the extension was loaded.

Implementation

Added ensureProxyFSHasMmapSupport() that patches PROXYFS at runtime:

mmap implementation:

  • Allocates memory using the runtime's malloc
  • Reads the entire file into that memory via PROXYFS's existing read operation
  • Returns a pointer to the allocated buffer

Special handling for PHP 7.4:

  • PHP 7.4 sometimes passes incorrect length values to mmap for non-.dat files
  • Solution: query the actual file size from the proxied filesystem using fstat

msync implementation:

  • Writes changes back to the file if mapping was MAP_SHARED
  • Skips write for MAP_PRIVATE (changes stay in memory only)

The patch is applied automatically when calling proxyFileSystem(), so existing code gets mmap support without changes.

Testing instructions

Run the new test suite:

npm test -- proxyfs-mmap.spec.ts

Tests verify:

  • Basic file operations through PROXYFS (read, binary files, large files, concurrent access)
  • Intl classes (Collator, NumberFormatter) work through PROXYFS
  • All tests run across PHP 7.4–8.5

E2E tests verify Intl works in blueprints:

npm run test:e2e -- blueprints.spec.ts -g "Intl"

@mho22
Copy link
Collaborator

mho22 commented Jan 15, 2026

@adamziel Nice! The tests passed! I saw that you explicitly ignored PHP versions below 8. Is that intentional? :

const phpVersionsWithIntl: SupportedPHPVersion[] = ['8.0', '8.1', '8.2', '8.3', '8.4', '8.5'];

@adamziel
Copy link
Collaborator Author

@adamziel Nice! The tests passed! I saw that you explicitly ignored PHP versions below 8. Is that intentional? :

const phpVersionsWithIntl: SupportedPHPVersion[] = ['8.0', '8.1', '8.2', '8.3', '8.4', '8.5'];

no, it's vibe coded 🤣 👀

The mmap implementation for PROXYFS now correctly handles file reading for
older PHP versions (7.2-7.4). Key changes:

1. Get actual file size from the proxied filesystem using fstat() for
   non-.dat files, as older PHP versions may pass incorrect length values

2. Read file contents using a loop with subarray view into the heap,
   matching how MEMFS and the PR #3073 implementation work

3. Fix symbol usage bug - get symbols from both replica and sourceOfTruth
   instances separately to ensure correct property access when instances
   come from different module builds (ESM vs CJS)

4. Enable Intl extension tests for PHP 7.2, 7.3, and 7.4
Remove specific PHP versions that support the Intl extension with ICU from the tests.
The Intl extension supports both procedural functions and object-oriented classes. While we were testing numfmt_create() and similar functions, we weren't testing that classes like Collator work correctly. This matters because classes depend on ICU data being properly loaded, and a misconfiguration could break class instantiation even if functions work. The new test creates a Collator instance and verifies it can sort data according to locale rules.
The Intl extension provides both procedural functions (like numfmt_create) and object-oriented classes (like Collator). While both depend on ICU data being properly loaded via mmap, they exercise different code paths in the extension. Testing both ensures the entire Intl implementation works correctly across all supported PHP versions (8.5 through 7.2).
PHP 7.2 and 7.3 are no longer supported in the repository. Updated the Intl extension tests to only run for PHP 7.4 through 8.5, and updated code comments to reflect that PHP 7.4 is now the oldest supported version.
Rewrote documentation to be more specific and useful:

- Explains exactly why ICU needs mmap (loads icudt74l.dat for Collator/NumberFormatter)
- Clarifies what PHP 7.4's bug is (passes wrong stat buffer value instead of file size)
- Explains why we check for .dat files (ICU files work correctly, other files don't)
- Documents that ICU only maps from offset 0
- Explains the subarray view approach for reading
- Notes that msync rarely runs since ICU only reads
- Improves proxyFileSystem docs with concrete examples

Removed vague phrases like "certain file types" and "older PHP versions" in favor of precise explanations of what's happening and why.
Refactor workaround for incorrect file length in PHP 7.4 mmap implementation.
Removed unused proxyFS checks in the file system.
@WordPress WordPress deleted a comment from Copilot AI Jan 16, 2026
@adamziel adamziel marked this pull request as ready for review January 16, 2026 11:18
@adamziel
Copy link
Collaborator Author

I've reviewed this, corrected a few problems, adapted your tests here @mho22, and they all pass 🎉 I'm tempted to just merge with co-author note.

@adamziel adamziel merged commit a04b1d7 into trunk Jan 16, 2026
33 checks passed
@adamziel adamziel deleted the proxyfs-mmap branch January 16, 2026 11:40
@mho22 mho22 mentioned this pull request Jan 26, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants