This is an automated email from the ASF dual-hosted git repository.

kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-js.git


The following commit(s) were added to refs/heads/main by this push:
     new f7398fa7 fix: Stop setting Symbol.isConcatSpreadable on Vector, Table 
and RecordBatch (#527)
f7398fa7 is described below

commit f7398fa7b390becd0d954da240cc81aa6c0a0c9f
Author: Arnaud MAURICE <[email protected]>
AuthorDate: Tue Nov 10 23:13:29 2026 +0100

    fix: Stop setting Symbol.isConcatSpreadable on Vector, Table and 
RecordBatch (#527)
    
    ## What's Changed
    
    `Vector`, `Table` and `RecordBatch` set `Symbol.isConcatSpreadable` on
    their prototypes when the module loads. V8 keeps a single process-wide
    protector for that symbol: once any object defines it, whatever its
    value, every `Array.prototype.concat` in the process takes the slow path
    for the life of the process. Loading `apache-arrow` therefore made
    `concat` about 100× slower everywhere, including in code that never
    touches an Arrow object (see #525).
    
    This PR removes the three assignments and the matching
    `[Symbol.isConcatSpreadable]: true` declarations in the `Vector`,
    `Table` and `RecordBatch` interfaces. No code in `src/` passes an Arrow
    object to `Array.prototype.concat`: the remaining `concat` calls
    concatenate arrays of `Data` or are the classes' own `concat` methods.
    
    ### Performance
    
    The reproduction script from #525 (20,000 `acc = acc.concat([i])` calls,
    run before and after `require`) against the ES2015 CommonJS build,
    median of 3 runs:
    
    | Node.js | Build | Before `require` | After `require` |
    `%IsConcatSpreadableProtector()` after `require` |
    | --- | --- | ---: | ---: | --- |
    | 20.20.2 | `main` | 471 ms | 48,150 ms | `false` |
    | 20.20.2 | this PR | 465 ms | 479 ms | `true` |
    | 24.19.0 | `main` | 412 ms | 47,410 ms | `false` |
    | 24.19.0 | this PR | 401 ms | 432 ms | `true` |
    
    ### Behaviour change
    
    `Array.prototype.concat` now appends an Arrow object as a single
    element, as for any other object. The symbol never made `concat` spread
    Arrow objects, because `concat` reads `length` and indexed properties:
    
    | Expression | Before | After |
    | --- | --- | --- |
    | `[0].concat(vectorFromArray([1, 2, 3]))` | `[0, <3 empty items>]` |
    `[0, vector]` |
    | `[0].concat(table)` | `[0]` | `[0, table]` |
    | `[0].concat(table.batches[0])` | `[0]` | `[0, recordBatch]` |
    
    `[...vector]` and `vector.toArray()` give the values.
    
    ### Tests
    
    `test/unit/concat-tests.ts` checks, for a numeric `Vector`, a `Utf8`
    `Vector`, a `Table` and a `RecordBatch`, that the object does not define
    `Symbol.isConcatSpreadable` and that `[0].concat(x)` returns `[0, x]`.
    All 8 cases fail without the fix.
    
    | Check | Node.js | Result |
    | --- | --- | --- |
    | Source suite (`jestconfigs/jest.src.config.js`) | 20.20.2 | 50 suites
    passed |
    | Source suite (`jestconfigs/jest.src.config.js`) | 24.19.0 | 50 suites
    passed |
    | ES2015 CommonJS build (`npm test -- -t es2015 -m cjs`) | 24.19.0 | 50
    suites passed |
    | `npm run lint:ci` | 24.19.0 | clean |
    
    Closes #525.
---
 src/recordbatch.ts        |  6 ------
 src/table.ts              |  6 ------
 src/vector.ts             |  6 ------
 test/unit/concat-tests.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/src/recordbatch.ts b/src/recordbatch.ts
index 58d1ace9..07183a92 100644
--- a/src/recordbatch.ts
+++ b/src/recordbatch.ts
@@ -39,11 +39,6 @@ export interface RecordBatch<T extends TypeMap = any> {
     readonly TType: Struct<T>;
     readonly TArray: Struct<T>['TArray'];
     readonly TValue: Struct<T>['TValue'];
-
-    /**
-     * @see 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/isConcatSpreadable
-     */
-    [Symbol.isConcatSpreadable]: true;
 }
 
 /**
@@ -306,7 +301,6 @@ export class RecordBatch<T extends TypeMap = any> {
     // out this logic, but also so we're still compliant with `"sideEffects": 
false`
     protected static [Symbol.toStringTag] = ((proto: RecordBatch) => {
         (proto as any)._nullCount = -1;
-        (proto as any)[Symbol.isConcatSpreadable] = true;
         (proto as any)[kRecordBatchSymbol] = true;
         return 'RecordBatch';
     })(RecordBatch.prototype);
diff --git a/src/table.ts b/src/table.ts
index 2caeb75a..6c5ab450 100644
--- a/src/table.ts
+++ b/src/table.ts
@@ -56,11 +56,6 @@ export interface Table<T extends TypeMap = any> {
     readonly TType: Struct<T>;
     readonly TArray: Struct<T>['TArray'];
     readonly TValue: Struct<T>['TValue'];
-
-    /**
-     * @see 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/isConcatSpreadable
-     */
-    [Symbol.isConcatSpreadable]: true;
 }
 
 /**
@@ -400,7 +395,6 @@ export class Table<T extends TypeMap = any> {
         (proto as any).batches = [];
         (proto as any)._offsets = new Uint32Array([0]);
         (proto as any)._nullCount = -1;
-        (proto as any)[Symbol.isConcatSpreadable] = true;
         (proto as any)[kTableSymbol] = true;
         (proto as any)['isValid'] = wrapChunkedCall1(isChunkedValid);
         (proto as any)['get'] = 
wrapChunkedCall1(getVisitor.getVisitFn(Type.Struct));
diff --git a/src/vector.ts b/src/vector.ts
index 197c0cd3..a72f0068 100644
--- a/src/vector.ts
+++ b/src/vector.ts
@@ -51,11 +51,6 @@ export interface Vector<T extends DataType = any> {
     readonly TType: T['TType'];
     readonly TArray: T['TArray'];
     readonly TValue: T['TValue'];
-
-    /**
-     * @see 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/isConcatSpreadable
-     */
-    [Symbol.isConcatSpreadable]: true;
 }
 
 const visitorsByTypeId = {} as { [typeId: number]: { get: any; set: any; 
indexOf: any } };
@@ -369,7 +364,6 @@ export class Vector<T extends DataType = any> {
         (proto as any).stride = 1;
         (proto as any).numChildren = 0;
         (proto as any)._offsets = new Uint32Array([0]);
-        (proto as any)[Symbol.isConcatSpreadable] = true;
         (proto as any)[kVectorSymbol] = true;
 
         const typeIds: Type[] = Object.keys(Type)
diff --git a/test/unit/concat-tests.ts b/test/unit/concat-tests.ts
new file mode 100644
index 00000000..56498862
--- /dev/null
+++ b/test/unit/concat-tests.ts
@@ -0,0 +1,45 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import { tableFromArrays, vectorFromArray } from 'apache-arrow';
+
+/**
+ * Defining `Symbol.isConcatSpreadable` on any object, whatever its value,
+ * sends every `Array.prototype.concat` in the V8 process down its slow path.
+ *
+ * @see https://github.com/apache/arrow-js/issues/525
+ */
+describe('Array.prototype.concat with Arrow objects', () => {
+    const table = tableFromArrays({ x: Int32Array.from([1, 2, 3]) });
+    const cases = [
+        ['Vector', vectorFromArray([1, 2, 3])],
+        ['Vector<Utf8>', vectorFromArray(['a', 'b', 'c'])],
+        ['Table', table],
+        ['RecordBatch', table.batches[0]],
+    ] as const;
+
+    test.each(cases)('%s does not define Symbol.isConcatSpreadable', (_, 
value) => {
+        expect(Symbol.isConcatSpreadable in value).toBe(false);
+    });
+
+    test.each(cases)('[0].concat(%s) appends the object itself', (_, value) => 
{
+        const result = ([0] as unknown[]).concat(value);
+        expect(result).toHaveLength(2);
+        expect(result[0]).toBe(0);
+        expect(result[1]).toBe(value);
+    });
+});

Reply via email to