Merge pull request #450 from Tyriar/449_keep_span_pool

Keep row spans in an object pool to reduce garbage collection by reusing DOM nodes
This commit is contained in:
Daniel Imms
2017-05-16 10:12:27 -07:00
committed by GitHub
5 changed files with 235 additions and 79 deletions
+108 -77
View File
@@ -3,6 +3,7 @@
*/ */
import { ITerminal } from './Interfaces'; import { ITerminal } from './Interfaces';
import { DomElementObjectPool } from './utils/DomElementObjectPool';
/** /**
* The maximum number of refresh frames to skip when the write buffer is non- * The maximum number of refresh frames to skip when the write buffer is non-
@@ -30,12 +31,15 @@ export class Renderer {
private _refreshFramesSkipped = 0; private _refreshFramesSkipped = 0;
private _refreshAnimationFrame = null; private _refreshAnimationFrame = null;
private _spanElementObjectPool = new DomElementObjectPool('span');
constructor(private _terminal: ITerminal) { constructor(private _terminal: ITerminal) {
// Figure out whether boldness affects // Figure out whether boldness affects
// the character width of monospace fonts. // the character width of monospace fonts.
if (brokenBold === null) { if (brokenBold === null) {
brokenBold = checkBoldBroken((<any>this._terminal).element); brokenBold = checkBoldBroken((<any>this._terminal).element);
} }
this._spanElementObjectPool = new DomElementObjectPool('span');
// TODO: Pull more DOM interactions into Renderer.constructor, element for // TODO: Pull more DOM interactions into Renderer.constructor, element for
// example should be owned by Renderer (and also exposed by Terminal due to // example should be owned by Renderer (and also exposed by Terminal due to
@@ -117,9 +121,8 @@ export class Renderer {
* @param {number} end The row to end at (between fromRow and terminal's height terminal - 1) * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
*/ */
private _refresh(start: number, end: number): void { private _refresh(start: number, end: number): void {
let x, y, i, line, out, ch, ch_width, width, data, attr, bg, fg, flags, row, parent, focused = document.activeElement;
// If this is a big refresh, remove the terminal rows from the DOM for faster calculations // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
let parent;
if (end - start >= this._terminal.rows / 2) { if (end - start >= this._terminal.rows / 2) {
parent = this._terminal.element.parentNode; parent = this._terminal.element.parentNode;
if (parent) { if (parent) {
@@ -127,8 +130,8 @@ export class Renderer {
} }
} }
width = this._terminal.cols; let width = this._terminal.cols;
y = start; let y = start;
if (end >= this._terminal.rows) { if (end >= this._terminal.rows) {
this._terminal.log('`end` is too large. Most likely a bad CSR.'); this._terminal.log('`end` is too large. Most likely a bad CSR.');
@@ -136,80 +139,104 @@ export class Renderer {
} }
for (; y <= end; y++) { for (; y <= end; y++) {
row = y + this._terminal.ydisp; let row = y + this._terminal.ydisp;
line = this._terminal.lines.get(row); let line = this._terminal.lines.get(row);
if (!line || !this._terminal.children[y]) {
// Continue if the line is not available, this means a resize is currently in progress
continue;
}
out = '';
if (this._terminal.y === y - (this._terminal.ybase - this._terminal.ydisp) let x;
&& this._terminal.cursorState if (this._terminal.y === y - (this._terminal.ybase - this._terminal.ydisp) &&
&& !this._terminal.cursorHidden) { this._terminal.cursorState &&
!this._terminal.cursorHidden) {
x = this._terminal.x; x = this._terminal.x;
} else { } else {
x = -1; x = -1;
} }
attr = this._terminal.defAttr; let attr = this._terminal.defAttr;
i = 0;
for (; i < width; i++) { const documentFragment = document.createDocumentFragment();
if (!line[i]) { let innerHTML = '';
// Continue if the character is not available, this means a resize is currently in progress let currentElement;
// Return the row's spans to the pool
while (this._terminal.children[y].children.length) {
const child = this._terminal.children[y].children[0];
this._terminal.children[y].removeChild(child);
this._spanElementObjectPool.release(<HTMLElement>child);
}
for (let i = 0; i < width; i++) {
// TODO: Could data be a more specific type?
let data: any = line[i][0];
const ch = line[i][1];
const ch_width: any = line[i][2];
if (!ch_width) {
continue; continue;
} }
data = line[i][0];
ch = line[i][1];
ch_width = line[i][2];
if (!ch_width)
continue;
if (i === x) data = -1; if (i === x) {
data = -1;
}
if (data !== attr) { if (data !== attr) {
if (attr !== this._terminal.defAttr) { if (attr !== this._terminal.defAttr) {
out += '</span>'; if (innerHTML) {
currentElement.innerHTML = innerHTML;
innerHTML = '';
}
documentFragment.appendChild(currentElement);
currentElement = null;
} }
if (data !== this._terminal.defAttr) { if (data !== this._terminal.defAttr) {
if (innerHTML && !currentElement) {
currentElement = this._spanElementObjectPool.acquire();
}
if (currentElement) {
if (innerHTML) {
currentElement.innerHTML = innerHTML;
innerHTML = '';
}
documentFragment.appendChild(currentElement);
}
currentElement = this._spanElementObjectPool.acquire();
if (data === -1) { if (data === -1) {
out += '<span class="reverse-video terminal-cursor">'; currentElement.classList.add('reverse-video', 'terminal-cursor');
} else { } else {
let classNames = []; let bg = data & 0x1ff;
let fg = (data >> 9) & 0x1ff;
bg = data & 0x1ff; let flags = data >> 18;
fg = (data >> 9) & 0x1ff;
flags = data >> 18;
if (flags & FLAGS.BOLD) { if (flags & FLAGS.BOLD) {
if (!brokenBold) { if (!brokenBold) {
classNames.push('xterm-bold'); currentElement.classList.add('xterm-bold');
} }
// See: XTerm*boldColors // See: XTerm*boldColors
if (fg < 8) fg += 8; if (fg < 8) {
fg += 8;
}
} }
if (flags & FLAGS.UNDERLINE) { if (flags & FLAGS.UNDERLINE) {
classNames.push('xterm-underline'); currentElement.classList.add('xterm-underline');
} }
if (flags & FLAGS.BLINK) { if (flags & FLAGS.BLINK) {
classNames.push('xterm-blink'); currentElement.classList.add('xterm-blink');
} }
// If inverse flag is on, then swap the foreground and background variables. // If inverse flag is on, then swap the foreground and background variables.
if (flags & FLAGS.INVERSE) { if (flags & FLAGS.INVERSE) {
/* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */ let temp = bg;
bg = [fg, fg = bg][0]; bg = fg;
// Should inverse just be before the fg = temp;
// above boldColors effect instead? // Should inverse just be before the above boldColors effect instead?
if ((flags & 1) && fg < 8) fg += 8; if ((flags & 1) && fg < 8) {
fg += 8;
}
} }
if (flags & FLAGS.INVISIBLE) { if (flags & FLAGS.INVISIBLE) {
classNames.push('xterm-hidden'); currentElement.classList.add('xterm-hidden');
} }
/** /**
@@ -229,55 +256,60 @@ export class Renderer {
} }
if (bg < 256) { if (bg < 256) {
classNames.push('xterm-bg-color-' + bg); currentElement.classList.add(`xterm-bg-color-${bg}`);
} }
if (fg < 256) { if (fg < 256) {
classNames.push('xterm-color-' + fg); currentElement.classList.add(`xterm-color-${fg}`);
} }
out += '<span';
if (classNames.length) {
out += ' class="' + classNames.join(' ') + '"';
}
out += '>';
} }
} }
} }
if (ch_width === 2) { if (ch_width === 2) {
out += '<span class="xterm-wide-char">'; // Wrap wide characters so they're sized correctly. It's more difficult to release these
} // from the object pool so just create new ones via innerHTML.
switch (ch) { innerHTML += `<span class="xterm-wide-char">${ch}</span>`;
case '&': } else if (ch.charCodeAt(0) > 255) {
out += '&amp;'; // Wrap any non-wide unicode character as some fonts size them badly
break; innerHTML += `<span class="xterm-normal-char">${ch}</span>`;
case '<': } else {
out += '&lt;'; switch (ch) {
break; case '&':
case '>': innerHTML += '&amp;';
out += '&gt;'; break;
break; case '<':
default: innerHTML += '&lt;';
if (ch <= ' ') { break;
out += '&nbsp;'; case '>':
} else { innerHTML += '&gt;';
out += ch; break;
} default:
break; if (ch <= ' ') {
} innerHTML += '&nbsp;';
if (ch_width === 2) { } else {
out += '</span>'; innerHTML += ch;
}
break;
}
} }
attr = data; attr = data;
} }
if (attr !== this._terminal.defAttr) { if (innerHTML && !currentElement) {
out += '</span>'; currentElement = this._spanElementObjectPool.acquire();
}
if (currentElement) {
if (innerHTML) {
currentElement.innerHTML = innerHTML;
innerHTML = '';
}
documentFragment.appendChild(currentElement);
currentElement = null;
} }
this._terminal.children[y].innerHTML = out; this._terminal.children[y].appendChild(documentFragment);
} }
if (parent) { if (parent) {
@@ -289,8 +321,7 @@ export class Renderer {
} }
// if bold is broken, we can't // If bold is broken, we can't use it in the terminal.
// use it in the terminal.
function checkBoldBroken(terminal) { function checkBoldBroken(terminal) {
const document = terminal.ownerDocument; const document = terminal.ownerDocument;
const el = document.createElement('span'); const el = document.createElement('span');
+47
View File
@@ -0,0 +1,47 @@
import { assert } from 'chai';
import { DomElementObjectPool } from './DomElementObjectPool';
class MockDocument {
private _attr: {[key: string]: string} = {};
constructor() {}
public getAttribute(key: string): string { return this._attr[key]; };
public setAttribute(key: string, value: string): void { this._attr[key] = value; }
}
describe('DomElementObjectPool', () => {
let pool: DomElementObjectPool;
beforeEach(() => {
pool = new DomElementObjectPool('span');
(<any>global).document = {
createElement: () => new MockDocument()
};
});
it('should acquire distinct elements', () => {
const element1 = pool.acquire();
const element2 = pool.acquire();
assert.notEqual(element1, element2);
});
it('should acquire released elements', () => {
const element = pool.acquire();
pool.release(element);
assert.equal(pool.acquire(), element);
});
it('should handle a series of acquisitions and releases', () => {
const element1 = pool.acquire();
const element2 = pool.acquire();
pool.release(element1);
assert.equal(pool.acquire(), element1);
pool.release(element1);
pool.release(element2);
assert.equal(pool.acquire(), element2);
assert.equal(pool.acquire(), element1);
});
it('should throw when releasing an element that was not acquired', () => {
assert.throws(() => pool.release(document.createElement('span')));
});
});
+75
View File
@@ -0,0 +1,75 @@
/**
* @module xterm/utils/DomElementObjectPool
* @license MIT
*/
/**
* An object pool that manages acquisition and releasing of DOM elements for
* when reuse is desirable.
*/
export class DomElementObjectPool {
private static readonly OBJECT_ID_ATTRIBUTE = 'data-obj-id';
private static _objectCount = 0;
private _type: string;
private _pool: HTMLElement[];
private _inUse: {[key: string]: HTMLElement};
/**
* @param type The DOM element type (div, span, etc.).
*/
constructor(private type: string) {
this._type = type;
this._pool = [];
this._inUse = {};
}
/**
* Acquire an element from the pool, creating it if the pool is empty.
*/
public acquire(): HTMLElement {
let element: HTMLElement;
if (this._pool.length === 0) {
element = this._createNew();
} else {
element = this._pool.pop();
}
this._inUse[element.getAttribute(DomElementObjectPool.OBJECT_ID_ATTRIBUTE)] = element;
return element;
}
/**
* Release an element back into the pool. It's up to the caller of this
* function to ensure that all external references to the element have been
* removed.
* @param element The element being released.
*/
public release(element: HTMLElement): void {
if (!this._inUse[element.getAttribute(DomElementObjectPool.OBJECT_ID_ATTRIBUTE)]) {
throw new Error('Could not release an element not yet acquired');
}
delete this._inUse[element.getAttribute(DomElementObjectPool.OBJECT_ID_ATTRIBUTE)];
this._cleanElement(element);
this._pool.push(element);
}
/**
* Creates a new element for the pool.
*/
private _createNew(): HTMLElement {
const element = document.createElement(this._type);
const id = DomElementObjectPool._objectCount++;
element.setAttribute(DomElementObjectPool.OBJECT_ID_ATTRIBUTE, id.toString(10));
return element;
}
/**
* Resets an element back to a "clean state".
* @param element The element to be cleaned.
*/
private _cleanElement(element: HTMLElement): void {
element.className = '';
element.innerHTML = '';
}
}
+2 -1
View File
@@ -153,7 +153,8 @@
overflow-y: scroll; overflow-y: scroll;
} }
.terminal .xterm-wide-char { .terminal .xterm-wide-char,
.terminal .xterm-normal-char {
display: inline-block; display: inline-block;
} }
+3 -1
View File
@@ -760,7 +760,9 @@ Terminal.loadAddon = function(addon, callback) {
* character width has been changed. * character width has been changed.
*/ */
Terminal.prototype.updateCharSizeCSS = function() { Terminal.prototype.updateCharSizeCSS = function() {
this.charSizeStyleElement.textContent = '.xterm-wide-char{width:' + (this.charMeasure.width * 2) + 'px;}'; this.charSizeStyleElement.textContent =
`.xterm-wide-char{width:${this.charMeasure.width * 2}px;}` +
`.xterm-normal-char{width:${this.charMeasure.width}px;}`
} }
/** /**