Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace push.apply with dedicated function #12361

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/engine/Source/Core/addAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import defined from "./defined.js";

/**
* Adds all elements from the given source array to the given target array.
*
* If the `source` is `undefined`, then nothing will be done. Otherwise,
javagl marked this conversation as resolved.
Show resolved Hide resolved
* this has the same semantics as
* ```
* for (const s of source) target.push(s);
* ```
* but is usually more efficient than a `for`-loop, and does not put the
* elements of the source on the stack, as it would be done with the
* spread operator or when using `target.push.apply(source)`.
*
* @function
* @private
*
* @param {Array|undefined} source The source array
* @param {Array} target The target array
*
* @example
* const target = [ 0, 1, 2 ];
* const source = [ 3, 4, 5 ];
* Cesium.addAll(source, target);
* // The target is now [ 0, 1, 2, 3, 4, 5 ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick on semantics, but this time I do think it actually matters. When I first glanced at this code I expected the order of the arguments to be reversed: addAll(target, source).
This is largely because I am comparing this to similar functions of target.push(...source) or target.concat(source) etc. In all the builtin array functions the "result" is on the left and the "source" is on the right.
Another way I think about it is that this is replacing the syntax [...target, ...source] to put all the target elements first and the source elements second. So the target variable should be the first argument. Even in this example you're doing addAll([ 3, 4, 5 ], [ 0, 1, 2 ]) = [ 0, 1, 2, 3, 4, 5 ]
(Maybe "source" and "target" are the wrong names in the first place)

I think swapping them also aligns better with the idea that the "source" could be null, undefined or []. Then the first argument is always defined even if the second only sometimes is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also hesitated here. Leaving the source as the last parameter would allow omitting it, as in
addAll(target);
which is probably not ideal.

But in general, I'm open to changing it, and embarassingly have to point out that commit c00243a ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From my all-time favorite presentation on API design:

Cesium Parameter order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Swapped the parameters. Fortunately, someone posted the Regex for that, which is to ...
replace (addAllToArray)\(([^,]+), *([^,]+)\)
with $1($3, $2)

*/
function addAll(source, target) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think I like the name addAll. I don't feel it's descriptive enough to understand what it does without looking at the documentation or the function itself. addAllToArray or maybe combineArrays could be better? Or, given my previous comment on this topic with this essentially doing what Array.concat does, maybe concatInto or concatIntoArray would be more accurate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No really strong opinions, just things that I thought about:

  • In the linked (soon to be obsolete) PR, I initially called that function pushAll, but I wanted to emphasize that it does exactly not push...
  • Any name that involves concat could easily be confused with Array.concat, and (as discussed in the other PR), that has a completely different semantic: People might expect it to return a concatenation. I think it's important to make clear that it operates in-place, and on the given target array
  • combine... could be OK (maybe people would expect it to return something there, but not necessarily)

Maybe something like appendAll or append(All?To?)Array or so ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to addAllToArray - sounds reasonable and is long and clear enough for a global function.

if (!defined(source)) {
return;
}
const s = source.length;
if (s === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it's a nitpick but in general I think we should avoid single letter variable names outside the looping variables i, j, k, and sometimes l.

Maybe it's just personal preference but I find this a little more readable even if it is more verbose

  const sourceLength = source.length;
  if (sourceLength === 0) {
    return;
  }
  const targetLength = target.length;
  target.length += sourceLength;
  for (let i = 0; i < sourceLength; i++) {
    target[targetLength + i] = source[i];
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I nearly espected that, and can change it accordingly.
But regarding the variable name 1: Here is an animated GIF of l and 1, switching every second:

Cesium L

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to sourceLength and targetLength

return;
}
const t = target.length;
target.length += s;
for (let i = 0; i < s; i++) {
target[t + i] = source[i];
}
}
export default addAll;
13 changes: 7 additions & 6 deletions packages/engine/Source/Renderer/ShaderBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ShaderProgram from "./ShaderProgram.js";
import ShaderSource from "./ShaderSource.js";
import ShaderStruct from "./ShaderStruct.js";
import ShaderFunction from "./ShaderFunction.js";
import addAll from "../Core/addAll.js";

