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

Remove event pooling in the modern system #18216

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/legacy-events/EventBatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import invariant from 'shared/invariant';
import {enableModernEventSystem} from 'shared/ReactFeatureFlags';
import {rethrowCaughtError} from 'shared/ReactErrorUtils';

import type {ReactSyntheticEvent} from './ReactSyntheticEventType';
Expand All @@ -30,10 +31,13 @@ const executeDispatchesAndRelease = function(event: ReactSyntheticEvent) {
if (event) {
executeDispatchesInOrder(event);

// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
if (!event.isPersistent()) {
event.constructor.release(event);
}
}
}
};
const executeDispatchesAndReleaseTopLevel = function(e) {
return executeDispatchesAndRelease(e);
Expand Down
8 changes: 8 additions & 0 deletions packages/legacy-events/ResponderEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {enableModernEventSystem} from 'shared/ReactFeatureFlags';
import {getLowestCommonAncestor, isAncestor} from 'shared/ReactTreeTraversal';

import {
Expand Down Expand Up @@ -378,9 +379,12 @@ function setResponderAndExtractTransfer(
accumulateTwoPhaseDispatches(shouldSetEvent);
}
const wantsResponderInst = executeDispatchesInOrderStopAtTrue(shouldSetEvent);
// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
if (!shouldSetEvent.isPersistent()) {
shouldSetEvent.constructor.release(shouldSetEvent);
}
}

if (!wantsResponderInst || wantsResponderInst === responderInst) {
return null;
Expand Down Expand Up @@ -409,9 +413,13 @@ function setResponderAndExtractTransfer(
const shouldSwitch =
!hasDispatches(terminationRequestEvent) ||
executeDirectDispatch(terminationRequestEvent);

// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
if (!terminationRequestEvent.isPersistent()) {
terminationRequestEvent.constructor.release(terminationRequestEvent);
}
}

if (shouldSwitch) {
const terminateEvent = ResponderSyntheticEvent.getPooled(
Expand Down
31 changes: 28 additions & 3 deletions packages/legacy-events/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import invariant from 'shared/invariant';

import {enableModernEventSystem} from 'shared/ReactFeatureFlags';

const EVENT_POOL_SIZE = 10;

/**
Expand Down Expand Up @@ -152,20 +154,27 @@ Object.assign(SyntheticEvent.prototype, {
* won't be added back into the pool.
*/
persist: function() {
// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We'd probably want to add a warning on using persist(), as it'll just be dead code in apps otherwise.

this.isPersistent = functionThatReturnsTrue;
}
},

/**
* Checks if this event should be released back into the pool.
*
* @return {boolean} True if this should not be released, false otherwise.
*/
isPersistent: functionThatReturnsFalse,
isPersistent: enableModernEventSystem
? functionThatReturnsTrue
: functionThatReturnsFalse,

/**
* `PooledClass` looks for `destructor` on each instance it releases.
*/
destructor: function() {
// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

const Interface = this.constructor.Interface;
for (const propName in Interface) {
if (__DEV__) {
Expand Down Expand Up @@ -218,6 +227,7 @@ Object.assign(SyntheticEvent.prototype, {
getPooledWarningPropertyDefinition('stopPropagation', () => {}),
);
}
}
},
});

Expand Down Expand Up @@ -296,8 +306,15 @@ function getPooledWarningPropertyDefinition(propName, getVal) {
}
}

function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) {
function createOrGetPooledEvent(
dispatchConfig,
targetInst,
nativeEvent,
nativeInst,
) {
const EventConstructor = this;
// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
if (EventConstructor.eventPool.length) {
const instance = EventConstructor.eventPool.pop();
EventConstructor.call(
Expand All @@ -309,6 +326,7 @@ function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) {
);
return instance;
}
}
return new EventConstructor(
dispatchConfig,
targetInst,
Expand All @@ -318,6 +336,8 @@ function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) {
}

function releasePooledEvent(event) {
// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
const EventConstructor = this;
invariant(
event instanceof EventConstructor,
Expand All @@ -328,11 +348,16 @@ function releasePooledEvent(event) {
EventConstructor.eventPool.push(event);
}
}
}

function addEventPoolingTo(EventConstructor) {
EventConstructor.getPooled = createOrGetPooledEvent;

// Modern event system doesn't use pooling.
if (!enableModernEventSystem) {
EventConstructor.eventPool = [];
EventConstructor.getPooled = getPooledEvent;
EventConstructor.release = releasePooledEvent;
}
}

export default SyntheticEvent;
5 changes: 1 addition & 4 deletions packages/react-dom/src/events/DOMModernPluginEventSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ function dispatchEventsForPlugins(
for (let i = 0; i < syntheticEvents.length; i++) {
const syntheticEvent = syntheticEvents[i];
executeDispatchesInOrder(syntheticEvent);
// Release the event from the pool if needed
if (!syntheticEvent.isPersistent()) {
syntheticEvent.constructor.release(syntheticEvent);
}
// This doesn't call release because modern system doesn't use pooling.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,25 @@ describe('DOMModernPluginEventSystem', () => {
expect(log).toEqual([]);
expect(onDivClick).toHaveBeenCalledTimes(0);
});

it('does not pool events', () => {
const buttonRef = React.createRef();
const log = [];
const onClick = jest.fn(e => log.push(e));

function Test() {
return <button ref={buttonRef} onClick={onClick} />;
}

ReactDOM.render(<Test />, container);

let buttonElement = buttonRef.current;
dispatchClickEvent(buttonElement);
expect(onClick).toHaveBeenCalledTimes(1);
dispatchClickEvent(buttonElement);
expect(onClick).toHaveBeenCalledTimes(2);
expect(log[0]).not.toBe(log[1]);
expect(log[0].type).toBe('click');
expect(log[1].type).toBe('click');
});
});