optimize encoding of move ops
This commit is contained in:
		
							parent
							
								
									ab5061cd47
								
							
						
					
					
						commit
						4078e115c1
					
				@ -4,9 +4,10 @@ import * as decoding from 'lib0/decoding'
 | 
				
			|||||||
import * as encoding from 'lib0/encoding'
 | 
					import * as encoding from 'lib0/encoding'
 | 
				
			||||||
import * as math from 'lib0/math'
 | 
					import * as math from 'lib0/math'
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  AbstractType, ContentType, RelativePosition, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Transaction, Item, StructStore, getItem, getItemCleanStart, getItemCleanEnd // eslint-disable-line
 | 
					  writeID,
 | 
				
			||||||
 | 
					  readID,
 | 
				
			||||||
 | 
					  ID, AbstractType, ContentType, RelativePosition, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Transaction, Item, StructStore, getItem, getItemCleanStart, getItemCleanEnd // eslint-disable-line
 | 
				
			||||||
} from '../internals.js'
 | 
					} from '../internals.js'
 | 
				
			||||||
import { decodeRelativePosition, encodeRelativePosition } from 'yjs'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {ContentMove | { start: RelativePosition, end: RelativePosition }} moved
 | 
					 * @param {ContentMove | { start: RelativePosition, end: RelativePosition }} moved
 | 
				
			||||||