/**
* An object that makes it easier to build the text of a {@link ShaderProgram}. This tracks GLSL code for both the vertex shader and the fragment shader.
Expand Down Expand Up @@ -414,7 +415,7 @@ ShaderBuilder.prototype.addVertexLines = function (lines) {

const vertexLines = this._vertexShaderParts.shaderLines;
if (Array.isArray(lines)) {
vertexLines.push.apply(vertexLines, lines);
addAll(lines, vertexLines);
} else {
// Single string case
vertexLines.push(lines);
Expand Down Expand Up @@ -449,7 +450,7 @@ ShaderBuilder.prototype.addFragmentLines = function (lines) {

const fragmentLines = this._fragmentShaderParts.shaderLines;
if (Array.isArray(lines)) {
fragmentLines.push.apply(fragmentLines, lines);
addAll(lines, fragmentLines);
} else {
// Single string case
fragmentLines.push(lines);
Expand Down Expand Up @@ -534,15 +535,15 @@ function generateStructLines(shaderBuilder) {
structId = structIds[i];
struct = shaderBuilder._structs[structId];
structLines = struct.generateGlslLines();
vertexLines.push.apply(vertexLines, structLines);
addAll(structLines, vertexLines);
}

structIds = shaderBuilder._fragmentShaderParts.structIds;
for (i = 0; i < structIds.length; i++) {
structId = structIds[i];
struct = shaderBuilder._structs[structId];
structLines = struct.generateGlslLines();
fragmentLines.push.apply(fragmentLines, structLines);
addAll(structLines, fragmentLines);
}

return {
Expand Down Expand Up @@ -577,15 +578,15 @@ function generateFunctionLines(shaderBuilder) {
functionId = functionIds[i];
func = shaderBuilder._functions[functionId];
functionLines = func.generateGlslLines();
vertexLines.push.apply(vertexLines, functionLines);
addAll(functionLines, vertexLines);
}

functionIds = shaderBuilder._fragmentShaderParts.functionIds;
for (i = 0; i < functionIds.length; i++) {
functionId = functionIds[i];
func = shaderBuilder._functions[functionId];
functionLines = func.generateGlslLines();
fragmentLines.push.apply(fragmentLines, functionLines);
addAll(functionLines, fragmentLines);
}

return {
Expand Down
10 changes: 6 additions & 4 deletions packages/engine/Source/Scene/Cesium3DTileBatchTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import getBinaryAccessor from "./getBinaryAccessor.js";
import StencilConstants from "./StencilConstants.js";
import StencilFunction from "./StencilFunction.js";
import StencilOperation from "./StencilOperation.js";
import addAll from "../Core/addAll.js";

const DEFAULT_COLOR_VALUE = BatchTexture.DEFAULT_COLOR_VALUE;
const DEFAULT_SHOW_VALUE = BatchTexture.DEFAULT_SHOW_VALUE;
Expand Down Expand Up @@ -372,13 +373,14 @@ Cesium3DTileBatchTable.prototype.getPropertyIds = function (batchId, results) {
results.length = 0;

const scratchPropertyIds = Object.keys(this._properties);
results.push.apply(results, scratchPropertyIds);
addAll(scratchPropertyIds, results);

if (defined(this._batchTableHierarchy)) {
results.push.apply(
results,
this._batchTableHierarchy.getPropertyIds(batchId, scratchPropertyIds),
const propertyIds = this._batchTableHierarchy.getPropertyIds(
batchId,
scratchPropertyIds,
);
addAll(propertyIds, results);
}

return results;
Expand Down
7 changes: 4 additions & 3 deletions packages/engine/Source/Scene/Cesium3DTileStyle.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import addAll from "../Core/addAll.js";
import clone from "../Core/clone.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
Expand Down Expand Up @@ -1455,15 +1456,15 @@ Cesium3DTileStyle.prototype.getVariables = function () {
let variables = [];

if (defined(this.color) && defined(this.color.getVariables)) {
variables.push.apply(variables, this.color.getVariables());
addAll(this.color.getVariables(), variables);
}

if (defined(this.show) && defined(this.show.getVariables)) {
variables.push.apply(variables, this.show.getVariables());
addAll(this.show.getVariables(), variables);
}

if (defined(this.pointSize) && defined(this.pointSize.getVariables)) {
variables.push.apply(variables, this.pointSize.getVariables());
addAll(this.pointSize.getVariables(), variables);
}

// Remove duplicates
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/Source/Scene/ConditionsExpression.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import addAll from "../Core/addAll.js";
import clone from "../Core/clone.js";
import defined from "../Core/defined.js";
import Expression from "./Expression.js";
Expand Down Expand Up @@ -203,8 +204,8 @@ ConditionsExpression.prototype.getVariables = function () {
const length = conditions.length;
for (let i = 0; i < length; ++i) {
const statement = conditions[i];
variables.push.apply(variables, statement.condition.getVariables());
variables.push.apply(variables, statement.expression.getVariables());
addAll(statement.condition.getVariables(), variables);
addAll(statement.expression.getVariables(), variables);
}

// Remove duplicates
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/Source/Scene/GltfLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import VertexAttributeSemantic from "./VertexAttributeSemantic.js";
import GltfGpmLoader from "./Model/Extensions/Gpm/GltfGpmLoader.js";
import GltfMeshPrimitiveGpmLoader from "./Model/Extensions/Gpm/GltfMeshPrimitiveGpmLoader.js";
import oneTimeWarning from "../Core/oneTimeWarning.js";
import addAll from "../Core/addAll.js";

const {
Attribute,
Expand Down Expand Up @@ -2749,12 +2750,12 @@ function parse(loader, frameState) {

// Gather promises and handle any errors
const readyPromises = [];
readyPromises.push.apply(readyPromises, loader._loaderPromises);
addAll(loader._loaderPromises, readyPromises);

// When incrementallyLoadTextures is true, the errors are caught and thrown individually
// since it doesn't affect the overall loader state
if (!loader._incrementallyLoadTextures) {
readyPromises.push.apply(readyPromises, loader._texturesPromises);
addAll(loader._texturesPromises, readyPromises);
}

return Promise.all(readyPromises);
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/Source/Scene/MetadataTableProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import oneTimeWarning from "../Core/oneTimeWarning.js";
import MetadataComponentType from "./MetadataComponentType.js";
import MetadataClassProperty from "./MetadataClassProperty.js";
import MetadataType from "./MetadataType.js";
import addAll from "../Core/addAll.js";

/**
* A binary property in a {@MetadataTable}
Expand Down Expand Up @@ -388,7 +389,7 @@ function flatten(values) {
for (let i = 0; i < values.length; i++) {
const value = values[i];
if (Array.isArray(value)) {
result.push.apply(result, value);
addAll(value, result);
} else {
result.push(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import addAll from "../../Core/addAll.js";
import BoundingSphere from "../../Core/BoundingSphere.js";
import Check from "../../Core/Check.js";
import defaultValue from "../../Core/defaultValue.js";
Expand Down Expand Up @@ -482,16 +483,16 @@ ClassificationModelDrawCommand.prototype.pushCommands = function (
const passes = frameState.passes;
if (passes.render) {
if (this._useDebugWireframe) {
result.push.apply(result, this._commandListDebugWireframe);
addAll(this._commandListDebugWireframe, result);
return;
}

if (this._classifiesTerrain) {
result.push.apply(result, this._commandListTerrain);
addAll(this._commandListTerrain, result);
}

if (this._classifies3DTiles) {
result.push.apply(result, this._commandList3DTiles);
addAll(this._commandList3DTiles, result);
}

const useIgnoreShowCommands =
Expand All @@ -513,17 +514,17 @@ ClassificationModelDrawCommand.prototype.pushCommands = function (
);
}

result.push.apply(result, this._commandListIgnoreShow);
addAll(this._commandListIgnoreShow, result);
}
}

if (passes.pick) {
if (this._classifiesTerrain) {
result.push.apply(result, this._commandListTerrainPicking);
addAll(this._commandListTerrainPicking, result);
}

if (this._classifies3DTiles) {
result.push.apply(result, this._commandList3DTilesPicking);
addAll(this._commandList3DTilesPicking, result);
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/engine/Source/Scene/Model/GeoJsonLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import StructuralMetadata from "../StructuralMetadata.js";
import VertexAttributeSemantic from "../VertexAttributeSemantic.js";
import Buffer from "../../Renderer/Buffer.js";
import BufferUsage from "../../Renderer/BufferUsage.js";
import addAll from "../../Core/addAll.js";

/**
* Loads a GeoJson model as part of the <code>MAXAR_content_geojson</code> extension with the following constraints:
Expand Down Expand Up @@ -166,7 +167,8 @@ function parseMultiPolygon(coordinates) {
const polygonsLength = coordinates.length;
const lines = [];
for (let i = 0; i < polygonsLength; i++) {
Array.prototype.push.apply(lines, parsePolygon(coordinates[i]));
const polygon = parsePolygon(coordinates[i]);
addAll(polygon, lines);
}
return lines;
}
Expand Down
11 changes: 3 additions & 8 deletions packages/engine/Source/Scene/Model/InstancingPipelineStage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import addAll from "../../Core/addAll.js";
import AttributeCompression from "../../Core/AttributeCompression.js";
import Cartesian3 from "../../Core/Cartesian3.js";
import clone from "../../Core/clone.js";
Expand Down Expand Up @@ -210,10 +211,7 @@ InstancingPipelineStage.process = function (renderResources, node, frameState) {
renderResources.uniformMap = combine(uniformMap, renderResources.uniformMap);

renderResources.instanceCount = count;
renderResources.attributes.push.apply(
renderResources.attributes,
instancingVertexAttributes,
);
addAll(instancingVertexAttributes, renderResources.attributes);
};

const projectedTransformScratch = new Matrix4();
Expand Down Expand Up @@ -988,10 +986,7 @@ function processMatrixAttributes(
shaderBuilder.addAttribute("vec4", `a_instancing${attributeString}Row1`);
shaderBuilder.addAttribute("vec4", `a_instancing${attributeString}Row2`);

instancingVertexAttributes.push.apply(
instancingVertexAttributes,
matrixAttributes,
);
addAll(matrixAttributes, instancingVertexAttributes);
}

function processVec3Attribute(
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/Source/Scene/Model/ModelSceneGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ModelType from "./ModelType.js";
import NodeRenderResources from "./NodeRenderResources.js";
import PrimitiveRenderResources from "./PrimitiveRenderResources.js";
import ModelDrawCommands from "./ModelDrawCommands.js";
import addAll from "../../Core/addAll.js";

/**
* An in memory representation of the scene graph for a {@link Model}
Expand Down Expand Up @@ -912,7 +913,7 @@ ModelSceneGraph.prototype.pushDrawCommands = function (frameState) {
pushDrawCommandOptions,
);

frameState.commandList.push.apply(frameState.commandList, silhouetteCommands);
addAll(silhouetteCommands, frameState.commandList);
};

// Callback is defined here to avoid allocating a closure in the render loop
Expand Down
19 changes: 7 additions & 12 deletions packages/engine/Source/Scene/PropertyTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import defaultValue from "../Core/defaultValue.js";
import DeveloperError from "../Core/DeveloperError.js";
import defined from "../Core/defined.js";
import JsonMetadataTable from "./JsonMetadataTable.js";
import addAll from "../Core/addAll.js";

/**
* A property table for use with the <code>EXT_structural_metadata</code> extension or
Expand Down Expand Up @@ -296,24 +297,18 @@ PropertyTable.prototype.getPropertyIds = function (index, results) {

if (defined(this._metadataTable)) {
// concat in place to avoid unnecessary array allocation
results.push.apply(
results,
this._metadataTable.getPropertyIds(scratchResults),
);
const ids = this._metadataTable.getPropertyIds(scratchResults);
addAll(ids, results);
}

if (defined(this._batchTableHierarchy)) {
results.push.apply(
results,
this._batchTableHierarchy.getPropertyIds(index, scratchResults),
);
const ids = this._batchTableHierarchy.getPropertyIds(index, scratchResults);
addAll(ids, results);
}

if (defined(this._jsonMetadataTable)) {
results.push.apply(
results,
this._jsonMetadataTable.getPropertyIds(scratchResults),
);
const ids = this._jsonMetadataTable.getPropertyIds(scratchResults);
addAll(ids, results);
}

return results;
Expand Down
30 changes: 30 additions & 0 deletions packages/engine/Specs/Core/addAllSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { addAll } from "../../index.js";

describe("Core/addAll", function () {
it("works for basic arrays", function () {
const source = [3, 4, 5];
const target = [0, 1, 2];
addAll(source, target);
expect(target).toEqual([0, 1, 2, 3, 4, 5]);
});

it("works for null source", function () {
const target = [0, 1, 2];
addAll(null, target);
expect(target).toEqual([0, 1, 2]);
});

it("works for undefined source", function () {
const target = [0, 1, 2];
addAll(undefined, target);
expect(target).toEqual([0, 1, 2]);
});

it("works for large arrays", function () {
const source = Array(200000);
const target = Array(200000);
const result = Array(400000);
addAll(source, target);
expect(target).toEqual(result);
});
});
Loading
Loading