diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-02-01 20:05:18 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-02-01 20:05:18 -0500 |
commit | b042a8599c47b2fd41c7eb0d22ec417ca629a986 (patch) | |
tree | 870ffbbfe86ada39733d8f31971379caa0e1ab4a | |
parent | bb9c6b6ff3ca196977f9ddd73120897c679e2515 (diff) |
small change to horizontal scrolling for massive performance improvement on large files
also fixed syntax highlighting for hex literals, for real this time
-rw-r--r-- | buffer.c | 17 | ||||
-rw-r--r-- | math.c | 58 | ||||
-rw-r--r-- | syntax.c | 2 | ||||
-rw-r--r-- | ted.h | 2 |
4 files changed, 37 insertions, 42 deletions
@@ -809,23 +809,13 @@ static u32 buffer_column_to_index(TextBuffer *buffer, u32 line, u32 column) { } // returns the number of lines of text in the buffer into *lines (if not NULL), -// and the number of columns of text, i.e. the number of columns in the longest line, into *cols (if not NULL) +// and the number of columns of text, i.e. the number of columns in the longest line displayed, into *cols (if not NULL) void buffer_text_dimensions(TextBuffer *buffer, u32 *lines, u32 *columns) { if (lines) { *lines = buffer->nlines; } if (columns) { - // @OPTIMIZE - u32 nlines = buffer->nlines; - Line *line_arr = buffer->lines; - u32 maxcol = 0; - for (u32 i = 0; i < nlines; ++i) { - Line *line = &line_arr[i]; - u32 cols = buffer_index_to_column(buffer, i, line->len); - if (cols > maxcol) - maxcol = cols; - } - *columns = maxcol; + *columns = buffer->longest_line_on_screen; } } @@ -2043,8 +2033,11 @@ void buffer_render(TextBuffer *buffer, Rect r) { buffer->frame_earliest_line_modified = U32_MAX; buffer->frame_latest_line_modified = 0; + buffer->longest_line_on_screen = 0; + for (u32 line_idx = start_line; line_idx < nlines; ++line_idx) { Line *line = &lines[line_idx]; + buffer->longest_line_on_screen = max_u32(buffer->longest_line_on_screen, line->len); if (arr_len(char_types) < line->len) { arr_set_len(char_types, line->len); } @@ -17,54 +17,54 @@ #include <math.h> -static float degrees(float r) { +static inline float degrees(float r) { return r * (180.0f / PIf); } -static float radians(float r) { +static inline float radians(float r) { return r * (PIf / 180.f); } // map x from the interval [0, 1] to the interval [a, b]. does NOT clamp. -static float lerpf(float x, float a, float b) { +static inline float lerpf(float x, float a, float b) { return x * (b-a) + a; } // opposite of lerp; map x from the interval [a, b] to the interval [0, 1]. does NOT clamp. -static float normf(float x, float a, float b) { +static inline float normf(float x, float a, float b) { return (x-a) / (b-a); } -static float clampf(float x, float a, float b) { +static inline float clampf(float x, float a, float b) { if (x < a) return a; if (x > b) return b; return x; } -static int clampi(int x, int a, int b) { +static inline int clampi(int x, int a, int b) { if (x < a) return a; if (x > b) return b; return x; } -static i16 clamp_i16(i16 x, i16 a, i16 b) { +static inline i16 clamp_i16(i16 x, i16 a, i16 b) { if (x < a) return a; if (x > b) return b; return x; } -static u16 clamp_u16(u16 x, u16 a, u16 b) { +static inline u16 clamp_u16(u16 x, u16 a, u16 b) { if (x < a) return a; if (x > b) return b; return x; } -static i32 clamp_i32(i32 x, i32 a, i32 b) { +static inline i32 clamp_i32(i32 x, i32 a, i32 b) { if (x < a) return a; if (x > b) return b; return x; } -static u32 clamp_u32(u32 x, u32 a, u32 b) { +static inline u32 clamp_u32(u32 x, u32 a, u32 b) { if (x < a) return a; if (x > b) return b; return x; @@ -72,88 +72,88 @@ static u32 clamp_u32(u32 x, u32 a, u32 b) { // remap x from the interval [from_a, from_b] to the interval [to_a, to_b], NOT clamping if x is outside the "from" interval. -static float remapf(float x, float from_a, float from_b, float to_a, float to_b) { +static inline float remapf(float x, float from_a, float from_b, float to_a, float to_b) { float pos = (x - from_a) / (from_b - from_a); return lerpf(pos, to_a, to_b); } -static float minf(float a, float b) { +static inline float minf(float a, float b) { return a < b ? a : b; } -static float maxf(float a, float b) { +static inline float maxf(float a, float b) { return a > b ? a : b; } -static double maxd(double a, double b) { +static inline double maxd(double a, double b) { return a > b ? a : b; } -static double mind(double a, double b) { +static inline double mind(double a, double b) { return a < b ? a : b; } -static u32 min_u32(u32 a, u32 b) { +static inline u32 min_u32(u32 a, u32 b) { return a < b ? a : b; } -static u32 max_u32(u32 a, u32 b) { +static inline u32 max_u32(u32 a, u32 b) { return a > b ? a : b; } -static i32 min_i32(i32 a, i32 b) { +static inline i32 min_i32(i32 a, i32 b) { return a < b ? a : b; } -static i32 max_i32(i32 a, i32 b) { +static inline i32 max_i32(i32 a, i32 b) { return a > b ? a : b; } -static u64 min_u64(u64 a, u64 b) { +static inline u64 min_u64(u64 a, u64 b) { return a < b ? a : b; } -static u64 max_u64(u64 a, u64 b) { +static inline u64 max_u64(u64 a, u64 b) { return a > b ? a : b; } -static i64 min_i64(i64 a, i64 b) { +static inline i64 min_i64(i64 a, i64 b) { return a < b ? a : b; } -static i64 max_i64(i64 a, i64 b) { +static inline i64 max_i64(i64 a, i64 b) { return a > b ? a : b; } -static i64 mod_i64(i64 a, i64 b) { +static inline i64 mod_i64(i64 a, i64 b) { i64 ret = a % b; if (ret < 0) ret += b; return ret; } -static i64 abs_i64(i64 x) { +static inline i64 abs_i64(i64 x) { return x < 0 ? -x : +x; } -static i64 sgn_i64(i64 x) { +static inline i64 sgn_i64(i64 x) { if (x < 0) return -1; if (x > 0) return +1; return 0; } -static float sgnf(float x) { +static inline float sgnf(float x) { if (x < 0) return -1; if (x > 0) return +1; return 0; } -static float smoothstepf(float x) { +static inline float smoothstepf(float x) { if (x <= 0) return 0; if (x >= 1) return 1; return x * x * (3 - 2 * x); } -static float randf(void) { +static inline float randf(void) { return (float)rand() / (float)((ulong)RAND_MAX + 1); } @@ -186,7 +186,7 @@ static void syntax_highlight_c(SyntaxState *state_ptr, char32_t *line, u32 line_ } if (line[i] != '\\') backslashes = 0; if (in_number && !(is32_digit(line[i]) || line[i] == '.' - || (line[i] < CHAR_MAX && strchr("xXoObBlLuU", (char)line[i])) + || (line[i] < CHAR_MAX && strchr("xXoObBlLuUabcdefABCDEF", (char)line[i])) || (i && line[i-1] == 'e' && (line[i] == '+' || line[i] == '-')))) { in_number = false; } @@ -118,6 +118,8 @@ typedef struct { u32 nlines; u32 lines_capacity; + u32 longest_line_on_screen; // length of the longest line on screen. used to determine how far right we can scroll. + // to cache syntax highlighting properly, it is important to keep track of the // first and last line modified since last frame. u32 frame_earliest_line_modified; |