Skip to content

feat(performance): removed double memory usage when reading/writing text files#2359

Merged
RohitKushvaha01 merged 7 commits into
Acode-Foundation:mainfrom
RohitKushvaha01:improve_perf
Jun 23, 2026
Merged

feat(performance): removed double memory usage when reading/writing text files#2359
RohitKushvaha01 merged 7 commits into
Acode-Foundation:mainfrom
RohitKushvaha01:improve_perf

Conversation

@RohitKushvaha01

Copy link
Copy Markdown
Member

No description provided.

@RohitKushvaha01 RohitKushvaha01 marked this pull request as draft June 22, 2026 06:43
@RohitKushvaha01 RohitKushvaha01 changed the title feat: removed double memory usage when reading/writing text files feat(performance): removed double memory usage when reading/writing text files Jun 22, 2026
@greptile-apps

greptile-apps Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR eliminates the base64 encode/decode round-trip that previously doubled memory usage when reading and writing text files on external storage. Two new native paths (readAsText / writeText) allow the Java layer to handle charset conversion directly, avoiding an intermediate ArrayBuffer allocation and transfer across the Cordova bridge.

  • Read path: readFile(encoding) now calls the new Java readAsText method which uses InputStreamReader to decode directly to a String; BOM stripping (U+FEFF) is applied in JavaScript before returning or parsing JSON.
  • Write path: writeFile(content, encoding) for string content now calls writeText, which encodes the string to bytes in Java with the resolved charset and writes via a try-with-resources OutputStream.
  • getEncodingName: Common charset-resolution logic is extracted from decode/encode into a shared helper, reducing duplication.

Confidence Score: 5/5

Safe to merge; the new native text paths behave correctly for all standard encodings.

The read and write paths correctly handle charset resolution, BOM stripping, and JSON parsing. The writeText OutputStream is properly managed in a try-with-resources block. The only note is in readAsText where the InputStream is opened just before its try-with-resources wrapper — closing cascades correctly on all normal paths, making this only a concern under very unlikely allocation failures.

The readAsText method in SDcard.java could tighten its resource management by declaring the InputStream inside its own try-with-resources arm.

Important Files Changed

Filename Overview
src/fileSystem/externalFs.js Adds readAsText and writeText wrappers that bypass the base64 encode/decode round-trip; readFile now dispatches directly to text-based paths when an encoding is given, and BOM stripping is correctly applied in JS before returning the text.
src/plugins/sdcard/src/android/SDcard.java Adds readAsText and writeText Java methods; writeText correctly uses try-with-resources for the OutputStream, but readAsText opens the InputStream outside the TWR block, creating a minor resource leak path on unlikely allocation failures.
src/plugins/sdcard/www/plugin.js Adds readAsText and writeText Cordova bridge methods with correct argument ordering matching the Java dispatch.
src/utils/encodings.js Extracts shared charset-resolution logic into getEncodingName, refactoring decode and encode to use it — clean deduplication with no behavioural change.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant JS as JavaScript (externalFs.js)
    participant Bridge as Cordova Bridge
    participant Java as SDcard.java

    Note over JS,Java: OLD read path (double memory)
    JS->>Bridge: sdcard.read(url) to ArrayBuffer via base64
    Bridge->>Java: read action
    Java-->>Bridge: byte[] as base64 String
    Bridge-->>JS: ArrayBuffer
    JS->>JS: decode(buffer, charset) to String 2x memory

    Note over JS,Java: NEW read path (single pass)
    JS->>Bridge: sdcard.readAsText(url, charset)
    Bridge->>Java: readAsText action
    Java->>Java: InputStreamReader(is, charset) to String
    Java-->>Bridge: String
    Bridge-->>JS: String
    JS->>JS: BOM strip then return text

    Note over JS,Java: NEW write path (single pass)
    JS->>Bridge: sdcard.writeText(url, text, charset)
    Bridge->>Java: writeText action
    Java->>Java: content.getBytes(charset) then write to disk
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant JS as JavaScript (externalFs.js)
    participant Bridge as Cordova Bridge
    participant Java as SDcard.java

    Note over JS,Java: OLD read path (double memory)
    JS->>Bridge: sdcard.read(url) to ArrayBuffer via base64
    Bridge->>Java: read action
    Java-->>Bridge: byte[] as base64 String
    Bridge-->>JS: ArrayBuffer
    JS->>JS: decode(buffer, charset) to String 2x memory

    Note over JS,Java: NEW read path (single pass)
    JS->>Bridge: sdcard.readAsText(url, charset)
    Bridge->>Java: readAsText action
    Java->>Java: InputStreamReader(is, charset) to String
    Java-->>Bridge: String
    Bridge-->>JS: String
    JS->>JS: BOM strip then return text

    Note over JS,Java: NEW write path (single pass)
    JS->>Bridge: sdcard.writeText(url, text, charset)
    Bridge->>Java: writeText action
    Java->>Java: content.getBytes(charset) then write to disk
Loading

Reviews (6): Last reviewed commit: "format" | Re-trigger Greptile

Comment thread src/plugins/sdcard/src/android/SDcard.java Outdated
Comment thread src/plugins/sdcard/src/android/SDcard.java Outdated
Comment thread src/fileSystem/externalFs.js Outdated
@RohitKushvaha01 RohitKushvaha01 marked this pull request as ready for review June 22, 2026 07:30
@RohitKushvaha01 RohitKushvaha01 merged commit b5ba27c into Acode-Foundation:main Jun 23, 2026
8 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in The Code Board - Acode Jun 23, 2026
@RohitKushvaha01 RohitKushvaha01 deleted the improve_perf branch June 23, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants