Skip to content

v1.22.0

Choose a tag to compare

@gmpassos gmpassos released this 14 Oct 07:34
862e069
  • DBSQLAdapter.executedSQLsCount: the number of SQL statements (round
    trips) executed for entity and relationship operations. The shared DB
    tests pin the statements of each operation (insert, selectByID,
    selectByQuery, length, update, deleteByID) on every SQL adapter, so
    an optimization or a regression changes them visibly.

    • benchmark/db_users: --repeat N (the median of N rounds per
      operation, with their range). The users registered by each register
      round are removed after it, so every round (and update/remove after
      it) runs on the same table sizes. The benchmark's User.create() no
      longer hashes an empty password for every user loaded, and
      Address.state is indexed (user/byState).
  • Optimization: storing the relationships of a just inserted entity, with
    an ID generated by that insert, no longer runs a DELETE of its other
    relationship rows (it has none) on the adapters whose generated IDs are
    never reused (DBSQLAdapter.generatedIDsHaveNoRelationships: PostgreSQL,
    MySQL): one statement (round trip) less per relationship field on every
    insert. An insert of a user with an address (2 relationship fields) and
    2 roles: from 9 statements to 6.

    • TransactionOperationStoreRelationship.newEntity.
    • The relationship tables bones_api generates delete a deleted entity's
      rows (ON DELETE CASCADE). A relationship table without it, on a
      database that can reuse an ID (MySQL before 8.0 resets AUTO_INCREMENT
      on restart; a reset PostgreSQL sequence), could give a new entity a
      deleted one's relationship rows: override
      generatedIDsHaveNoRelationships as false then.
  • Optimization: generated relationship tables no longer get an index on the
    source column (<table>__<source>__idx): it's the first column of their
    UNIQUE (source, target) constraint's index, which already serves
    lookups by it (one index less to update on every write). Existing tables
    keep it; checkTables doesn't require it (PostgreSQL resolves an index
    by its first column).

  • Optimization: Transaction no longer captures a StackTrace on every
    commit (it only located an unexpected error): only with asserts enabled
    (debug, tests). user/count of the users benchmark: +62% (memory), +32%
    (SQLite).

  • Optimization: SQL.sqlPositional (SQLite) is computed once per SQL text
    (with the default placeholder), not with a regular expression per
    statement.

  • Optimization: DBPostgreSQLAdapter resolves the column names of a result
    once for all its rows.

  • Optimization: @pragma('vm:prefer-inline') on small functions of hot
    paths that a CPU profile showed not inlined: ZoneField.get (the
    executing Transaction), the row layout match of createFromMap, the
    digits of parseDateTimeISOUTCFast (a top-level function, not a closure)
    and the common values (null, num, String, bool) of the tracking
    copy of an entity's fields. Users benchmark: byState (a join, 20 users)
    +3% (SQLite), +1% (memory); the others within their variation.

  • Behavior change: a SQL select with a limit (and no orderByID) is
    now ordered by the ID column (ORDER BY id ... LIMIT n), as one with an
    offset already was: which rows an unordered LIMIT returns is
    undefined in SQL (it can change with the query plan), e.g. which row a
    selectFirstByQuery returns. Pass orderByID: false to opt out. The
    in-memory adapters (DBSQLMemoryAdapter, the object adapters,
    IterableEntityRepository) keep their rows in insertion order and still
    order only for an offset. A limit of 0 (no LIMIT clause) doesn't
    order.

    • OrderDirection.resolveOrderByID(orderByID, offset, [limit]).
  • Fix: DBObjectGCSAdapter: parallel cache writes over the cache limits
    (cacheLimit/cacheFilesLimit) started concurrent limit checks, which
    listed and deleted the same files: the second delete threw a
    PathNotFoundException in a future nobody awaited (an unhandled error).
    The checks now run one at a time: a call during a check queues one more
    check after it (shared by the calls until then), since the running one
    may have listed the files before that call's write. A cached file already
    removed counts as deleted, and an error of a check (nobody awaits it: e.g.
    the cache directory removed) is logged, not left as an unhandled error.

  • Optimization: turning rows (or any Map) into entities
    (EntityHandler.createFromMap), for every adapter:

    • resolveFieldsValues resolves the values into one map, synchronously
      when none of them is a Future (no forEachAsync, entries and
      intermediate maps per row).
    • The key of each field in a row is matched once per row layout (its
      keys, in order) and reused for the next rows with the same keys,
      instead of per row (with 2 lookup maps built per row).
    • resolveValueByType no longer builds a TypeInfo of every value: the
      conditions that don't depend on the value are checked first.
    • parseDateTimeISOUTCFast: the DateTimes DBSQLiteAdapter stores
      (toUtc().toIso8601String()) are parsed without a regular expression.
    • Users benchmark vs 1.22.0's previous changes: SQLite list/byState
      (20 users per call) +31%, single-user reads +11–12%; memory byId
      +12%.
  • Fix: DBMySQLAdapter: a NULL column of a select with parameters (e.g.
    selectByQuery(' name == ? ')) was read as absent, so the entity field
    got its default (e.g. 0) instead of null: mysql1 leaves it out of a
    row's fields in its binary protocol (a statement with parameters). The
    rows are now read from their values.

  • Fix: ConditionSQLEncoder.encodeIDCondition/encodeIDConditionIN built
    the ID column from the encoding parameter key, which is renamed (e.g.
    "param_id") when the query has a parameter named like the ID column.

  • SQL: the IDs of an ID condition (#ID == x, selectByID, selectByIDs
    and the selects of references) are now parameters (placeholders), not
    values written into the statement: its text is the same for any ID. An
    IN list of IDs is padded (repeating its last ID) to 1, 2, 4, 8
    ... 256, then multiples of 256 (ConditionSQLEncoder.idsListPaddedLength),
    so its text is the same for lists of similar lengths. An ID that is
    already a parameter of the query (#ID == ?) keeps it. Not faster by
    itself (users benchmark: within −1.5% and +0.6%): the groundwork for
    reusing statements (prepared statements per connection).

    • SQLDialect.intParameterTypeSuffix (PostgreSQL: :int8): an integer
      ID parameter is typed, so an ID beyond the column's type matches
      nothing (as a literal did) instead of failing ("out of range").
    • SQLDialect.maxIDsAsParameters: an IN list padded beyond it is
      written as values. A parameter is only faster when its statement is
      reused, which a long list's length seldom is, and a statement's
      parameters are limited (SQLite: 32766; PostgreSQL: 65535; e.g. a
      selectByIDs of 40000 IDs). Measured selecting N rows by ID (reused
      parameters vs values): PostgreSQL 512 (parameters 16–29% faster up
      to ~400 IDs, about even at ~640–768, values faster from ~1024); SQLite
      1024 (the same cost per ID, ~10–15% less when reused); MySQL 0, which
      keeps the IDs written as values: mysql1 runs a statement with
      parameters as prepare + execute (2 round trips, not reused), one
      without as 1. Splitting a list into batches is never faster than
      values (PostgreSQL: a round trip per batch).
  • Behavior change: prepared statements, reused by SQL text
    (PreparedStatementsCache; the least recently used released over
    preparedStatementsCacheSize, default 100 per connection). On by
    default; db config preparedStatements: false disables them. Behind
    PgBouncer in transaction pooling mode before 1.21, disable them
    : a
    prepared statement's name can then refer, on a shared server connection,
    to another client's statement, which runs instead (silently). DBSQLAdapter.preparedStatementsInfo: hits, prepared, evicted and
    stale counters. An invalid preparedStatementsCacheSize (< 1) passed to
    an adapter's constructor throws an ArgumentError there (a config value
    falls back to the default).

    • DBPostgreSQLAdapter: per connection. A SQL text is prepared when it
      repeats (preparing costs a round trip; a statement run once doesn't pay
      it), and only outside a transaction (it would be prepared on the
      transaction's session, ending with it); a prepared statement is reused
      also inside the connection's transactions. A stale one (0A000: a
      schema change altered its result columns; 26000) is replaced, and the
      statement run again unprepared (outside a transaction: inside one the
      error aborted it). A cached statement runs only while its session is
      open: on the session of a finished transaction it fails, as an
      unprepared one does, instead of running outside that transaction.
    • DBSQLiteAdapter: per database handle, on the first use (no round
      trip); SQLite recompiles a prepared statement after a schema change.
      Released before the handle is closed. A SQL with more than one
      statement is still rejected (checkNoTail), not run up to its first.
    • DBMySQLAdapter: none (mysql1 doesn't reuse prepared statements).
    • Users benchmark vs the previous changes: SQLite single-user reads
      +17–19%, writes +13–15%, list/byState +4–5%; PostgreSQL reads
      +8–12%, writes about the same.