@ -230,12 +231,11 @@ export class ContentMove {
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  write (encoder, offset) {
 | 
					  write (encoder, offset) {
 | 
				
			||||||
    const isCollapsed = this.isCollapsed()
 | 
					    const isCollapsed = this.isCollapsed()
 | 
				
			||||||
    encoding.writeUint8(encoder.restEncoder, isCollapsed ? 1 : 0)
 | 
					    encoding.writeVarUint(encoder.restEncoder, (isCollapsed ? 1 : 0) | (this.start.assoc >= 0 ? 2 : 0) | (this.end.assoc >= 0 ? 4 : 0) | this.priority << 3)
 | 
				
			||||||
    encoder.writeBuf(encodeRelativePosition(this.start))
 | 
					    writeID(encoder.restEncoder, /** @type {ID} */ (this.start.item))
 | 
				
			||||||
    if (!isCollapsed) {
 | 
					    if (!isCollapsed) {
 | 
				
			||||||
      encoder.writeBuf(encodeRelativePosition(this.end))
 | 
					      writeID(encoder.restEncoder, /** @type {ID} */ (this.end.item))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    encoding.writeVarUint(encoder.restEncoder, this.priority)
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@ -258,11 +258,13 @@ export class ContentMove {
 | 
				
			|||||||
 * @return {ContentMove}
 | 
					 * @return {ContentMove}
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export const readContentMove = decoder => {
 | 
					export const readContentMove = decoder => {
 | 
				
			||||||
  const isCollapsed = decoding.readUint8(decoder.restDecoder) === 1
 | 
					  const info = decoding.readVarUint(decoder.restDecoder)
 | 
				
			||||||
  const start = decodeRelativePosition(decoder.readBuf())
 | 
					  const isCollapsed = (info & 1) === 1
 | 
				
			||||||
  const end = isCollapsed ? start.clone() : decodeRelativePosition(decoder.readBuf())
 | 
					  const startAssoc = (info & 2) === 2 ? 0 : -1
 | 
				
			||||||
  if (isCollapsed) {
 | 
					  const endAssoc = (info & 4) === 4 ? 0 : -1
 | 
				
			||||||
    end.assoc = -1
 | 
					  const priority = info >>> 3
 | 
				
			||||||
  }
 | 
					  const startId = readID(decoder.restDecoder)
 | 
				
			||||||
  return new ContentMove(start, end, decoding.readVarUint(decoder.restDecoder))
 | 
					  const start = new RelativePosition(null, null, startId, startAssoc)
 | 
				
			||||||
 | 
					  const end = new RelativePosition(null, null, isCollapsed ? startId : readID(decoder.restDecoder), endAssoc)
 | 
				
			||||||
 | 
					  return new ContentMove(start, end, priority)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -167,6 +167,8 @@ export class YArray extends AbstractType {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * @experimental
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param {number} startIndex Inclusive move-start
 | 
					   * @param {number} startIndex Inclusive move-start
 | 
				
			||||||
   * @param {number} endIndex Inclusive move-end
 | 
					   * @param {number} endIndex Inclusive move-end
 | 
				
			||||||
   * @param {number} target
 | 
					   * @param {number} target
 | 
				
			||||||
 | 
				
			|||||||
@ -513,8 +513,9 @@ export const testMove2 = tc => {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 | 
					 * @todo
 | 
				
			||||||
 * @param {t.TestCase} tc
 | 
					 * @param {t.TestCase} tc
 | 
				
			||||||
 */
 | 
					 *
 | 
				
			||||||
export const testMoveCircles = tc => {
 | 
					export const testMoveCircles = tc => {
 | 
				
			||||||
  const { testConnector, array0, array1 } = init(tc, { users: 3 })
 | 
					  const { testConnector, array0, array1 } = init(tc, { users: 3 })
 | 
				
			||||||
  array0.insert(0, [1, 2, 3, 4])
 | 
					  array0.insert(0, [1, 2, 3, 4])
 | 
				
			||||||
@ -528,6 +529,7 @@ export const testMoveCircles = tc => {
 | 
				
			|||||||
  t.assert(array0.length === array0.toArray().length)
 | 
					  t.assert(array0.length === array0.toArray().length)
 | 
				
			||||||
  t.compareArrays(array0.toArray(), array1.toArray())
 | 
					  t.compareArrays(array0.toArray(), array1.toArray())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {t.TestCase} tc
 | 
					 * @param {t.TestCase} tc
 | 
				
			||||||
@ -563,12 +565,13 @@ const arrayTransactions = [
 | 
				
			|||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const pos = prng.int32(gen, 0, yarray.length - 1)
 | 
					    const pos = prng.int32(gen, 0, yarray.length - 1)
 | 
				
			||||||
    const len = prng.int32(gen, 1, math.min(3, yarray.length - pos))
 | 
					    const len = 1 // prng.int32(gen, 1, math.min(3, yarray.length - pos))
 | 
				
			||||||
    const _newPosAdj = prng.int32(gen, 0, yarray.length - len)
 | 
					    const _newPosAdj = prng.int32(gen, 0, yarray.length - len)
 | 
				
			||||||
    // make sure that we don't insert in-between the moved range
 | 
					    // make sure that we don't insert in-between the moved range
 | 
				
			||||||
    const newPos = _newPosAdj + (_newPosAdj > pos ? len : 0)
 | 
					    const newPos = _newPosAdj + (_newPosAdj > pos ? len : 0)
 | 
				
			||||||
    const oldContent = yarray.toArray()
 | 
					    const oldContent = yarray.toArray()
 | 
				
			||||||
    yarray.moveRange(pos, pos + len - 1, newPos)
 | 
					    // yarray.moveRange(pos, pos + len - 1, newPos)
 | 
				
			||||||
 | 
					    yarray.move(pos, newPos)
 | 
				
			||||||
    const movedValues = oldContent.splice(pos, len)
 | 
					    const movedValues = oldContent.splice(pos, len)
 | 
				
			||||||
    oldContent.splice(pos < newPos ? newPos - len : newPos, 0, ...movedValues)
 | 
					    oldContent.splice(pos < newPos ? newPos - len : newPos, 0, ...movedValues)
 | 
				
			||||||
    t.compareArrays(yarray.toArray(), oldContent) // we want to make sure that fastSearch markers insert at the correct position
 | 
					    t.compareArrays(yarray.toArray(), oldContent) // we want to make sure that fastSearch markers insert at the correct position
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user