-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
|
||
import invariant from 'shared/invariant'; | ||
|
||
import {enableModernEventSystem} from 'shared/ReactFeatureFlags'; | ||
|
||
const EVENT_POOL_SIZE = 10; | ||
|
||
/** | ||
|
@@ -152,71 +154,79 @@ Object.assign(SyntheticEvent.prototype, { | |
* won't be added back into the pool. | ||
*/ | ||
persist: function() { | ||
this.isPersistent = functionThatReturnsTrue; | ||
// Modern event system doesn't use pooling. | ||
if (!enableModernEventSystem) { | ||
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() { | ||
const Interface = this.constructor.Interface; | ||
for (const propName in Interface) { | ||
// Modern event system doesn't use pooling. | ||
if (!enableModernEventSystem) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes below are just whitespace. https://github.com/facebook/react/pull/18216/files?w=1 |
||
const Interface = this.constructor.Interface; | ||
for (const propName in Interface) { | ||
if (__DEV__) { | ||
Object.defineProperty( | ||
this, | ||
propName, | ||
getPooledWarningPropertyDefinition(propName, Interface[propName]), | ||
); | ||
} else { | ||
this[propName] = null; | ||
} | ||
} | ||
this.dispatchConfig = null; | ||
this._targetInst = null; | ||
this.nativeEvent = null; | ||
this.isDefaultPrevented = functionThatReturnsFalse; | ||
this.isPropagationStopped = functionThatReturnsFalse; | ||
this._dispatchListeners = null; | ||
this._dispatchInstances = null; | ||
if (__DEV__) { | ||
Object.defineProperty( | ||
this, | ||
propName, | ||
getPooledWarningPropertyDefinition(propName, Interface[propName]), | ||
'nativeEvent', | ||
getPooledWarningPropertyDefinition('nativeEvent', null), | ||
); | ||
} else { | ||
this[propName] = null; | ||
} | ||
} | ||
this.dispatchConfig = null; | ||
this._targetInst = null; | ||
this.nativeEvent = null; | ||
this.isDefaultPrevented = functionThatReturnsFalse; | ||
this.isPropagationStopped = functionThatReturnsFalse; | ||
this._dispatchListeners = null; | ||
this._dispatchInstances = null; | ||
if (__DEV__) { | ||
Object.defineProperty( | ||
this, | ||
'nativeEvent', | ||
getPooledWarningPropertyDefinition('nativeEvent', null), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'isDefaultPrevented', | ||
getPooledWarningPropertyDefinition( | ||
Object.defineProperty( | ||
this, | ||
'isDefaultPrevented', | ||
functionThatReturnsFalse, | ||
), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'isPropagationStopped', | ||
getPooledWarningPropertyDefinition( | ||
getPooledWarningPropertyDefinition( | ||
'isDefaultPrevented', | ||
functionThatReturnsFalse, | ||
), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'isPropagationStopped', | ||
functionThatReturnsFalse, | ||
), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'preventDefault', | ||
getPooledWarningPropertyDefinition('preventDefault', () => {}), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'stopPropagation', | ||
getPooledWarningPropertyDefinition('stopPropagation', () => {}), | ||
); | ||
getPooledWarningPropertyDefinition( | ||
'isPropagationStopped', | ||
functionThatReturnsFalse, | ||
), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'preventDefault', | ||
getPooledWarningPropertyDefinition('preventDefault', () => {}), | ||
); | ||
Object.defineProperty( | ||
this, | ||
'stopPropagation', | ||
getPooledWarningPropertyDefinition('stopPropagation', () => {}), | ||
); | ||
} | ||
} | ||
}, | ||
}); | ||
|
@@ -296,18 +306,26 @@ function getPooledWarningPropertyDefinition(propName, getVal) { | |
} | ||
} | ||
|
||
function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) { | ||
function createOrGetPooledEvent( | ||
dispatchConfig, | ||
targetInst, | ||
nativeEvent, | ||
nativeInst, | ||
) { | ||
const EventConstructor = this; | ||
if (EventConstructor.eventPool.length) { | ||
const instance = EventConstructor.eventPool.pop(); | ||
EventConstructor.call( | ||
instance, | ||
dispatchConfig, | ||
targetInst, | ||
nativeEvent, | ||
nativeInst, | ||
); | ||
return instance; | ||
// Modern event system doesn't use pooling. | ||
if (!enableModernEventSystem) { | ||
if (EventConstructor.eventPool.length) { | ||
const instance = EventConstructor.eventPool.pop(); | ||
EventConstructor.call( | ||
instance, | ||
dispatchConfig, | ||
targetInst, | ||
nativeEvent, | ||
nativeInst, | ||
); | ||
return instance; | ||
} | ||
} | ||
return new EventConstructor( | ||
dispatchConfig, | ||
|
@@ -318,21 +336,28 @@ function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) { | |
} | ||
|
||
function releasePooledEvent(event) { | ||
const EventConstructor = this; | ||
invariant( | ||
event instanceof EventConstructor, | ||
'Trying to release an event instance into a pool of a different type.', | ||
); | ||
event.destructor(); | ||
if (EventConstructor.eventPool.length < EVENT_POOL_SIZE) { | ||
EventConstructor.eventPool.push(event); | ||
// Modern event system doesn't use pooling. | ||
if (!enableModernEventSystem) { | ||
const EventConstructor = this; | ||
invariant( | ||
event instanceof EventConstructor, | ||
'Trying to release an event instance into a pool of a different type.', | ||
); | ||
event.destructor(); | ||
if (EventConstructor.eventPool.length < EVENT_POOL_SIZE) { | ||
EventConstructor.eventPool.push(event); | ||
} | ||
} | ||
} | ||
|
||
function addEventPoolingTo(EventConstructor) { | ||
EventConstructor.eventPool = []; | ||
EventConstructor.getPooled = getPooledEvent; | ||
EventConstructor.release = releasePooledEvent; | ||
EventConstructor.getPooled = createOrGetPooledEvent; | ||
|
||
// Modern event system doesn't use pooling. | ||
if (!enableModernEventSystem) { | ||
EventConstructor.eventPool = []; | ||
EventConstructor.release = releasePooledEvent; | ||
} | ||
} | ||
|
||
export default SyntheticEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.